1+ use super :: { BuildError , SvdError , ValidateLevel } ;
2+
13/// Describes an interrupt in the device
24#[ cfg_attr( feature = "serde" , derive( serde:: Deserialize , serde:: Serialize ) ) ]
35#[ derive( Clone , Debug , PartialEq ) ]
6+ #[ non_exhaustive]
47pub struct Interrupt {
58 /// The string represents the interrupt name
69 pub name : String ,
@@ -15,3 +18,91 @@ pub struct Interrupt {
1518 /// Represents the enumeration index value associated to the interrupt
1619 pub value : u32 ,
1720}
21+
22+ /// Builder for [`Interrupt`]
23+ #[ derive( Clone , Debug , Default , PartialEq ) ]
24+ pub struct InterruptBuilder {
25+ name : Option < String > ,
26+ description : Option < String > ,
27+ value : Option < u32 > ,
28+ }
29+
30+ impl 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+
40+ impl 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 mut 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+ if !lvl. is_disabled ( ) {
68+ de. validate ( lvl) ?;
69+ }
70+ Ok ( de)
71+ }
72+ }
73+
74+ impl Interrupt {
75+ /// Make a builder for [`Interrupt`]
76+ pub fn builder ( ) -> InterruptBuilder {
77+ InterruptBuilder :: default ( )
78+ }
79+ /// Modify an existing [`Interrupt`] based on a [builder](InterruptBuilder).
80+ pub fn modify_from (
81+ & mut self ,
82+ builder : InterruptBuilder ,
83+ lvl : ValidateLevel ,
84+ ) -> Result < ( ) , SvdError > {
85+ if let Some ( name) = builder. name {
86+ self . name = name;
87+ }
88+ if builder. description . is_some ( ) {
89+ self . description = builder. description ;
90+ }
91+ if let Some ( value) = builder. value {
92+ self . value = value;
93+ }
94+ if !lvl. is_disabled ( ) {
95+ self . validate ( lvl)
96+ } else {
97+ Ok ( ( ) )
98+ }
99+ }
100+ /// Validate the [`Interrupt`].
101+ ///
102+ /// # Notes
103+ ///
104+ /// This doesn't do anything.
105+ pub fn validate ( & mut self , _lvl : ValidateLevel ) -> Result < ( ) , SvdError > {
106+ Ok ( ( ) )
107+ }
108+ }
0 commit comments