opendrive/lane/
lanes.rs

1use crate::core::additional_data::AdditionalData;
2use crate::lane::lane_section::LaneSection;
3use crate::lane::offset::Offset;
4use std::borrow::Cow;
5use vec1::Vec1;
6
7/// Contains a series of lane section elements that define the characteristics of the road cross
8/// sections with respect to the lanes along the reference line.
9#[derive(Debug, Clone, PartialEq)]
10pub struct Lanes {
11    pub lane_offset: Vec<Offset>,
12    pub lane_section: Vec1<LaneSection>,
13    pub additional_data: AdditionalData,
14}
15
16impl Lanes {
17    pub fn visit_attributes(
18        &self,
19        visitor: impl for<'b> FnOnce(
20            Cow<'b, [xml::attribute::Attribute<'b>]>,
21        ) -> xml::writer::Result<()>,
22    ) -> xml::writer::Result<()> {
23        visit_attributes!(visitor)
24    }
25
26    pub fn visit_children(
27        &self,
28        mut visitor: impl FnMut(xml::writer::XmlEvent) -> xml::writer::Result<()>,
29    ) -> xml::writer::Result<()> {
30        for lane_offset in &self.lane_offset {
31            visit_children!(visitor, "laneOffset" => lane_offset);
32        }
33
34        for lane_section in &self.lane_section {
35            visit_children!(visitor, "laneSection" => lane_section);
36        }
37
38        self.additional_data.append_children(visitor)
39    }
40}
41
42impl<'a, I> TryFrom<crate::parser::ReadContext<'a, I>> for Lanes
43where
44    I: Iterator<Item = xml::reader::Result<xml::reader::XmlEvent>>,
45{
46    type Error = Box<crate::parser::Error>;
47
48    fn try_from(mut read: crate::parser::ReadContext<'a, I>) -> Result<Self, Self::Error> {
49        let mut lane_offset = Vec::new();
50        let mut lane_section = Vec::new();
51        let mut additional_data = AdditionalData::default();
52
53        match_child_eq_ignore_ascii_case!(
54            read,
55            "laneOffset" => Offset => |v| lane_offset.push(v),
56            "laneSection" true => LaneSection => |v| lane_section.push(v),
57            _ => |_name, context| additional_data.fill(context),
58        );
59
60        Ok(Self {
61            lane_offset,
62            lane_section: Vec1::try_from_vec(lane_section).unwrap(),
63            additional_data,
64        })
65    }
66}
67
68#[cfg(feature = "fuzzing")]
69impl arbitrary::Arbitrary<'_> for Lanes {
70    fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
71        Ok(Self {
72            lane_offset: u.arbitrary()?,
73            lane_section: {
74                let mut vec1 = Vec1::new(u.arbitrary()?);
75                vec1.extend(u.arbitrary::<Vec<_>>()?);
76                vec1
77            },
78            additional_data: u.arbitrary()?,
79        })
80    }
81}