diff options
author | Cyborus <cyborus@noreply.codeberg.org> | 2024-08-05 19:10:36 +0200 |
---|---|---|
committer | Cyborus <cyborus@noreply.codeberg.org> | 2024-08-05 19:10:36 +0200 |
commit | 59d56d2f2fc07aa0d06f77202de20ec49b50335d (patch) | |
tree | c9177b304ca25f5ca250261926a4a1fd3cb960ba /generator/src/structs.rs | |
parent | Merge pull request 'replace instances of "gitea" with "Forgejo"' (#71) from r... (diff) | |
parent | chore: generate (diff) | |
download | forgejo-api-59d56d2f2fc07aa0d06f77202de20ec49b50335d.tar.xz forgejo-api-59d56d2f2fc07aa0d06f77202de20ec49b50335d.zip |
Merge pull request 'set parsing by format field' (#72) from parse-by-format into main
Reviewed-on: https://codeberg.org/Cyborus/forgejo-api/pulls/72
Diffstat (limited to 'generator/src/structs.rs')
-rw-r--r-- | generator/src/structs.rs | 66 |
1 files changed, 44 insertions, 22 deletions
diff --git a/generator/src/structs.rs b/generator/src/structs.rs index 4543f5c..428bb1d 100644 --- a/generator/src/structs.rs +++ b/generator/src/structs.rs @@ -70,33 +70,50 @@ pub fn create_struct_for_definition( crate::schema_subtype_name(spec, name, prop_name, value, &mut field_ty)?; crate::schema_subtypes(spec, name, prop_name, value, &mut subtypes)?; } - if field_name.ends_with("url") && field_ty == "String" { - field_ty = "url::Url".into() + let is_required = required.contains(prop_name); + if let Some(format) = &prop_schema.deref(spec)?.format { + match &**format { + "url" => { + if !is_required { + fields.push_str( + "#[serde(deserialize_with = \"crate::none_if_blank_url\")]\n", + ); + } + } + "ssh-url" => { + if is_required { + fields.push_str( + "#[serde(deserialize_with = \"crate::deserialize_ssh_url\")]\n", + ); + } else { + fields.push_str( + "#[serde(deserialize_with = \"crate::deserialize_optional_ssh_url\")]\n", + ); + } + } + "date-time" => { + if is_required { + fields.push_str("#[serde(with = \"time::serde::rfc3339\")]\n"); + } else { + fields.push_str("#[serde(with = \"time::serde::rfc3339::option\")]\n"); + } + } + "date" => { + if is_required { + fields.push_str("#[serde(with = \"time::serde::rfc3339\")]\n"); + } else { + fields.push_str("#[serde(with = \"time::serde::rfc3339::option\")]\n"); + } + } + _ => (), + } } if field_ty == name { field_ty = format!("Box<{field_ty}>") } - if !required.contains(prop_name) { + if !is_required { field_ty = format!("Option<{field_ty}>") } - if field_ty == "Option<url::Url>" { - if field_name == "ssh_url" { - fields.push_str( - "#[serde(deserialize_with = \"crate::deserialize_optional_ssh_url\")]\n", - ); - } else { - fields.push_str("#[serde(deserialize_with = \"crate::none_if_blank_url\")]\n"); - } - } - if field_ty == "url::Url" && field_name == "ssh_url" { - fields.push_str("#[serde(deserialize_with = \"crate::deserialize_ssh_url\")]\n"); - } - if field_ty == "time::OffsetDateTime" { - fields.push_str("#[serde(with = \"time::serde::rfc3339\")]\n"); - } - if field_ty == "Option<time::OffsetDateTime>" { - fields.push_str("#[serde(with = \"time::serde::rfc3339::option\")]\n"); - } if let Some(parse_method) = parse_with.get(prop_name) { fields.push_str("#[serde(deserialize_with = \"crate::"); fields.push_str(parse_method); @@ -539,7 +556,12 @@ fn create_header_struct( ) .unwrap(); match &header._type { - ParameterType::String => imp.push_str("Ok(s.to_string())"), + ParameterType::String => match header.format.as_deref() { + Some("url" | "ssh-url") => imp.push_str( + "s.parse::<url::Url>().map_err(|_| StructureError::HeaderParseFailed)", + ), + _ => imp.push_str("Ok(s.to_string())"), + }, ParameterType::Number => match header.format.as_deref() { Some("float") => { imp.push_str("s.parse::<f32>().map_err(|_| StructureError::HeaderParseFailed)") |