Skip to content

Commit

Permalink
used algebraic data types representation for Node
Browse files Browse the repository at this point in the history
  • Loading branch information
ZibanPirate committed Jan 7, 2024
1 parent 0479726 commit ffe3fbc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 51 deletions.
39 changes: 9 additions & 30 deletions rust/src/api/get_node_by_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ mod test {
fr: "Université Mohamed Khider Biskra".to_string(),
},
r#type: NodeType::University,
terms: None,
},
),
TestCase::new(
Expand All @@ -56,7 +55,6 @@ mod test {
fr: "Faculté des Sciences et de la Technologie".to_string(),
},
r#type: NodeType::Faculty,
terms: None,
},
),
TestCase::new(
Expand All @@ -67,11 +65,12 @@ mod test {
en: "Specialy of Electrical Control".to_string(),
fr: "Spécialité de commande électrique".to_string(),
},
r#type: NodeType::Specialty,
terms: Some(NodeTerms {
per_year: 2,
slots: vec![7, 8, 9, 10],
}),
r#type: NodeType::Specialty {
terms: NodeTerms {
per_year: 2,
slots: vec![7, 8, 9, 10],
},
},
},
),
];
Expand All @@ -90,29 +89,9 @@ mod test {

fn assert_node(expected: &Node, actual: &Node) {
assert_eq!(
expected.name.ar, actual.name.ar,
"Expected ar name to be '{}', but got '{}'",
expected.name.ar, actual.name.ar
);
assert_eq!(
expected.name.en, actual.name.en,
"Expected en name to be '{}', but got '{}'",
expected.name.en, actual.name.en
);
assert_eq!(
expected.name.fr, actual.name.fr,
"Expected fr name to be '{}', but got '{}'",
expected.name.fr, actual.name.fr
);
assert_eq!(
expected.r#type, actual.r#type,
"Expected ty to be '{:?}', but got '{:?}'",
expected.r#type, actual.r#type
expected, actual,
"Expected node to be '{}', but got '{}'",
expected, actual
);
assert_eq!(
expected.terms, actual.terms,
"Expeted terms to be '{:?}', but got '{:?}'",
expected.terms, actual.terms
)
}
}
30 changes: 9 additions & 21 deletions rust/src/node/model.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use serde::{Deserialize, Serialize};
use serde_json::json;

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct NodeName {
pub ar: String,
pub en: String,
Expand All @@ -16,6 +16,7 @@ impl std::fmt::Display for NodeName {
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(tag = "type")] // to flatten the enum to the parent struct
pub enum NodeType {
#[serde(rename = "UNIVERSITY")]
University,
Expand All @@ -30,9 +31,9 @@ pub enum NodeType {
#[serde(rename = "DEPARTMENT")]
Department,
#[serde(rename = "SPECIALTY")]
Specialty,
Specialty { terms: NodeTerms },
#[serde(rename = "SECTOR")]
Sector,
Sector { terms: NodeTerms },
}

impl std::fmt::Display for NodeType {
Expand All @@ -44,8 +45,8 @@ impl std::fmt::Display for NodeType {
NodeType::Institute => write!(f, "INSTITUTE"),
NodeType::Faculty => write!(f, "FACULTY"),
NodeType::Department => write!(f, "DEPARTMENT"),
NodeType::Specialty => write!(f, "SPECIALTY"),
NodeType::Sector => write!(f, "SECTOR"),
NodeType::Specialty { .. } => write!(f, "SPECIALTY"),
NodeType::Sector { .. } => write!(f, "SECTOR"),
}
}
}
Expand All @@ -64,28 +65,15 @@ impl std::fmt::Display for NodeTerms {
}
}

#[derive(Debug, Serialize, Deserialize)]
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Node {
pub name: NodeName,
#[serde(flatten)]
pub r#type: NodeType,
pub terms: Option<NodeTerms>,
}

impl std::fmt::Display for Node {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.terms.is_some() {
let obj = json!({
"name": self.name,
"type": self.r#type,
"terms": self.terms.clone().unwrap()
});
return write!(f, "{}", serde_json::to_string_pretty(&obj).unwrap());
} else {
let obj = json!({
"name": self.name,
"type": self.r#type
});
return write!(f, "{}", serde_json::to_string_pretty(&obj).unwrap());
}
write!(f, "{}", serde_json::to_string_pretty(&self).unwrap())
}
}

0 comments on commit ffe3fbc

Please sign in to comment.