Skip to content

Commit 77d4b4f

Browse files
fix macros
1 parent a973620 commit 77d4b4f

File tree

1 file changed

+54
-56
lines changed

1 file changed

+54
-56
lines changed

crates/djls-ipc/src/messages/macros.rs

Lines changed: 54 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -34,66 +34,64 @@ macro_rules! define_messages {
3434
}
3535
)+
3636

37-
pub fn all_message_schemas() -> Result<(String, Vec<(String, schemars::schema::RootSchema)>), SchemaError> {
38-
let mut combined_schema = schemars::schema::RootSchema {
39-
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_string()),
40-
schema: schemars::schema::Schema::Object(schemars::schema::SchemaObject {
41-
reference: Some("#/definitions/Messages".to_string()), // Or another type
42-
..Default::default()
43-
}).into(),
44-
definitions: std::collections::BTreeMap::new(),
45-
};
46-
47-
// Add our core types with proper schema definitions
48-
combined_schema.definitions.insert(
49-
"ErrorResponse".to_string(),
50-
schemars::schema::Schema::Object(schemars::schema_for!(ErrorResponse).schema)
51-
);
52-
combined_schema.definitions.insert(
53-
"Messages".to_string(),
54-
schemars::schema::Schema::Object(schemars::schema_for!(Messages).schema)
55-
);
56-
combined_schema.definitions.insert(
57-
"Request".to_string(),
58-
schemars::schema::Schema::Object(schemars::schema_for!(GenericRequest).schema)
59-
);
60-
combined_schema.definitions.insert(
61-
"Response".to_string(),
62-
schemars::schema::Schema::Object(schemars::schema_for!(GenericResponse).schema)
63-
);
37+
pub fn all_message_schemas() -> Result<(String, Vec<(String, schemars::schema::RootSchema)>), SchemaError> {
38+
let mut combined_schema = schemars::schema::RootSchema {
39+
meta_schema: Some("http://json-schema.org/draft-07/schema#".to_string()),
40+
schema: schemars::schema::Schema::Object(schemars::schema::SchemaObject {
41+
reference: Some("#/definitions/Messages".to_string()),
42+
..Default::default()
43+
}).into(),
44+
definitions: std::collections::BTreeMap::new(),
45+
};
6446

65-
// Add message-specific schemas
66-
$(
67-
paste::paste! {
68-
let name = stringify!($variant)
69-
.chars()
70-
.enumerate()
71-
.fold(String::new(), |mut acc, (i, c)| {
72-
if i > 0 && c.is_uppercase() {
73-
acc.push('_');
74-
}
75-
acc.extend(c.to_lowercase());
76-
acc
77-
});
78-
79-
let message_schema = schemars::schema_for!([<$variant Message>]);
80-
combined_schema.definitions.extend(message_schema.definitions); // Include nested definitions
8147
combined_schema.definitions.insert(
82-
format!("{}_message", name),
83-
schemars::schema::Schema::Object(message_schema.schema)
48+
"ErrorResponse".to_string(),
49+
schemars::schema::Schema::Object(schemars::schema_for!(ErrorResponse).schema)
50+
);
51+
combined_schema.definitions.insert(
52+
"Messages".to_string(),
53+
schemars::schema::Schema::Object(schemars::schema_for!(Messages).schema)
54+
);
55+
combined_schema.definitions.insert(
56+
"Request".to_string(),
57+
schemars::schema::Schema::Object(schemars::schema_for!(GenericRequest).schema)
58+
);
59+
combined_schema.definitions.insert(
60+
"Response".to_string(),
61+
schemars::schema::Schema::Object(schemars::schema_for!(GenericResponse).schema)
8462
);
85-
}
86-
)+
8763

88-
// Set the root schema to be an object with all our definitions
89-
combined_schema.schema = schemars::schema::Schema::Object(schemars::schema::SchemaObject {
90-
// Maybe specify the root object structure here
91-
..Default::default()
92-
}).into();
64+
$(
65+
paste::paste! {
66+
let name = stringify!($variant)
67+
.chars()
68+
.enumerate()
69+
.fold(String::new(), |mut acc, (i, c)| {
70+
if i > 0 && c.is_uppercase() {
71+
acc.push('_');
72+
}
73+
acc.extend(c.to_lowercase());
74+
acc
75+
});
9376

94-
Ok((file!().to_string(), vec![
95-
("schema".to_string(), combined_schema)
96-
]))
97-
}
77+
let message_schema = schemars::schema_for!([<$variant Message>]);
78+
combined_schema.definitions.extend(message_schema.definitions);
79+
combined_schema.definitions.insert(
80+
format!("{}_message", name),
81+
schemars::schema::Schema::Object(message_schema.schema)
82+
);
83+
}
84+
)+
85+
86+
// Set the root schema to be an object with all our definitions
87+
// This gets rid of the random Model/RootModel from appearing
88+
combined_schema.schema = schemars::schema::Schema::Object(schemars::schema::SchemaObject {
89+
..Default::default()
90+
}).into();
91+
92+
Ok((file!().to_string(), vec![
93+
("schema".to_string(), combined_schema)
94+
]))
95+
}
9896
};
9997
}

0 commit comments

Comments
 (0)