quil_rs/instruction/
pragma.rs

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