snarkvm_synthesizer_program/logic/command/
position.rs1use crate::Opcode;
17use console::{network::prelude::*, program::Identifier};
18
19#[derive(Clone, PartialEq, Eq, Hash)]
22pub struct Position<N: Network> {
23 name: Identifier<N>,
25}
26
27impl<N: Network> Position<N> {
28 #[inline]
30 pub const fn opcode() -> Opcode {
31 Opcode::Command("position")
32 }
33
34 #[inline]
36 pub fn name(&self) -> &Identifier<N> {
37 &self.name
38 }
39
40 #[inline]
42 pub fn contains_external_struct(&self) -> bool {
43 false
44 }
45}
46
47impl<N: Network> Position<N> {
48 #[inline]
51 pub fn finalize(&self) -> Result<()> {
52 Ok(())
53 }
54}
55
56impl<N: Network> Parser for Position<N> {
57 #[inline]
59 fn parse(string: &str) -> ParserResult<Self> {
60 let (string, _) = Sanitizer::parse(string)?;
62 let (string, _) = tag(*Self::opcode())(string)?;
64 let (string, _) = Sanitizer::parse_whitespaces(string)?;
66
67 let (string, name) = Identifier::parse(string)?;
69
70 let (string, _) = Sanitizer::parse_whitespaces(string)?;
72 let (string, _) = tag(";")(string)?;
74
75 Ok((string, Self { name }))
76 }
77}
78
79impl<N: Network> FromStr for Position<N> {
80 type Err = Error;
81
82 #[inline]
84 fn from_str(string: &str) -> Result<Self> {
85 match Self::parse(string) {
86 Ok((remainder, object)) => {
87 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
89 Ok(object)
91 }
92 Err(error) => bail!("Failed to parse string. {error}"),
93 }
94 }
95}
96
97impl<N: Network> Debug for Position<N> {
98 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
100 Display::fmt(self, f)
101 }
102}
103
104impl<N: Network> Display for Position<N> {
105 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
107 write!(f, "{} ", Self::opcode())?;
109 write!(f, "{};", self.name)
111 }
112}
113
114impl<N: Network> FromBytes for Position<N> {
115 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
117 let name = Identifier::read_le(&mut reader)?;
119 Ok(Self { name })
121 }
122}
123
124impl<N: Network> ToBytes for Position<N> {
125 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
127 self.name.write_le(&mut writer)
129 }
130}
131
132#[cfg(test)]
133mod tests {
134 use super::*;
135 use console::network::MainnetV0;
136
137 type CurrentNetwork = MainnetV0;
138
139 #[test]
140 fn test_parse() {
141 let (string, position) = Position::<CurrentNetwork>::parse("position exit;").unwrap();
142 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
143 assert_eq!(position.name, Identifier::from_str("exit").unwrap());
144 }
145}