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
41impl<N: Network> Position<N> {
42 #[inline]
45 pub fn finalize(&self) -> Result<()> {
46 Ok(())
47 }
48}
49
50impl<N: Network> Parser for Position<N> {
51 #[inline]
53 fn parse(string: &str) -> ParserResult<Self> {
54 let (string, _) = Sanitizer::parse(string)?;
56 let (string, _) = tag(*Self::opcode())(string)?;
58 let (string, _) = Sanitizer::parse_whitespaces(string)?;
60
61 let (string, name) = Identifier::parse(string)?;
63
64 let (string, _) = Sanitizer::parse_whitespaces(string)?;
66 let (string, _) = tag(";")(string)?;
68
69 Ok((string, Self { name }))
70 }
71}
72
73impl<N: Network> FromStr for Position<N> {
74 type Err = Error;
75
76 #[inline]
78 fn from_str(string: &str) -> Result<Self> {
79 match Self::parse(string) {
80 Ok((remainder, object)) => {
81 ensure!(remainder.is_empty(), "Failed to parse string. Found invalid character in: \"{remainder}\"");
83 Ok(object)
85 }
86 Err(error) => bail!("Failed to parse string. {error}"),
87 }
88 }
89}
90
91impl<N: Network> Debug for Position<N> {
92 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
94 Display::fmt(self, f)
95 }
96}
97
98impl<N: Network> Display for Position<N> {
99 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
101 write!(f, "{} ", Self::opcode())?;
103 write!(f, "{};", self.name)
105 }
106}
107
108impl<N: Network> FromBytes for Position<N> {
109 fn read_le<R: Read>(mut reader: R) -> IoResult<Self> {
111 let name = Identifier::read_le(&mut reader)?;
113 Ok(Self { name })
115 }
116}
117
118impl<N: Network> ToBytes for Position<N> {
119 fn write_le<W: Write>(&self, mut writer: W) -> IoResult<()> {
121 self.name.write_le(&mut writer)
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129 use console::network::MainnetV0;
130
131 type CurrentNetwork = MainnetV0;
132
133 #[test]
134 fn test_parse() {
135 let (string, position) = Position::<CurrentNetwork>::parse("position exit;").unwrap();
136 assert!(string.is_empty(), "Parser did not consume all of the string: '{string}'");
137 assert_eq!(position.name, Identifier::from_str("exit").unwrap());
138 }
139}