use crate::types::basic::OSString;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[derive(Default)]
pub struct RoadNetwork {
#[serde(rename = "LogicFile", skip_serializing_if = "Option::is_none")]
pub logic_file: Option<LogicFile>,
#[serde(rename = "SceneGraphFile", skip_serializing_if = "Option::is_none")]
pub scene_graph_file: Option<SceneGraphFile>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LogicFile {
#[serde(rename = "@filepath")]
pub filepath: OSString,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SceneGraphFile {
#[serde(rename = "@filepath")]
pub filepath: OSString,
}
impl RoadNetwork {
pub fn new(logic_file: LogicFile) -> Self {
Self {
logic_file: Some(logic_file),
scene_graph_file: None,
}
}
pub fn from_logic_file_path(filepath: String) -> Self {
Self::new(LogicFile::new(filepath))
}
}
impl LogicFile {
pub fn new(filepath: String) -> Self {
Self {
filepath: OSString::literal(filepath),
}
}
}
impl SceneGraphFile {
pub fn new(filepath: String) -> Self {
Self {
filepath: OSString::literal(filepath),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_road_network_creation() {
let logic_file = LogicFile::new("./road_networks/test.xodr".to_string());
let road_network = RoadNetwork::new(logic_file);
assert!(road_network.logic_file.is_some());
assert_eq!(
road_network.logic_file.unwrap().filepath.as_literal(),
Some(&"./road_networks/test.xodr".to_string())
);
}
#[test]
fn test_road_network_from_path() {
let road_network = RoadNetwork::from_logic_file_path(
"./road_networks/alks_road_different_curvatures.xodr".to_string(),
);
assert!(road_network.logic_file.is_some());
assert_eq!(
road_network.logic_file.unwrap().filepath.as_literal(),
Some(&"./road_networks/alks_road_different_curvatures.xodr".to_string())
);
}
#[test]
fn test_logic_file_creation() {
let logic_file = LogicFile::new("test.xodr".to_string());
assert_eq!(
logic_file.filepath.as_literal(),
Some(&"test.xodr".to_string())
);
}
#[test]
fn test_scene_graph_file_creation() {
let scene_file = SceneGraphFile::new("test.osgb".to_string());
assert_eq!(
scene_file.filepath.as_literal(),
Some(&"test.osgb".to_string())
);
}
#[test]
fn test_road_network_serialization() {
let road_network = RoadNetwork::from_logic_file_path("test.xodr".to_string());
let xml = quick_xml::se::to_string(&road_network).unwrap();
assert!(xml.contains("RoadNetwork"));
assert!(xml.contains("LogicFile"));
assert!(xml.contains("filepath=\"test.xodr\""));
}
}