quil_rs/instruction/
pragma.rs

1use crate::quil::Quil;
2
3use super::QuotedString;
4
5#[derive(Clone, Debug, PartialEq, Eq, Hash)]
6pub struct Pragma {
7    pub name: String,
8    pub arguments: Vec<PragmaArgument>,
9    pub data: Option<String>,
10}
11
12impl Pragma {
13    pub fn new(name: String, arguments: Vec<PragmaArgument>, data: Option<String>) -> Self {
14        Self {
15            name,
16            arguments,
17            data,
18        }
19    }
20}
21
22impl Quil for Pragma {
23    fn write(
24        &self,
25        f: &mut impl std::fmt::Write,
26        fall_back_to_debug: bool,
27    ) -> crate::quil::ToQuilResult<()> {
28        write!(f, "PRAGMA {}", self.name)?;
29        for arg in &self.arguments {
30            write!(f, " ")?;
31            arg.write(f, fall_back_to_debug)?;
32        }
33        if let Some(data) = &self.data {
34            write!(f, " {}", QuotedString(data))?;
35        }
36        Ok(())
37    }
38}
39
40#[derive(Clone, Debug, PartialEq, Eq, Hash)]
41pub enum PragmaArgument {
42    Identifier(String),
43    Integer(u64),
44}
45
46impl Quil for PragmaArgument {
47    fn write(
48        &self,
49        f: &mut impl std::fmt::Write,
50        _fall_back_to_debug: bool,
51    ) -> crate::quil::ToQuilResult<()> {
52        match self {
53            PragmaArgument::Identifier(i) => write!(f, "{i}"),
54            PragmaArgument::Integer(i) => write!(f, "{i}"),
55        }
56        .map_err(Into::into)
57    }
58}
59
60#[derive(Clone, Debug, PartialEq, Eq, Hash)]
61pub struct Include {
62    pub filename: String,
63}
64
65impl Quil for Include {
66    fn write(
67        &self,
68        f: &mut impl std::fmt::Write,
69        _fall_back_to_debug: bool,
70    ) -> crate::quil::ToQuilResult<()> {
71        write!(f, r#"INCLUDE {}"#, QuotedString(&self.filename)).map_err(Into::into)
72    }
73}
74
75impl Include {
76    pub fn new(filename: String) -> Self {
77        Self { filename }
78    }
79}
80
81pub const RESERVED_PRAGMA_EXTERN: &str = "EXTERN";