Skip to content

Commit b39c251

Browse files
remove panic. fix typos.
1 parent 2b5e06a commit b39c251

File tree

13 files changed

+66
-33
lines changed

13 files changed

+66
-33
lines changed

app/engine/src/iter_util.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Some extra iterator methods.
2+
3+
/// Iterator extension trait.
4+
pub trait IterExt: Iterator {
5+
/// Fallible version of `any()`. Short circuits
6+
/// on true or error.
7+
#[inline]
8+
fn try_any<F, E>(&mut self, mut f: F) -> Result<bool, E>
9+
where
10+
Self: Sized,
11+
F: FnMut(Self::Item) -> Result<bool, E>,
12+
{
13+
for x in self.by_ref() {
14+
if f(x)? {
15+
return Ok(true);
16+
}
17+
}
18+
Ok(false)
19+
}
20+
}
21+
22+
impl<T> IterExt for std::slice::Iter<'_, T> {}

app/engine/src/lib.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
pub mod args;
33
pub mod config;
44
pub mod driver;
5+
mod iter_util;
56
pub mod parser;
67
mod rename;
78
mod serde;
@@ -63,7 +64,7 @@ impl Display for FileParseErrors {
6364
#[non_exhaustive]
6465
pub enum FileErrorKind {
6566
/// We couldn't figure which crate this file belongs to, which we need in
66-
/// mutli-file mode
67+
/// multi-file mode
6768
UnknownCrate,
6869

6970
/// There were parse errors
@@ -76,13 +77,13 @@ pub enum FileErrorKind {
7677
impl Display for FileErrorKind {
7778
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
7879
match self {
79-
FileErrorKind::UnknownCrate => f.write_str("unknown crate in mutli-file mode"),
80+
FileErrorKind::UnknownCrate => f.write_str("unknown crate in multi-file mode"),
8081
FileErrorKind::ParseErrors(parse_error_set) => parse_error_set.fmt(f),
8182
FileErrorKind::ReadError(error) => write!(f, "i/o error: {error}"),
8283
}
8384
}
8485
}
85-
/// A group of parse errors from a single file. Guaranteed to be non-emtpy.
86+
/// A group of parse errors from a single file. Guaranteed to be non-empty.
8687
#[derive(Debug)]
8788
pub struct ParseErrorSet {
8889
errors: Vec<ParseError>,

app/engine/src/parser.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Source file parsing.
22
use crate::{
3+
iter_util::IterExt as _,
34
rename::RenameExt,
45
target_os,
56
type_parser::{parse_rust_type, parse_rust_type_from_string, type_name},
@@ -278,7 +279,7 @@ pub fn parse_input(
278279
)
279280
}
280281

281-
/// Check if we have not parsed any relavent typehsared types.
282+
/// Check if we have not parsed any relevant typehsared types.
282283
fn is_parsed_data_empty(parsed_data: &ParsedData) -> bool {
283284
parsed_data.enums.is_empty()
284285
&& parsed_data.aliases.is_empty()
@@ -474,14 +475,21 @@ pub(crate) fn parse_enum(e: &ItemEnum, valid_os: Option<&[&str]>) -> Result<Rust
474475
.collect::<Result<Vec<_>, _>>()?;
475476

476477
// Check if the enum references itself recursively in any of its variants
477-
let is_recursive = variants.iter().any(|v| match v {
478-
RustEnumVariant::Unit(_) => false,
479-
RustEnumVariant::Tuple { ty, .. } => ty.contains_type(&original_enum_ident),
480-
RustEnumVariant::AnonymousStruct { fields, .. } => fields
481-
.iter()
482-
.any(|f| f.ty.contains_type(&original_enum_ident)),
483-
_ => panic!("unrecgonized enum type"),
484-
});
478+
let is_recursive = variants.iter().try_any(|v| {
479+
Ok(match v {
480+
RustEnumVariant::Unit(_) => false,
481+
RustEnumVariant::Tuple { ty, .. } => ty.contains_type(&original_enum_ident),
482+
RustEnumVariant::AnonymousStruct { fields, .. } => fields
483+
.iter()
484+
.any(|f| f.ty.contains_type(&original_enum_ident)),
485+
_ => {
486+
return Err(ParseError::new(
487+
&e,
488+
ParseErrorKind::UnsupportedType("Unsupported enum type".into()),
489+
))
490+
}
491+
})
492+
})?;
485493

486494
let shared = RustEnumShared {
487495
id: get_ident(Some(&e.ident), &e.attrs, None),

app/engine/src/serde/args.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
//! Command line argument parsing.
2+
use serde::ser;
23
use std::{
34
collections::HashMap,
45
fmt::{self, Display, Write},
56
};
67

7-
use serde::ser;
8-
98
#[derive(Debug, Clone, Copy)]
109
pub enum ArgType {
1110
Bool,

app/engine/src/serde/config.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1+
//! Deserialization for configuration.
2+
use super::args::{ArgSpec, ArgType, CliArgsSet};
3+
use itertools::Itertools;
4+
use serde::de::{self, value::BorrowedStrDeserializer};
15
use std::{
26
ffi::{OsStr, OsString},
37
str::FromStr,
48
};
59

6-
use itertools::Itertools;
7-
use serde::de::{self, value::BorrowedStrDeserializer};
8-
9-
use super::args::{ArgSpec, ArgType, CliArgsSet};
10-
1110
#[derive(Debug, thiserror::Error)]
1211
pub enum ConfigDeserializeError {
1312
#[error("error from Deserialize type: {0}")]

app/engine/src/serde/empty.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Deserialization for empty
12
use serde::de;
23
use thiserror::Error;
34

app/engine/src/serde/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Various serde deserializers
12
pub mod args;
23
pub mod config;
34
pub mod empty;

app/engine/src/serde/toml.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::{marker::PhantomData, str};
2-
1+
//! Toml deserialization.
32
use serde::de;
3+
use std::{marker::PhantomData, str};
44

55
/// Borrowing deserializer for a `toml::Value`
66
pub struct ValueDeserializer<'a, E> {

app/engine/src/target_os.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Parsing of `target_os` attributes
12
use proc_macro2::{Delimiter, Group, TokenStream};
23
use syn::{parse::Parser, punctuated::Punctuated, Ident, LitStr, Token};
34

app/engine/src/topsort.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
//! Top sort of rust types.
2+
use crate::writer::BorrowedRustItem;
13
use std::collections::{HashMap, HashSet};
2-
34
use typeshare_model::prelude::*;
45

5-
use crate::writer::BorrowedRustItem;
6-
76
fn get_dependencies_from_type<'a>(
87
tp: &'a RustType,
98
types: &HashMap<&'a TypeName, BorrowedRustItem<'a>>,

0 commit comments

Comments
 (0)