Skip to content

Commit

Permalink
fix unit structs and deal with serde(with=serde_bytes)
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Dec 11, 2024
1 parent 9128d8a commit bd3c953
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 9 deletions.
80 changes: 77 additions & 3 deletions crux_cli/fixtures-counter-actual.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,89 @@
}
}
},
"ServerSentEvents": {
"HttpError": {
"ENUM": {
"0": {
"Url": {
"NEWTYPE": "STR"
}
},
"1": {
"Io": {
"NEWTYPE": "STR"
}
},
"2": {
"Timeout": "UNIT"
}
}
},
"HttpHeader": {
"STRUCT": [
{
"name": "STR"
},
{
"value": "STR"
}
]
},
"HttpRequest": {
"STRUCT": [
{
"method": "STR"
},
{
"url": "STR"
},
{
"headers": {
"SEQ": {
"TYPENAME": "HttpHeader"
}
}
},
{
"body": "BYTES"
}
]
},
"HttpResponse": {
"STRUCT": [
{
"context": {
"TYPENAME": "crux_core::capability::CapabilityContext"
"status": "U16"
},
{
"headers": {
"SEQ": {
"TYPENAME": "HttpHeader"
}
}
},
{
"body": "BYTES"
}
]
},
"HttpResult": {
"ENUM": {
"0": {
"Ok": {
"NEWTYPE": {
"TYPENAME": "HttpResponse"
}
}
},
"1": {
"Err": {
"NEWTYPE": {
"TYPENAME": "crate::HttpError"
}
}
}
}
},
"RenderOperation": "UNITSTRUCT",
"SseRequest": {
"STRUCT": [
{
Expand Down
8 changes: 6 additions & 2 deletions crux_cli/src/codegen/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,15 @@ ascent! {
// set of all the edges we are interested in
relation edge(ItemNode, ItemNode);

// root fields
// roots that are unit structs
edge(root, root) <--
root(root),
is_struct(root);
// roots that have fields
edge(root, field) <--
root(root),
field(root, field);
// root variants
// roots that have variants
edge(root, variant) <--
root(root),
variant(root, variant);
Expand Down
13 changes: 12 additions & 1 deletion crux_cli/src/codegen/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,18 @@ fn make_format(field: &ItemNode, all_fields: &Vec<ItemNode>) -> Option<Indexed<F
match &field.0.inner {
ItemEnum::StructField(type_) => Some(Indexed {
index: index as u32,
value: type_.into(),
value: {
if let Some((_whole, serde_with)) = &field.0.attrs.iter().find_map(|attr| {
lazy_regex::regex_captures!(r#"\[serde\(with\s*=\s*"(\w+)"\)\]"#, attr)
}) {
match *serde_with {
"serde_bytes" => Format::Bytes, // e.g. HttpRequest.body, HttpResponse.body
_ => todo!(),
}
} else {
type_.into()
}
},
}),
_ => None,
}
Expand Down
7 changes: 4 additions & 3 deletions crux_cli/src/codegen/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,12 @@ impl ItemNode {
}

pub fn is_subset(&self) -> bool {
self.is_struct()
// from most likely to least likely
self.is_impl()
|| self.is_struct_field()
|| self.is_enum()
|| self.is_enum_variant()
|| self.is_impl()
|| self.is_struct()
|| self.is_enum()
|| self.is_associated_type()
}

Expand Down

0 comments on commit bd3c953

Please sign in to comment.