use crate::junction::crg_mode::CrgMode;
use crate::junction::crg_purpose::CrgPurpose;
use crate::object::orientation::Orientation;
use std::borrow::Cow;
use uom::si::angle::radian;
use uom::si::f64::Angle;
use uom::si::f64::Length;
use uom::si::length::meter;
#[derive(Debug, Clone, PartialEq)]
pub struct Crg {
pub file: String,
pub h_offset: Option<Angle>,
pub mode: CrgMode,
pub orientation: Orientation,
pub purpose: Option<CrgPurpose>,
pub s_end: Length,
pub s_offset: Option<Length>,
pub s_start: Length,
pub t_offset: Option<Length>,
pub z_offset: Option<Length>,
pub z_scale: Option<f64>,
}
impl Crg {
pub fn visit_attributes(
&self,
visitor: impl for<'b> FnOnce(
Cow<'b, [xml::attribute::Attribute<'b>]>,
) -> xml::writer::Result<()>,
) -> xml::writer::Result<()> {
visit_attributes_flatten!(
visitor,
"file" => Some(self.file.as_str()),
"hOffset" => self.h_offset.map(|v| v.value.to_scientific_string()).as_deref(),
"mode" => Some(self.mode.as_str()),
"orientation" => Some(self.orientation.as_str()),
"purpose" => self.purpose.as_ref().map(CrgPurpose::as_str),
"sEnd" => Some(self.s_end.value.to_scientific_string()).as_deref(),
"sOffset" => self.s_offset.map(|v| v.value.to_scientific_string()).as_deref(),
"sStart" => Some(self.s_start.value.to_scientific_string()).as_deref(),
"tOffset" => self.t_offset.map(|v| v.value.to_scientific_string()).as_deref(),
"zOffset" => self.z_offset.map(|v| v.value.to_scientific_string()).as_deref(),
"zScale" => self.z_scale.map(|v| v.to_scientific_string()).as_deref(),
)
}
pub fn visit_children(
&self,
mut visitor: impl FnMut(xml::writer::XmlEvent) -> xml::writer::Result<()>,
) -> xml::writer::Result<()> {
visit_children!(visitor);
Ok(())
}
}
impl<'a, I> TryFrom<crate::parser::ReadContext<'a, I>> for Crg
where
I: Iterator<Item = xml::reader::Result<xml::reader::XmlEvent>>,
{
type Error = Box<crate::parser::Error>;
fn try_from(mut read: crate::parser::ReadContext<'a, I>) -> Result<Self, Self::Error> {
read.expecting_no_child_elements_for(Self {
file: read.attribute("file")?,
h_offset: read.attribute_opt("hOffset")?.map(Angle::new::<radian>),
mode: read.attribute("mode")?,
orientation: read.attribute("orientation")?,
purpose: read.attribute_opt("purpose")?,
s_end: read.attribute("sEnd").map(Length::new::<meter>)?,
s_offset: read.attribute_opt("sOffset")?.map(Length::new::<meter>),
s_start: read.attribute("sStart").map(Length::new::<meter>)?,
t_offset: read.attribute_opt("tOffset")?.map(Length::new::<meter>),
z_offset: read.attribute_opt("zOffset")?.map(Length::new::<meter>),
z_scale: read.attribute_opt("zScale")?,
})
}
}
#[cfg(feature = "fuzzing")]
impl arbitrary::Arbitrary<'_> for Crg {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
use crate::fuzzing::NotNan;
Ok(Self {
file: u.arbitrary()?,
h_offset: u
.arbitrary::<Option<()>>()?
.map(|_| u.not_nan_f64())
.transpose()?
.map(Angle::new::<radian>),
mode: u.arbitrary()?,
orientation: u.arbitrary()?,
purpose: u.arbitrary()?,
s_end: u.not_nan_f64().map(Length::new::<meter>)?,
s_offset: u
.arbitrary::<Option<()>>()?
.map(|_| u.not_nan_f64())
.transpose()?
.map(Length::new::<meter>),
s_start: u.not_nan_f64().map(Length::new::<meter>)?,
t_offset: u
.arbitrary::<Option<()>>()?
.map(|_| u.not_nan_f64())
.transpose()?
.map(Length::new::<meter>),
z_offset: u
.arbitrary::<Option<()>>()?
.map(|_| u.not_nan_f64())
.transpose()?
.map(Length::new::<meter>),
z_scale: u
.arbitrary::<Option<()>>()?
.map(|_| u.not_nan_f64())
.transpose()?,
})
}
}