Skip to main content

source_generator/vhdl/
direction.rs

1use serde_derive::Deserialize;
2
3#[derive(Deserialize, Debug, Clone, Copy)]
4#[serde(rename_all = "lowercase")]
5pub enum Direction {
6    IN,
7    OUT,
8    INOUT,
9    BUFFER
10}
11
12impl Direction {
13    pub fn invert( & mut self ) {
14        match self {
15            Direction::IN => { * self = Direction::OUT },
16            Direction::OUT => { * self = Direction::IN },
17            Direction::INOUT => { * self = Direction::INOUT },
18            Direction::BUFFER => { * self = Direction::IN },
19        }
20    }
21
22    pub fn get_inverted( & self ) -> Direction {
23        match self {
24            Direction::IN => { Direction::OUT },
25            Direction::OUT => { Direction::IN },
26            Direction::INOUT => { Direction::INOUT },
27            Direction::BUFFER => { Direction::IN },
28        }
29    }
30}
31
32impl std::fmt::Display for Direction {
33    fn fmt( & self, f : & mut std::fmt::Formatter ) -> std::fmt::Result {
34        match self {
35            Direction::IN => write!( f, "{}", crate::vhdl::keywords::IN ),
36            Direction::OUT => write!( f, "{}", crate::vhdl::keywords::OUT ),
37            Direction::INOUT => write!( f, "{}", crate::vhdl::keywords::INOUT ),
38            Direction::BUFFER => write!( f, "{}", crate::vhdl::keywords::BUFFER ),
39        }
40    }
41}
42
43
44//------------------------------------------------------------------------------
45#[cfg(test)]
46mod tests {
47    use super::*;
48    use std::error::Error;
49    use std::matches;
50
51    #[test]
52    fn deserialize_in() -> Result< (), Box< dyn Error > > {
53        let direction : Direction = serde_json::from_str( "\"in\"" )?;
54        assert!( matches!( direction, Direction::IN ) );
55        Ok(())
56    }
57
58    #[test]
59    fn deserialize_out() -> Result< (), Box< dyn Error > > {
60        let direction : Direction = serde_json::from_str( "\"out\"" )?;
61        assert!( matches!( direction, Direction::OUT ) );
62        Ok(())
63    }
64
65    #[test]
66    fn deserialize_inout() -> Result< (), Box< dyn Error > > {
67        let direction : Direction = serde_json::from_str( "\"inout\"" )?;
68        assert!( matches!( direction, Direction::INOUT ) );
69        Ok(())
70    }
71
72    #[test]
73    fn deserialize_buffer() -> Result< (), Box< dyn Error > > {
74        let direction : Direction = serde_json::from_str( "\"buffer\"" )?;
75        assert!( matches!( direction, Direction::BUFFER ) );
76        Ok(())
77    }
78
79    #[test]
80    fn deserialize_invalid() {
81        let ret : Result< Direction, serde_json::Error > = serde_json::from_str( "\"invalid\"" );
82        assert!( ret.is_err() );
83    }
84
85    #[test]
86    fn in_to_string() {
87        assert_eq!( Direction::IN.to_string(), String::from( "in" ) );
88    }
89
90    #[test]
91    fn out_to_string() {
92        assert_eq!( Direction::OUT.to_string(), String::from( "out" ) );
93    }
94
95    #[test]
96    fn inout_to_string() {
97        assert_eq!( Direction::INOUT.to_string(), String::from( "inout" ) );
98    }
99
100    #[test]
101    fn buffer_to_string() {
102        assert_eq!( Direction::BUFFER.to_string(), String::from( "buffer" ) );
103    }
104
105    #[test]
106    fn invert_in() {
107        let mut direction = Direction::IN;
108        direction.invert();
109        assert_eq!( direction.to_string(), String::from( "out" ) );
110    }
111
112    #[test]
113    fn invert_out() {
114        let mut direction = Direction::OUT;
115        direction.invert();
116        assert_eq!( direction.to_string(), String::from( "in" ) );
117    }
118
119    #[test]
120    fn invert_inout() {
121        let mut direction = Direction::INOUT;
122        direction.invert();
123        assert_eq!( direction.to_string(), String::from( "inout" ) );
124    }
125
126    #[test]
127    fn invert_buffer() {
128        let mut direction = Direction::BUFFER;
129        direction.invert();
130        assert_eq!( direction.to_string(), String::from( "in" ) );
131    }
132
133    #[test]
134    fn get_inverted_in() {
135        assert_eq!( Direction::IN.get_inverted().to_string(), String::from( "out" ) );
136    }
137
138    #[test]
139    fn get_inverted_out() {
140        assert_eq!( Direction::OUT.get_inverted().to_string(), String::from( "in" ) );
141    }
142
143    #[test]
144    fn get_inverted_inout() {
145        assert_eq!( Direction::INOUT.get_inverted().to_string(), String::from( "inout" ) );
146    }
147
148    #[test]
149    fn get_inverted_buffer() {
150        assert_eq!( Direction::BUFFER.get_inverted().to_string(), String::from( "in" ) );
151    }
152}
153