use argentum_rest_infrastructure::data_type::DeserializableSchemaRaw; use argentum_rest_infrastructure::data_type::SerializableBody; {{#if properties}} use argentum_standard_business::invariant_violation::{ InvariantResult, ViolationItem, ViolationObject, Violations, }; use std::collections::BTreeMap; {{else}} use argentum_standard_business::invariant_violation::InvariantResult; {{/if}} {{# each dependencies as |dep|}} use {{dep}}; {{/each}} #[derive(Debug, Clone, PartialEq, serde::Serialize)] pub struct {{ name }} { {{# each properties as |prop| }} pub {{ snake prop.name }}: {{{ prop.dataType }}}, {{/each}} } impl {{ name }} { pub fn new( {{# each properties as |prop| }} {{ snake prop.name }}: {{{ prop.dataType }}}, {{/each}} ) -> Self { Self { {{# each properties as |prop| }} {{ snake prop.name }}, {{/each}} } } } impl SerializableBody for {{ name }} {} impl DeserializableSchemaRaw<'_> for {{ name }} { type Raw = {{ name }}Raw; {{#if properties}} fn try_from_raw(raw: Self::Raw) -> InvariantResult { let mut argentum_violations: ViolationObject = BTreeMap::new(); {{# each properties as |prop| }} {{#if prop.isRef}} {{#if prop.required}} let name = if raw.name.is_none() { argentum_violations.insert( "name".into(), Violations::new(vec!["required field".to_string()], None), ); None } else { match UserName::try_from_raw(raw.name.unwrap()) { Ok(value) => Some(value), Err(v) => { argentum_violations.insert("name".into(), v); None } } }; {{else}} let name = match UserName::try_from_raw(raw.name.unwrap()) { Ok(value) => Some(value), Err(v) => { argentum_violations.insert("name".into(), v); None } } {{/if}} {{else}} let {{ snake prop.name }} = raw.{{ snake prop.name }}; {{# if prop.required }} if {{ snake prop.name }}.is_none() { argentum_violations.insert( "{{ snake prop.name }}".into(), Violations::new(vec!["field is required".to_string()], None), ); } {{/if}} {{/if}} {{/each}} if argentum_violations.is_empty() { Ok(Self::new( {{# each properties as |prop| }} {{ snake prop.name }}{{# if prop.required }}.unwrap(){{/if}}, {{/each}} )) } else { Err(Violations::new( vec!["wrong data for {{ name }}".to_string()], Some(ViolationItem::Object(argentum_violations)), )) } } {{else}} fn try_from_raw(_: Self::Raw) -> InvariantResult { Ok(Self::new()) } {{/if}} } #[derive(serde::Deserialize)] pub struct {{ name }}Raw { {{# each properties as |prop| }} #[serde(rename = "{{ prop.rename }}")] pub {{ snake prop.name }}: {{{ prop.rawType }}}, {{/each}} }