mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-07 05:22:00 +00:00
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 <w.bumiller@proxmox.com>
This commit is contained in:
parent
adcbf7cab8
commit
e2975598ad
@ -326,10 +326,10 @@ struct TableColumn {
|
|||||||
right_align: bool,
|
right_align: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_table<W: Write, I: Iterator<Item = &'static SchemaPropertyEntry>>(
|
fn format_table<W: Write>(
|
||||||
output: W,
|
output: W,
|
||||||
list: &mut Vec<Value>,
|
list: &mut Vec<Value>,
|
||||||
schema: &dyn ObjectSchemaType<PropertyIter = I>,
|
schema: &dyn ObjectSchemaType,
|
||||||
options: &TableFormatOptions,
|
options: &TableFormatOptions,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let properties_to_print = if options.column_config.is_empty() {
|
let properties_to_print = if options.column_config.is_empty() {
|
||||||
@ -580,10 +580,10 @@ fn render_table<W: Write>(
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn format_object<W: Write, I: Iterator<Item = &'static SchemaPropertyEntry>>(
|
fn format_object<W: Write>(
|
||||||
output: W,
|
output: W,
|
||||||
data: &Value,
|
data: &Value,
|
||||||
schema: &dyn ObjectSchemaType<PropertyIter = I>,
|
schema: &dyn ObjectSchemaType,
|
||||||
options: &TableFormatOptions,
|
options: &TableFormatOptions,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let properties_to_print = if options.column_config.is_empty() {
|
let properties_to_print = if options.column_config.is_empty() {
|
||||||
|
@ -308,15 +308,12 @@ pub fn dump_enum_properties(schema: &Schema) -> Result<String, Error> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Generate ReST Documentaion for object properties
|
/// Generate ReST Documentaion for object properties
|
||||||
pub fn dump_properties<I>(
|
pub fn dump_properties(
|
||||||
param: &dyn ObjectSchemaType<PropertyIter = I>,
|
param: &dyn ObjectSchemaType,
|
||||||
indent: &str,
|
indent: &str,
|
||||||
style: ParameterDisplayStyle,
|
style: ParameterDisplayStyle,
|
||||||
skip: &[&str],
|
skip: &[&str],
|
||||||
) -> String
|
) -> String {
|
||||||
where
|
|
||||||
I: Iterator<Item = &'static SchemaPropertyEntry>,
|
|
||||||
{
|
|
||||||
let mut res = String::new();
|
let mut res = String::new();
|
||||||
let next_indent = format!(" {}", indent);
|
let next_indent = format!(" {}", indent);
|
||||||
|
|
||||||
@ -539,7 +536,7 @@ pub fn dump_section_config(config: &SectionConfig) -> String {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if plugin_count > 1 {
|
if plugin_count > 1 {
|
||||||
let description = wrap_text("", "", &properties.description, 80);
|
let description = wrap_text("", "", properties.description(), 80);
|
||||||
res.push_str(&format!(
|
res.push_str(&format!(
|
||||||
"\n**Section type** \'``{}``\': {}\n\n",
|
"\n**Section type** \'``{}``\': {}\n\n",
|
||||||
name, description
|
name, description
|
||||||
|
@ -525,17 +525,13 @@ impl AllOfSchema {
|
|||||||
|
|
||||||
/// Beside [`ObjectSchema`] we also have an [`AllOfSchema`] which also represents objects.
|
/// Beside [`ObjectSchema`] we also have an [`AllOfSchema`] which also represents objects.
|
||||||
pub trait ObjectSchemaType {
|
pub trait ObjectSchemaType {
|
||||||
type PropertyIter: Iterator<Item = &'static SchemaPropertyEntry>;
|
|
||||||
|
|
||||||
fn description(&self) -> &'static str;
|
fn description(&self) -> &'static str;
|
||||||
fn lookup(&self, key: &str) -> Option<(bool, &Schema)>;
|
fn lookup(&self, key: &str) -> Option<(bool, &Schema)>;
|
||||||
fn properties(&self) -> Self::PropertyIter;
|
fn properties(&self) -> ObjectPropertyIterator;
|
||||||
fn additional_properties(&self) -> bool;
|
fn additional_properties(&self) -> bool;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectSchemaType for ObjectSchema {
|
impl ObjectSchemaType for ObjectSchema {
|
||||||
type PropertyIter = std::slice::Iter<'static, SchemaPropertyEntry>;
|
|
||||||
|
|
||||||
fn description(&self) -> &'static str {
|
fn description(&self) -> &'static str {
|
||||||
self.description
|
self.description
|
||||||
}
|
}
|
||||||
@ -544,8 +540,12 @@ impl ObjectSchemaType for ObjectSchema {
|
|||||||
ObjectSchema::lookup(self, key)
|
ObjectSchema::lookup(self, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn properties(&self) -> Self::PropertyIter {
|
fn properties(&self) -> ObjectPropertyIterator {
|
||||||
self.properties.iter()
|
ObjectPropertyIterator {
|
||||||
|
schemas: [].iter(),
|
||||||
|
properties: Some(self.properties.iter()),
|
||||||
|
nested: None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn additional_properties(&self) -> bool {
|
fn additional_properties(&self) -> bool {
|
||||||
@ -554,8 +554,6 @@ impl ObjectSchemaType for ObjectSchema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectSchemaType for AllOfSchema {
|
impl ObjectSchemaType for AllOfSchema {
|
||||||
type PropertyIter = AllOfProperties;
|
|
||||||
|
|
||||||
fn description(&self) -> &'static str {
|
fn description(&self) -> &'static str {
|
||||||
self.description
|
self.description
|
||||||
}
|
}
|
||||||
@ -564,8 +562,8 @@ impl ObjectSchemaType for AllOfSchema {
|
|||||||
AllOfSchema::lookup(self, key)
|
AllOfSchema::lookup(self, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn properties(&self) -> Self::PropertyIter {
|
fn properties(&self) -> ObjectPropertyIterator {
|
||||||
AllOfProperties {
|
ObjectPropertyIterator {
|
||||||
schemas: self.list.iter(),
|
schemas: self.list.iter(),
|
||||||
properties: None,
|
properties: None,
|
||||||
nested: None,
|
nested: None,
|
||||||
@ -578,13 +576,13 @@ impl ObjectSchemaType for AllOfSchema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct AllOfProperties {
|
pub struct ObjectPropertyIterator {
|
||||||
schemas: std::slice::Iter<'static, &'static Schema>,
|
schemas: std::slice::Iter<'static, &'static Schema>,
|
||||||
properties: Option<std::slice::Iter<'static, SchemaPropertyEntry>>,
|
properties: Option<std::slice::Iter<'static, SchemaPropertyEntry>>,
|
||||||
nested: Option<Box<AllOfProperties>>,
|
nested: Option<Box<ObjectPropertyIterator>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Iterator for AllOfProperties {
|
impl Iterator for ObjectPropertyIterator {
|
||||||
type Item = &'static SchemaPropertyEntry;
|
type Item = &'static SchemaPropertyEntry;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<&'static SchemaPropertyEntry> {
|
fn next(&mut self) -> Option<&'static SchemaPropertyEntry> {
|
||||||
@ -598,7 +596,7 @@ impl Iterator for AllOfProperties {
|
|||||||
Some(item) => return Some(item),
|
Some(item) => return Some(item),
|
||||||
None => match self.schemas.next()? {
|
None => match self.schemas.next()? {
|
||||||
Schema::AllOf(o) => self.nested = Some(Box::new(o.properties())),
|
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;
|
self.properties = None;
|
||||||
continue;
|
continue;
|
||||||
@ -781,8 +779,6 @@ pub enum ParameterSchema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl ObjectSchemaType for ParameterSchema {
|
impl ObjectSchemaType for ParameterSchema {
|
||||||
type PropertyIter = Box<dyn Iterator<Item = &'static SchemaPropertyEntry>>;
|
|
||||||
|
|
||||||
fn description(&self) -> &'static str {
|
fn description(&self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
ParameterSchema::Object(o) => o.description(),
|
ParameterSchema::Object(o) => o.description(),
|
||||||
@ -797,10 +793,10 @@ impl ObjectSchemaType for ParameterSchema {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn properties(&self) -> Self::PropertyIter {
|
fn properties(&self) -> ObjectPropertyIterator {
|
||||||
match self {
|
match self {
|
||||||
ParameterSchema::Object(o) => Box::new(o.properties()),
|
ParameterSchema::Object(o) => o.properties(),
|
||||||
ParameterSchema::AllOf(o) => Box::new(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`.
|
/// Verify JSON value using an `ObjectSchema`.
|
||||||
pub fn verify_json_object<I>(
|
pub fn verify_json_object(
|
||||||
data: &Value,
|
data: &Value,
|
||||||
schema: &dyn ObjectSchemaType<PropertyIter = I>,
|
schema: &dyn ObjectSchemaType,
|
||||||
) -> Result<(), Error>
|
) -> Result<(), Error> {
|
||||||
where
|
|
||||||
I: Iterator<Item = &'static SchemaPropertyEntry>,
|
|
||||||
{
|
|
||||||
let map = match data {
|
let map = match data {
|
||||||
Value::Object(ref map) => map,
|
Value::Object(ref map) => map,
|
||||||
Value::Array(_) => bail!("Expected object - got array."),
|
Value::Array(_) => bail!("Expected object - got array."),
|
||||||
|
Loading…
Reference in New Issue
Block a user