From e2975598ad2834f7562dfa0d71c1316e8bd9e1e6 Mon Sep 17 00:00:00 2001 From: Wolfgang Bumiller Date: Wed, 24 Feb 2021 14:43:15 +0100 Subject: [PATCH] get rid of associated type in ObjectSchemaType trait The AllOfProperties Iterator is capable of also being the Iterator for the regular ObjectSchemas by simply filling it like it was the "last" object type in an AllOf. This makes the ObjectSchemaType trait more usable later. Signed-off-by: Wolfgang Bumiller --- proxmox/src/api/cli/text_table.rs | 8 +++--- proxmox/src/api/format.rs | 11 +++----- proxmox/src/api/schema.rs | 45 +++++++++++++------------------ 3 files changed, 27 insertions(+), 37 deletions(-) diff --git a/proxmox/src/api/cli/text_table.rs b/proxmox/src/api/cli/text_table.rs index 131e6673..5ff2db09 100644 --- a/proxmox/src/api/cli/text_table.rs +++ b/proxmox/src/api/cli/text_table.rs @@ -326,10 +326,10 @@ struct TableColumn { right_align: bool, } -fn format_table>( +fn format_table( output: W, list: &mut Vec, - schema: &dyn ObjectSchemaType, + schema: &dyn ObjectSchemaType, options: &TableFormatOptions, ) -> Result<(), Error> { let properties_to_print = if options.column_config.is_empty() { @@ -580,10 +580,10 @@ fn render_table( Ok(()) } -fn format_object>( +fn format_object( output: W, data: &Value, - schema: &dyn ObjectSchemaType, + schema: &dyn ObjectSchemaType, options: &TableFormatOptions, ) -> Result<(), Error> { let properties_to_print = if options.column_config.is_empty() { diff --git a/proxmox/src/api/format.rs b/proxmox/src/api/format.rs index 523979dd..7f16e934 100644 --- a/proxmox/src/api/format.rs +++ b/proxmox/src/api/format.rs @@ -308,15 +308,12 @@ pub fn dump_enum_properties(schema: &Schema) -> Result { } /// Generate ReST Documentaion for object properties -pub fn dump_properties( - param: &dyn ObjectSchemaType, +pub fn dump_properties( + param: &dyn ObjectSchemaType, indent: &str, style: ParameterDisplayStyle, skip: &[&str], -) -> String -where - I: Iterator, -{ +) -> String { let mut res = String::new(); let next_indent = format!(" {}", indent); @@ -539,7 +536,7 @@ pub fn dump_section_config(config: &SectionConfig) -> String { }; if plugin_count > 1 { - let description = wrap_text("", "", &properties.description, 80); + let description = wrap_text("", "", properties.description(), 80); res.push_str(&format!( "\n**Section type** \'``{}``\': {}\n\n", name, description diff --git a/proxmox/src/api/schema.rs b/proxmox/src/api/schema.rs index 86fddc68..142f68c0 100644 --- a/proxmox/src/api/schema.rs +++ b/proxmox/src/api/schema.rs @@ -525,17 +525,13 @@ impl AllOfSchema { /// Beside [`ObjectSchema`] we also have an [`AllOfSchema`] which also represents objects. pub trait ObjectSchemaType { - type PropertyIter: Iterator; - fn description(&self) -> &'static str; fn lookup(&self, key: &str) -> Option<(bool, &Schema)>; - fn properties(&self) -> Self::PropertyIter; + fn properties(&self) -> ObjectPropertyIterator; fn additional_properties(&self) -> bool; } impl ObjectSchemaType for ObjectSchema { - type PropertyIter = std::slice::Iter<'static, SchemaPropertyEntry>; - fn description(&self) -> &'static str { self.description } @@ -544,8 +540,12 @@ impl ObjectSchemaType for ObjectSchema { ObjectSchema::lookup(self, key) } - fn properties(&self) -> Self::PropertyIter { - self.properties.iter() + fn properties(&self) -> ObjectPropertyIterator { + ObjectPropertyIterator { + schemas: [].iter(), + properties: Some(self.properties.iter()), + nested: None, + } } fn additional_properties(&self) -> bool { @@ -554,8 +554,6 @@ impl ObjectSchemaType for ObjectSchema { } impl ObjectSchemaType for AllOfSchema { - type PropertyIter = AllOfProperties; - fn description(&self) -> &'static str { self.description } @@ -564,8 +562,8 @@ impl ObjectSchemaType for AllOfSchema { AllOfSchema::lookup(self, key) } - fn properties(&self) -> Self::PropertyIter { - AllOfProperties { + fn properties(&self) -> ObjectPropertyIterator { + ObjectPropertyIterator { schemas: self.list.iter(), properties: None, nested: None, @@ -578,13 +576,13 @@ impl ObjectSchemaType for AllOfSchema { } #[doc(hidden)] -pub struct AllOfProperties { +pub struct ObjectPropertyIterator { schemas: std::slice::Iter<'static, &'static Schema>, properties: Option>, - nested: Option>, + nested: Option>, } -impl Iterator for AllOfProperties { +impl Iterator for ObjectPropertyIterator { type Item = &'static SchemaPropertyEntry; fn next(&mut self) -> Option<&'static SchemaPropertyEntry> { @@ -598,7 +596,7 @@ impl Iterator for AllOfProperties { Some(item) => return Some(item), None => match self.schemas.next()? { Schema::AllOf(o) => self.nested = Some(Box::new(o.properties())), - Schema::Object(o) => self.properties = Some(o.properties()), + Schema::Object(o) => self.properties = Some(o.properties.iter()), _ => { self.properties = None; continue; @@ -781,8 +779,6 @@ pub enum ParameterSchema { } impl ObjectSchemaType for ParameterSchema { - type PropertyIter = Box>; - fn description(&self) -> &'static str { match self { ParameterSchema::Object(o) => o.description(), @@ -797,10 +793,10 @@ impl ObjectSchemaType for ParameterSchema { } } - fn properties(&self) -> Self::PropertyIter { + fn properties(&self) -> ObjectPropertyIterator { match self { - ParameterSchema::Object(o) => Box::new(o.properties()), - ParameterSchema::AllOf(o) => Box::new(o.properties()), + ParameterSchema::Object(o) => o.properties(), + ParameterSchema::AllOf(o) => o.properties(), } } @@ -1106,13 +1102,10 @@ pub fn verify_json_array(data: &Value, schema: &ArraySchema) -> Result<(), Error } /// Verify JSON value using an `ObjectSchema`. -pub fn verify_json_object( +pub fn verify_json_object( data: &Value, - schema: &dyn ObjectSchemaType, -) -> Result<(), Error> -where - I: Iterator, -{ + schema: &dyn ObjectSchemaType, +) -> Result<(), Error> { let map = match data { Value::Object(ref map) => map, Value::Array(_) => bail!("Expected object - got array."),