summaryrefslogtreecommitdiffstats
path: root/generator
diff options
context:
space:
mode:
authorCyborus <cyborus@cyborus.xyz>2024-03-15 19:28:28 +0100
committerCyborus <cyborus@cyborus.xyz>2024-03-15 19:28:28 +0100
commit5431303ecb3be284282ad2df5862cbfc752ba4c2 (patch)
treebbfe1614d19a2b0a5906d64d610c90a9c2a81784 /generator
parentsplit generation into method and struct files (diff)
downloadforgejo-api-5431303ecb3be284282ad2df5862cbfc752ba4c2.tar.xz
forgejo-api-5431303ecb3be284282ad2df5862cbfc752ba4c2.zip
don't implement `as_str` for enums that don't need it
Diffstat (limited to 'generator')
-rw-r--r--generator/src/main.rs2
-rw-r--r--generator/src/structs.rs27
2 files changed, 18 insertions, 11 deletions
diff --git a/generator/src/main.rs b/generator/src/main.rs
index 986c21f..ab23cc8 100644
--- a/generator/src/main.rs
+++ b/generator/src/main.rs
@@ -261,7 +261,7 @@ fn schema_subtypes(
..
} => {
let name = format!("{parent_name}{}", name.to_pascal_case());
- let subtype = structs::create_enum(&name, schema.description.as_deref(), _enum)?;
+ let subtype = structs::create_enum(&name, schema.description.as_deref(), _enum, false)?;
subtypes.push(subtype);
}
Schema {
diff --git a/generator/src/structs.rs b/generator/src/structs.rs
index 04d3039..9dc6fc7 100644
--- a/generator/src/structs.rs
+++ b/generator/src/structs.rs
@@ -44,7 +44,7 @@ pub fn create_struct_for_definition(
if schema._type == Some(SchemaType::One(Primitive::String)) {
if let Some(_enum) = &schema._enum {
- return create_enum(name, schema.description.as_deref(), _enum);
+ return create_enum(name, schema.description.as_deref(), _enum, false);
}
}
@@ -124,6 +124,7 @@ pub fn create_enum(
name: &str,
desc: Option<&str>,
_enum: &[serde_json::Value],
+ imp_as_str: bool,
) -> eyre::Result<String> {
let mut variants = String::new();
let mut imp = String::new();
@@ -143,20 +144,26 @@ pub fn create_enum(
}
imp.push_str("}");
- let out = format!(
+ let strukt = format!(
"
{docs}
#[derive(Debug, Clone, PartialEq, serde::Serialize, serde::Deserialize)]
pub enum {name} {{
{variants}
-}}
-
-impl {name} {{
- fn as_str(&self) -> &'static str {{
- {imp}
- }}
}}"
);
+ let out = if imp_as_str {
+ let imp = format!(
+ "\n\nimpl {name} {{
+ fn as_str(&self) -> &'static str {{
+ {imp}
+ }}
+ }}"
+ );
+ format!("{strukt} {imp}")
+ } else {
+ strukt
+ };
Ok(out)
}
@@ -241,7 +248,7 @@ fn create_query_struct(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String>
..
} => {
let name = format!("{op_name}{}", param.name.to_pascal_case());
- let enum_def = create_enum(&name, None, _enum)?;
+ let enum_def = create_enum(&name, None, _enum, true)?;
enums.push(enum_def);
name
}
@@ -256,7 +263,7 @@ fn create_query_struct(spec: &OpenApiV2, op: &Operation) -> eyre::Result<String>
..
} => {
let name = format!("{op_name}{}", param.name.to_pascal_case());
- let enum_def = create_enum(&name, None, _enum)?;
+ let enum_def = create_enum(&name, None, _enum, true)?;
enums.push(enum_def);
format!("Vec<{name}>")
}