feat(mavlink): adopt the kaptan-native dialect
This commit is contained in:
1 parent
1c90870634
commit
5f91465e66
87 files changed
+7733
-6850
No files matched your search
@@ -31,6 +31,7 @@ fn expand_scalars(
|
||||
let mut descriptors = Vec::new();
|
||||
let mut reads = Vec::new();
|
||||
let mut writes = Vec::new();
|
||||
let mut snapshot_widths = Vec::new();
|
||||
for field in &fields {
|
||||
let FieldMetadata {
|
||||
descriptor,
|
||||
@@ -42,6 +43,7 @@ fn expand_scalars(
|
||||
let ty = &field.ty;
|
||||
let field_ident = &field.ident;
|
||||
let (kind, read, decode, out_of_domain_rule) = if !field.enum_backed {
|
||||
snapshot_widths.push(quote! { <#ty as #crate_path::ConfigScalar>::KIND.width() });
|
||||
(
|
||||
quote! { <#ty as #crate_path::ConfigScalar>::KIND },
|
||||
quote! { #crate_path::ConfigScalar::into_config_value(self.#field_ident) },
|
||||
@@ -49,16 +51,13 @@ fn expand_scalars(
|
||||
quote! { #crate_path::ValidationRule::AboveMaximum },
|
||||
)
|
||||
} else {
|
||||
snapshot_widths.push(quote! { 1usize });
|
||||
(
|
||||
quote! { #crate_path::ValueKind::U32 },
|
||||
quote! {
|
||||
#crate_path::ConfigValue::U32(u32::from(self.#field_ident.wire_value()))
|
||||
},
|
||||
quote! { #crate_path::ValueKind::U8 },
|
||||
quote! { #crate_path::ConfigValue::U8(self.#field_ident.wire_value()) },
|
||||
quote! {
|
||||
match value {
|
||||
#crate_path::ConfigValue::U32(value) => u8::try_from(value)
|
||||
.ok()
|
||||
.and_then(<#ty>::try_from_wire)
|
||||
#crate_path::ConfigValue::U8(value) => <#ty>::try_from_wire(value)
|
||||
.ok_or(#crate_path::ScalarValueError::OutOfDomain),
|
||||
value => Err(#crate_path::ScalarValueError::WrongKind(value.kind())),
|
||||
}
|
||||
@@ -122,6 +121,7 @@ fn expand_scalars(
|
||||
|
||||
impl #impl_generics #crate_path::Config for #ident #type_generics #where_clause {
|
||||
const FIELD_COUNT: usize = Self::DESCRIPTORS.len();
|
||||
const SNAPSHOT_VALUE_BYTES: usize = 0 #(+ #snapshot_widths)*;
|
||||
const SCHEMA_ID: [u8; 16] =
|
||||
#crate_path::validation::__schema_id(Self::DESCRIPTORS);
|
||||
|
||||
@@ -168,6 +168,7 @@ fn expand_children(
|
||||
let mut reads = Vec::new();
|
||||
let mut writes = Vec::new();
|
||||
let mut counts = Vec::new();
|
||||
let mut snapshot_widths = Vec::new();
|
||||
let mut schema_ids = Vec::new();
|
||||
let mut descriptor_lookups = Vec::new();
|
||||
for field in &fields {
|
||||
@@ -194,6 +195,7 @@ fn expand_children(
|
||||
}
|
||||
});
|
||||
counts.push(quote! { <#ty as #crate_path::Config>::FIELD_COUNT });
|
||||
snapshot_widths.push(quote! { <#ty as #crate_path::Config>::SNAPSHOT_VALUE_BYTES });
|
||||
schema_ids.push(quote! { <#ty as #crate_path::Config>::SCHEMA_ID });
|
||||
descriptor_lookups.push(quote! {
|
||||
if index < <#ty as #crate_path::Config>::FIELD_COUNT {
|
||||
@@ -207,6 +209,7 @@ fn expand_children(
|
||||
Ok(quote! {
|
||||
impl #impl_generics #crate_path::Config for #ident #type_generics #where_clause {
|
||||
const FIELD_COUNT: usize = 0 #(+ #counts)*;
|
||||
const SNAPSHOT_VALUE_BYTES: usize = 0 #(+ #snapshot_widths)*;
|
||||
const SCHEMA_ID: [u8; 16] =
|
||||
#crate_path::validation::__combine_schema_ids(&[#(#schema_ids),*]);
|
||||
|
||||
@@ -282,7 +285,7 @@ fn expand_field(crate_path: &syn::Path, field: &crate::parse::FieldModel) -> Res
|
||||
)
|
||||
} else {
|
||||
(
|
||||
quote! { #crate_path::ValueKind::U32 },
|
||||
quote! { #crate_path::ValueKind::U8 },
|
||||
quote! { <#ty>::CONFIG_ENUM_OPTIONS },
|
||||
)
|
||||
};
|
||||
@@ -328,7 +331,9 @@ fn config_value_constructor_tokens(crate_path: &syn::Path, ty: &syn::Type) -> Re
|
||||
let value = match segment.ident.to_string().as_str() {
|
||||
"bool" => quote! { #crate_path::ConfigValue::Bool },
|
||||
"i32" => quote! { #crate_path::ConfigValue::I32 },
|
||||
"u8" | "u16" | "u32" => quote! { #crate_path::ConfigValue::U32 },
|
||||
"u8" => quote! { #crate_path::ConfigValue::U8 },
|
||||
"u16" => quote! { #crate_path::ConfigValue::U16 },
|
||||
"u32" => quote! { #crate_path::ConfigValue::U32 },
|
||||
"u64" => quote! { #crate_path::ConfigValue::U64 },
|
||||
"f32" => quote! { #crate_path::ConfigValue::F32 },
|
||||
"f64" => quote! { #crate_path::ConfigValue::F64 },
|
||||
|
||||
@@ -77,6 +77,8 @@ struct CompactSection {
|
||||
endpoint: u8,
|
||||
#[config(enum, id = 21, key = "compact.mode", unit = "mode")]
|
||||
mode: TestMode,
|
||||
#[config(id = 22, key = "compact.port", unit = "id", range(min = 1))]
|
||||
port: u16,
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -115,14 +117,16 @@ fn narrow_scalars_and_compact_enums_keep_native_types_and_generic_metadata() {
|
||||
let mut section = CompactSection {
|
||||
endpoint: 7,
|
||||
mode: TestMode::Quiet,
|
||||
port: 4861,
|
||||
};
|
||||
assert_eq!(CompactSection::SNAPSHOT_VALUE_BYTES, 1 + 1 + 2);
|
||||
assert_eq!(
|
||||
section.field_value(FieldId::new(20)),
|
||||
Some(ConfigValue::U32(7))
|
||||
Some(ConfigValue::U8(7))
|
||||
);
|
||||
assert_eq!(
|
||||
CompactSection::DESCRIPTORS[0].minimum,
|
||||
Some(ConfigValue::U32(1))
|
||||
Some(ConfigValue::U8(1))
|
||||
);
|
||||
assert_eq!(CompactSection::DESCRIPTORS[0].maximum, None);
|
||||
assert_eq!(
|
||||
@@ -134,19 +138,27 @@ fn narrow_scalars_and_compact_enums_keep_native_types_and_generic_metadata() {
|
||||
assert_eq!(TestMode::try_from_wire(2), None);
|
||||
|
||||
section
|
||||
.set_field(FieldId::new(21), ConfigValue::U32(3))
|
||||
.set_field(FieldId::new(21), ConfigValue::U8(3))
|
||||
.unwrap();
|
||||
assert_eq!(section.mode, TestMode::Loud);
|
||||
assert!(matches!(
|
||||
section.set_field(FieldId::new(21), ConfigValue::U32(2)),
|
||||
section.set_field(FieldId::new(21), ConfigValue::U8(2)),
|
||||
Err(FieldWriteError::Violation(issue))
|
||||
if issue.rule() == ValidationRule::InvalidEnumValue
|
||||
));
|
||||
assert!(matches!(
|
||||
section.set_field(FieldId::new(20), ConfigValue::U32(256)),
|
||||
section.set_field(FieldId::new(20), ConfigValue::U8(0)),
|
||||
Err(FieldWriteError::Violation(issue))
|
||||
if issue.rule() == ValidationRule::AboveMaximum
|
||||
if issue.rule() == ValidationRule::BelowMinimum
|
||||
));
|
||||
assert_eq!(
|
||||
section.field_value(FieldId::new(22)),
|
||||
Some(ConfigValue::U16(4861))
|
||||
);
|
||||
assert_eq!(
|
||||
CompactSection::DESCRIPTORS[2].kind,
|
||||
kaptan_config::ValueKind::U16
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -184,6 +196,7 @@ fn aggregate_routes_writes_and_defers_declared_post_validation() {
|
||||
|
||||
#[test]
|
||||
fn aggregate_derives_its_flattened_descriptor_order() {
|
||||
assert_eq!(Aggregate::SNAPSHOT_VALUE_BYTES, 4 * 4);
|
||||
let descriptors: Vec<_> = (0..Aggregate::FIELD_COUNT)
|
||||
.map(|index| Aggregate::descriptor(index).unwrap())
|
||||
.collect();
|
||||
|
||||
Reference in new issue
Block a user