cooplan_definitions_lib/
validated_source_category.rs1use crate::source_category::SourceCategory;
2use serde::{Deserialize, Serialize};
3
4use crate::validated_source_attribute::ValidatedSourceAttribute;
5
6#[derive(Serialize, Deserialize, Debug, Clone)]
8pub struct ValidatedSourceCategory {
9 pub id: String,
10 pub parent: Option<String>,
11 pub parent_name: Option<String>,
12 pub name: String,
13 pub selectable_as_last: bool,
14 pub attributes: Vec<ValidatedSourceAttribute>,
15}
16
17impl TryFrom<SourceCategory> for ValidatedSourceCategory {
18 type Error = String;
19
20 fn try_from(value: SourceCategory) -> Result<Self, Self::Error> {
21 match &value.id {
22 Some(id) => match ValidatedSourceAttribute::validate_attributes(value.attributes) {
23 Ok(validated_attributes) => Ok(ValidatedSourceCategory {
24 id: id.clone(),
25 parent: value.parent,
26 parent_name: value.parent_name,
27 name: value.name,
28 selectable_as_last: value.selectable_as_last.unwrap_or(false),
29 attributes: validated_attributes,
30 }),
31 Err(error) => Err(String::from(format!(
32 "failed to validate attributes: {}",
33 error
34 ))),
35 },
36 None => Err(String::from(format!(
37 "source category '{}' has no id",
38 value.name
39 ))),
40 }
41 }
42}
43
44impl PartialEq for ValidatedSourceCategory {
45 fn eq(&self, other: &Self) -> bool {
46 if !(self.id == other.id
47 && self.parent == other.parent
48 && self.parent_name == other.parent_name
49 && self.name == other.name
50 && self.selectable_as_last == other.selectable_as_last)
51 {
52 return false;
53 }
54
55 if self.attributes.len() != other.attributes.len() {
56 return false;
57 }
58
59 for i in 0..self.attributes.len() {
60 if self.attributes[i] != other.attributes[i] {
61 return false;
62 }
63 }
64
65 true
66 }
67}