surfer_translation_types/
lib.rs

1mod field_ref;
2pub mod plugin_types;
3#[cfg(feature = "pyo3")]
4pub mod python;
5mod result;
6mod scope_ref;
7pub mod translator;
8pub mod variable_index;
9mod variable_ref;
10
11use derive_more::Display;
12use ecolor::Color32;
13use extism_convert::{FromBytes, Json, ToBytes};
14use num::BigUint;
15use serde::{Deserialize, Serialize};
16use std::collections::HashMap;
17
18pub use crate::field_ref::FieldRef;
19pub use crate::result::{
20    HierFormatResult, SubFieldFlatTranslationResult, SubFieldTranslationResult, TranslatedValue,
21    TranslationResult, ValueRepr,
22};
23pub use crate::scope_ref::ScopeRef;
24pub use crate::translator::{translates_all_bit_types, BasicTranslator, Translator, WaveSource};
25pub use crate::variable_index::VariableIndex;
26pub use crate::variable_ref::VariableRef;
27
28#[derive(Deserialize, Serialize, FromBytes, ToBytes)]
29#[encoding(Json)]
30pub struct PluginConfig(pub HashMap<String, String>);
31
32#[derive(Debug, PartialEq, Clone, Display, Serialize, Deserialize)]
33pub enum VariableValue {
34    #[display("{_0}")]
35    BigUint(BigUint),
36    #[display("{_0}")]
37    String(String),
38}
39
40#[derive(Clone, PartialEq, Copy, Debug, Serialize, Deserialize)]
41pub enum ValueKind {
42    Normal,
43    Undef,
44    HighImp,
45    Custom(Color32),
46    Warn,
47    DontCare,
48    Weak,
49}
50
51#[derive(PartialEq, Deserialize, Serialize, FromBytes, ToBytes)]
52#[encoding(Json)]
53pub enum TranslationPreference {
54    /// This translator prefers translating the variable, so it will be selected
55    /// as the default translator for the variable
56    Prefer,
57    /// This translator is able to translate the variable, but will not be
58    /// selected by default, the user has to select it
59    Yes,
60    No,
61}
62
63/// Static information about the structure of a variable.
64#[derive(Clone, Debug, Default, Deserialize, Serialize, FromBytes, ToBytes)]
65#[encoding(Json)]
66pub enum VariableInfo {
67    Compound {
68        subfields: Vec<(String, VariableInfo)>,
69    },
70    Bits,
71    Bool,
72    Clock,
73    // NOTE: only used for state saving where translators will clear this out with the actual value
74    #[default]
75    String,
76    Real,
77}
78
79#[derive(Debug, Display, Clone, Copy, Eq, PartialEq, Serialize, Deserialize)]
80pub enum VariableType {
81    // VCD-specific types
82    #[display("event")]
83    VCDEvent,
84    #[display("reg")]
85    VCDReg,
86    #[display("wire")]
87    VCDWire,
88    #[display("real")]
89    VCDReal,
90    #[display("time")]
91    VCDTime,
92    #[display("string")]
93    VCDString,
94    #[display("parameter")]
95    VCDParameter,
96    #[display("integer")]
97    VCDInteger,
98    #[display("real time")]
99    VCDRealTime,
100    #[display("supply 0")]
101    VCDSupply0,
102    #[display("supply 1")]
103    VCDSupply1,
104    #[display("tri")]
105    VCDTri,
106    #[display("tri and")]
107    VCDTriAnd,
108    #[display("tri or")]
109    VCDTriOr,
110    #[display("tri reg")]
111    VCDTriReg,
112    #[display("tri 0")]
113    VCDTri0,
114    #[display("tri 1")]
115    VCDTri1,
116    #[display("wand")]
117    VCDWAnd,
118    #[display("wor")]
119    VCDWOr,
120    #[display("port")]
121    Port,
122    #[display("sparse array")]
123    SparseArray,
124    #[display("realtime")]
125    RealTime,
126
127    // System Verilog
128    #[display("bit")]
129    Bit,
130    #[display("logic")]
131    Logic,
132    #[display("int")]
133    Int,
134    #[display("shortint")]
135    ShortInt,
136    #[display("longint")]
137    LongInt,
138    #[display("byte")]
139    Byte,
140    #[display("enum")]
141    Enum,
142    #[display("shortreal")]
143    ShortReal,
144
145    // VHDL (these are the types emitted by GHDL)
146    #[display("boolean")]
147    Boolean,
148    #[display("bit_vector")]
149    BitVector,
150    #[display("std_logic")]
151    StdLogic,
152    #[display("std_logic_vector")]
153    StdLogicVector,
154    #[display("std_ulogic")]
155    StdULogic,
156    #[display("std_ulogic_vector")]
157    StdULogicVector,
158}
159
160#[derive(Clone, Display, Copy, PartialOrd, Debug, Eq, PartialEq, Serialize, Deserialize)]
161pub enum VariableDirection {
162    // Ordering is used for sorting variable list
163    #[display("input")]
164    Input,
165    #[display("output")]
166    Output,
167    #[display("inout")]
168    InOut,
169    #[display("buffer")]
170    Buffer,
171    #[display("linkage")]
172    Linkage,
173    #[display("implicit")]
174    Implicit,
175    #[display("unknown")]
176    Unknown,
177}
178
179#[derive(Clone, Debug, Serialize, Deserialize, FromBytes, ToBytes)]
180#[encoding(Json)]
181pub struct VariableMeta<VarId, ScopeId> {
182    pub var: VariableRef<VarId, ScopeId>,
183    pub num_bits: Option<u32>,
184    /// Type of the variable in the HDL (on a best effort basis).
185    pub variable_type: Option<VariableType>,
186    /// Type name of variable, if available
187    pub variable_type_name: Option<String>,
188    pub index: Option<VariableIndex>,
189    pub direction: Option<VariableDirection>,
190    pub enum_map: HashMap<String, String>,
191    /// Indicates how the variable is stored. A variable of "type" boolean for example
192    /// could be stored as a String or as a BitVector.
193    pub encoding: VariableEncoding,
194}
195
196#[derive(Debug, Copy, Clone, Eq, PartialEq, Serialize, Deserialize)]
197pub enum VariableEncoding {
198    String,
199    Real,
200    BitVector,
201}
202
203impl<VarId1, ScopeId1> VariableMeta<VarId1, ScopeId1> {
204    pub fn map_ids<VarId2, ScopeId2>(
205        self,
206        var_fn: impl FnMut(VarId1) -> VarId2,
207        scope_fn: impl FnMut(ScopeId1) -> ScopeId2,
208    ) -> VariableMeta<VarId2, ScopeId2> {
209        VariableMeta {
210            var: self.var.map_ids(var_fn, scope_fn),
211            num_bits: self.num_bits,
212            variable_type: self.variable_type,
213            index: self.index,
214            direction: self.direction,
215            enum_map: self.enum_map,
216            encoding: self.encoding,
217            variable_type_name: self.variable_type_name,
218        }
219    }
220}