Toromyx / schema_org_types_rs

Schema.org schemas as Rust types
https://docs.rs/schema_org_types
2 stars 1 forks source link

Provide and implement traits for inherited class properties #9

Closed Toromyx closed 9 months ago

Toromyx commented 9 months ago

Most class schema properties are inherited from other class schemas. Implementing common functionalities would be easier if this crate would provide a trait for each class schema and implement that trait for every struct that inherits properties from that class schema.

For example, the corresponding code for Thing would look like this:

trait ThingTrait {
    fn get_additional_type(&self) -> &[AdditionalTypeProperty];
    fn take_additional_type(&mut self) -> VecAdditionalTypeProperty>;
    fn get_alternate_name(&self) -> &[AlternateNameProperty];
    fn take_alternate_name(&mut self) -> Vec<AlternateNameProperty>;
    // ...
}

impl ThingTrait for Thing {
    fn get_additional_type(&self) -> &[AdditionalTypeProperty] {
        self.additional_type.as_slice()
    }
    fn take_additional_type(&mut self) -> VecAdditionalTypeProperty> {
        std::mem::take(&mut self.additional_type)
    }
    fn get_alternate_name(&self) -> &[AlternateNameProperty] {
        self.alternate_name.as_slice()
    }
    fn take_alternate_name(&mut self) -> Vec<AlternateNameProperty> {
        std::mem::take(&mut self.alternate_name)
    }
    // ...
}