Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ impl TypeSpace {
enum_values,
..
} if multiple.len() == 2 && multiple.contains(&InstanceType::Null) => {
let only_null = enum_values.as_ref().map_or(false, |values| {
values.iter().all(serde_json::Value::is_null)
});
let only_null = enum_values
.as_ref()
.is_some_and(|values| values.iter().all(serde_json::Value::is_null));

if only_null {
// If there are enumerated values and they're all null,
Expand Down
122 changes: 50 additions & 72 deletions typify-impl/src/defaults.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,24 +151,24 @@ impl TypeEntry {
}) => match tag_type {
EnumTagType::External => {
validate_default_for_external_enum(type_space, variants, default)
.ok_or_else(|| Error::invalid_value())
.ok_or_else(Error::invalid_value)
}
EnumTagType::Internal { tag } => {
validate_default_for_internal_enum(type_space, variants, default, tag)
.ok_or_else(|| Error::invalid_value())
.ok_or_else(Error::invalid_value)
}
EnumTagType::Adjacent { tag, content } => {
validate_default_for_adjacent_enum(type_space, variants, default, tag, content)
.ok_or_else(|| Error::invalid_value())
.ok_or_else(Error::invalid_value)
}
EnumTagType::Untagged => {
validate_default_for_untagged_enum(type_space, variants, default)
.ok_or_else(|| Error::invalid_value())
.ok_or_else(Error::invalid_value)
}
},
TypeEntryDetails::Struct(TypeEntryStruct { properties, .. }) => {
validate_default_struct_props(properties, type_space, default)
.ok_or_else(|| Error::invalid_value())
.ok_or_else(Error::invalid_value)
}

TypeEntryDetails::Newtype(TypeEntryNewtype { type_id, .. }) => {
Expand Down Expand Up @@ -242,8 +242,9 @@ impl TypeEntry {
Err(Error::invalid_value())
}
}
TypeEntryDetails::Tuple(ids) => validate_default_tuple(ids, type_space, default)
.ok_or_else(|| Error::invalid_value()),
TypeEntryDetails::Tuple(ids) => {
validate_default_tuple(ids, type_space, default).ok_or_else(Error::invalid_value)
}

TypeEntryDetails::Array(type_id, length) => {
let Some(arr) = default.as_array() else {
Expand Down Expand Up @@ -648,10 +649,9 @@ mod tests {
let (type_space, type_id) = get_type::<Option<u32>>();
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();

assert!(matches!(
type_entry.validate_value(&type_space, &json!("forty-two")),
Err(_)
));
assert!(type_entry
.validate_value(&type_space, &json!("forty-two"))
.is_err());
assert!(matches!(
type_entry.validate_value(&type_space, &json!(null)),
Ok(DefaultKind::Intrinsic)
Expand All @@ -671,10 +671,9 @@ mod tests {
extra_derives: Default::default(),
};

assert!(matches!(
type_entry.validate_value(&type_space, &json!("forty-two")),
Err(_)
));
assert!(type_entry
.validate_value(&type_space, &json!("forty-two"))
.is_err());
assert!(matches!(
type_entry.validate_value(&type_space, &json!(null)),
Ok(DefaultKind::Intrinsic)
Expand All @@ -690,10 +689,9 @@ mod tests {
let (type_space, type_id) = get_type::<Vec<u32>>();
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();

assert!(matches!(
type_entry.validate_value(&type_space, &json!([null])),
Err(_),
));
assert!(type_entry
.validate_value(&type_space, &json!([null]))
.is_err());
assert!(matches!(
type_entry.validate_value(&type_space, &json!([])),
Ok(DefaultKind::Intrinsic),
Expand All @@ -709,10 +707,7 @@ mod tests {
let (type_space, type_id) = get_type::<HashMap<String, u32>>();
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();

assert!(matches!(
type_entry.validate_value(&type_space, &json!([])),
Err(_),
));
assert!(type_entry.validate_value(&type_space, &json!([])).is_err());
assert!(matches!(
type_entry.validate_value(&type_space, &json!({})),
Ok(DefaultKind::Intrinsic),
Expand All @@ -728,10 +723,9 @@ mod tests {
let (type_space, type_id) = get_type::<(u32, u32, String)>();
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();

assert!(matches!(
type_entry.validate_value(&type_space, &json!([1, 2, "three", 4])),
Err(_),
));
assert!(type_entry
.validate_value(&type_space, &json!([1, 2, "three", 4]))
.is_err());
assert!(matches!(
type_entry.validate_value(&type_space, &json!([1, 2, "three"])),
Ok(DefaultKind::Specific),
Expand Down Expand Up @@ -769,10 +763,9 @@ mod tests {
let (type_space, type_id) = get_type::<u32>();
let type_entry = type_space.id_to_entry.get(&type_id).unwrap();

assert!(matches!(
type_entry.validate_value(&type_space, &json!(true)),
Err(_),
));
assert!(type_entry
.validate_value(&type_space, &json!(true))
.is_err());
assert!(matches!(
type_entry.validate_value(&type_space, &json!(0)),
Ok(DefaultKind::Intrinsic),
Expand Down Expand Up @@ -822,8 +815,8 @@ mod tests {
),
Ok(DefaultKind::Specific),
));
assert!(matches!(
type_entry.validate_value(
assert!(type_entry
.validate_value(
&type_space,
&json!(
{
Expand All @@ -832,11 +825,10 @@ mod tests {
"d": 7
}
)
),
Err(_),
));
assert!(matches!(
type_entry.validate_value(
)
.is_err());
assert!(type_entry
.validate_value(
&type_space,
&json!(
{
Expand All @@ -845,9 +837,8 @@ mod tests {
"d": {}
}
)
),
Err(_),
));
)
.is_err());
}

#[test]
Expand Down Expand Up @@ -885,14 +876,10 @@ mod tests {
),
Ok(DefaultKind::Specific),
));
assert!(matches!(
type_entry.validate_value(&type_space, &json!({ "A": null })),
Err(_),
));
assert!(matches!(
type_entry.validate_value(&type_space, &json!("B")),
Err(_),
));
assert!(type_entry
.validate_value(&type_space, &json!({ "A": null }))
.is_err());
assert!(type_entry.validate_value(&type_space, &json!("B")).is_err());
}

#[test]
Expand Down Expand Up @@ -928,25 +915,23 @@ mod tests {
),
Ok(DefaultKind::Specific),
));
assert!(matches!(
type_entry.validate_value(
assert!(type_entry
.validate_value(
&type_space,
&json!({
"not-tag": "A"
})
),
Err(_),
));
assert!(matches!(
type_entry.validate_value(
)
.is_err());
assert!(type_entry
.validate_value(
&type_space,
&json!({
"tag": "B",
"cc": "where's D?"
})
),
Err(_),
));
)
.is_err());
}

#[test]
Expand Down Expand Up @@ -992,20 +977,16 @@ mod tests {
),
Ok(DefaultKind::Specific),
));
assert!(matches!(
type_entry.validate_value(&type_space, &json!("A")),
Err(_),
));
assert!(matches!(
type_entry.validate_value(
assert!(type_entry.validate_value(&type_space, &json!("A")).is_err());
assert!(type_entry
.validate_value(
&type_space,
&json!({
"tag": "A",
"content": null,
})
),
Err(_),
));
)
.is_err());
}
#[test]
fn test_enum_untagged() {
Expand Down Expand Up @@ -1033,9 +1014,6 @@ mod tests {
type_entry.validate_value(&type_space, &json!( { "cc": "xx", "dd": "yy" })),
Ok(DefaultKind::Specific),
));
assert!(matches!(
type_entry.validate_value(&type_space, &json!({})),
Err(_),
));
assert!(type_entry.validate_value(&type_space, &json!({})).is_err());
}
}
2 changes: 1 addition & 1 deletion typify-impl/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ mod tests {
assert_eq!(variants.len(), 5);

assert!(matches!(
variants.get(0).unwrap(),
variants.first().unwrap(),
Variant {
details: VariantDetails::Simple,
..
Expand Down
10 changes: 3 additions & 7 deletions typify-impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,7 @@ impl Error {
pub type Result<T> = std::result::Result<T, Error>;

fn show_type_name(type_name: Option<&str>) -> &str {
if let Some(type_name) = type_name {
type_name
} else {
"<unknown type>"
}
type_name.unwrap_or("<unknown type>")
}

/// Representation of a type which may have a definition or may be built-in.
Expand Down Expand Up @@ -987,7 +983,7 @@ impl ToTokens for TypeSpace {
}
}

impl<'a> Type<'a> {
impl Type<'_> {
/// The name of the type as a String.
pub fn name(&self) -> String {
let Type {
Expand Down Expand Up @@ -1167,7 +1163,7 @@ impl<'a> TypeStruct<'a> {
}
}

impl<'a> TypeNewtype<'a> {
impl TypeNewtype<'_> {
/// Get the inner type of the newtype struct.
pub fn inner(&self) -> TypeId {
self.details.type_id.clone()
Expand Down
8 changes: 3 additions & 5 deletions typify-impl/src/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ fn merge_additional_properties(
) -> Option<Schema> {
match (a, b) {
(None, other) | (other, None) => other.cloned(),
(Some(aa), Some(bb)) => {
Some(try_merge_schema(aa, bb, defs).unwrap_or_else(|_| Schema::Bool(false)))
}
(Some(aa), Some(bb)) => Some(try_merge_schema(aa, bb, defs).unwrap_or(Schema::Bool(false))),
}
}

Expand Down Expand Up @@ -893,12 +891,12 @@ fn merge_so_array(
let aa_items_iter = aa_items.iter().chain(repeat(
aa_additional_items
.as_deref()
.unwrap_or_else(|| &Schema::Bool(true)),
.unwrap_or(&Schema::Bool(true)),
));
let bb_items_iter = bb_items.iter().chain(repeat(
bb_additional_items
.as_deref()
.unwrap_or_else(|| &Schema::Bool(true)),
.unwrap_or(&Schema::Bool(true)),
));
let items_iter = aa_items_iter.zip(bb_items_iter).take(items_len);
let (items, allow_additional_items) =
Expand Down
15 changes: 8 additions & 7 deletions typify-impl/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl OutputSpace {
) {
self.items
.entry((location, order_hint.to_string()))
.or_insert_with(TokenStream::new)
.or_default()
.extend(stream);
}

Expand All @@ -36,12 +36,13 @@ impl OutputSpace {
.items
.into_iter()
.map(|((location, _), item)| (location, item))
.fold(BTreeMap::new(), |mut map, (location, item)| {
map.entry(location)
.or_insert_with(TokenStream::new)
.extend(item);
map
});
.fold(
BTreeMap::<_, TokenStream>::new(),
|mut map, (location, item)| {
map.entry(location).or_default().extend(item);
map
},
);

let mod_streams = mods.into_iter().map(|(location, items)| match location {
OutputSpaceMod::Crate => quote! {
Expand Down
2 changes: 1 addition & 1 deletion typify-impl/src/type_entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,7 +1536,7 @@ impl TypeEntry {
static PATTERN: ::std::sync::LazyLock<::regress::Regex> = ::std::sync::LazyLock::new(|| {
::regress::Regex::new(#p).unwrap()
});
if (&*PATTERN).find(value).is_none() {
if PATTERN.find(value).is_none() {
return Err(#err.into());
}
}
Expand Down
Loading