svd_rs/
interrupt.rs

1use super::{BuildError, Description, Name, SvdError, ValidateLevel};
2
3/// Describes an interrupt in the device
4#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
5#[derive(Clone, Debug, PartialEq, Eq)]
6#[non_exhaustive]
7pub struct Interrupt {
8    /// The string represents the interrupt name
9    pub name: String,
10
11    /// The string describes the interrupt
12    #[cfg_attr(
13        feature = "serde",
14        serde(default, skip_serializing_if = "Option::is_none")
15    )]
16    pub description: Option<String>,
17
18    /// Represents the enumeration index value associated to the interrupt
19    pub value: u32,
20}
21
22/// Builder for [`Interrupt`]
23#[derive(Clone, Debug, Default, PartialEq, Eq)]
24pub struct InterruptBuilder {
25    name: Option<String>,
26    description: Option<String>,
27    value: Option<u32>,
28}
29
30impl From<Interrupt> for InterruptBuilder {
31    fn from(d: Interrupt) -> Self {
32        Self {
33            name: Some(d.name),
34            description: d.description,
35            value: Some(d.value),
36        }
37    }
38}
39
40impl InterruptBuilder {
41    /// Set the name of the interrupt
42    pub fn name(mut self, value: String) -> Self {
43        self.name = Some(value);
44        self
45    }
46    /// Set the description of the interrupt
47    pub fn description(mut self, value: Option<String>) -> Self {
48        self.description = value;
49        self
50    }
51    /// Set the value of the interrupt
52    pub fn value(mut self, value: u32) -> Self {
53        self.value = Some(value);
54        self
55    }
56    /// Validate and build a [`Interrupt`].
57    pub fn build(self, lvl: ValidateLevel) -> Result<Interrupt, SvdError> {
58        let de = Interrupt {
59            name: self
60                .name
61                .ok_or_else(|| BuildError::Uninitialized("name".to_string()))?,
62            description: self.description,
63            value: self
64                .value
65                .ok_or_else(|| BuildError::Uninitialized("value".to_string()))?,
66        };
67        de.validate(lvl)?;
68        Ok(de)
69    }
70}
71
72impl Interrupt {
73    /// Make a builder for [`Interrupt`]
74    pub fn builder() -> InterruptBuilder {
75        InterruptBuilder::default()
76    }
77    /// Modify an existing [`Interrupt`] based on a [builder](InterruptBuilder).
78    pub fn modify_from(
79        &mut self,
80        builder: InterruptBuilder,
81        lvl: ValidateLevel,
82    ) -> Result<(), SvdError> {
83        if let Some(name) = builder.name {
84            self.name = name;
85        }
86        if builder.description.is_some() {
87            self.description = builder.description;
88        }
89        if let Some(value) = builder.value {
90            self.value = value;
91        }
92        self.validate(lvl)
93    }
94    /// Validate the [`Interrupt`].
95    ///
96    /// # Notes
97    ///
98    /// This doesn't do anything.
99    pub fn validate(&self, _lvl: ValidateLevel) -> Result<(), SvdError> {
100        Ok(())
101    }
102}
103
104impl Name for Interrupt {
105    fn name(&self) -> &str {
106        &self.name
107    }
108}
109
110impl Description for Interrupt {
111    fn description(&self) -> Option<&str> {
112        self.description.as_deref()
113    }
114}