Skip to main content

surfer_translation_types/
variable_meta.rs

1#[cfg(feature = "wasm_plugins")]
2use extism_convert::{FromBytes, Json, ToBytes};
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5
6use crate::{VariableDirection, VariableEncoding, VariableIndex, VariableRef, VariableType};
7
8#[cfg_attr(feature = "wasm_plugins", derive(FromBytes, ToBytes))]
9#[cfg_attr(feature = "wasm_plugins", encoding(Json))]
10#[derive(Clone, Debug, Serialize, Deserialize)]
11/// Additional information about a variable in the waveform.
12pub struct VariableMeta<VarId, ScopeId> {
13    /// Reference to the variable.
14    pub var: VariableRef<VarId, ScopeId>,
15    /// Number of bits for the variable, if applicable.
16    pub num_bits: Option<u32>,
17    /// Type of the variable in the HDL (on a best effort basis).
18    pub variable_type: Option<VariableType>,
19    /// Type name of variable, if available.
20    pub variable_type_name: Option<String>,
21    /// Index information for the variable, if available.
22    pub index: Option<VariableIndex>,
23    /// Direction of the variable, if available.
24    pub direction: Option<VariableDirection>,
25    /// For enum variables, either an enumerated type in VHDL or an enum in SystemVerilog,
26    /// a mapping from enum option names to their string representations.
27    pub enum_map: HashMap<String, String>,
28    /// Indicates how the variable is stored.
29    ///
30    /// For example, a variable of "type" boolean can be stored as a `String` or as a `BitVector`.
31    pub encoding: VariableEncoding,
32}
33
34impl<VarId, ScopeId> VariableMeta<VarId, ScopeId> {
35    /// Variable is a parameter
36    pub fn is_parameter(&self) -> bool {
37        matches!(
38            self.variable_type,
39            Some(VariableType::VCDParameter | VariableType::RealParameter)
40        )
41    }
42
43    /// Variable corresponds to events
44    pub fn is_event(&self) -> bool {
45        matches!(
46            self.variable_type,
47            Some(VariableType::VCDEvent | VariableType::EventParameter)
48        )
49    }
50
51    /// Variable has real encoding
52    pub fn is_real(&self) -> bool {
53        matches!(self.encoding, VariableEncoding::Real)
54    }
55
56    /// Types that should default to signed integer conversion
57    pub fn is_integer_type(&self) -> bool {
58        matches!(
59            self.variable_type,
60            Some(
61                VariableType::VCDInteger
62                    | VariableType::Int
63                    | VariableType::ShortInt
64                    | VariableType::LongInt
65            )
66        )
67    }
68}
69
70impl<VarId1, ScopeId1> VariableMeta<VarId1, ScopeId1> {
71    pub fn map_ids<VarId2, ScopeId2>(
72        self,
73        var_fn: impl FnMut(VarId1) -> VarId2,
74        scope_fn: impl FnMut(ScopeId1) -> ScopeId2,
75    ) -> VariableMeta<VarId2, ScopeId2> {
76        VariableMeta {
77            var: self.var.map_ids(var_fn, scope_fn),
78            num_bits: self.num_bits,
79            variable_type: self.variable_type,
80            index: self.index,
81            direction: self.direction,
82            enum_map: self.enum_map,
83            encoding: self.encoding,
84            variable_type_name: self.variable_type_name,
85        }
86    }
87}