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. A variable of "type" boolean for example
29    /// could be stored as a `String` or as a `BitVector`.
30    pub encoding: VariableEncoding,
31}
32
33impl<VarId, ScopeId> VariableMeta<VarId, ScopeId> {
34    /// Variable is a parameter
35    pub fn is_parameter(&self) -> bool {
36        matches!(
37            self.variable_type,
38            Some(VariableType::VCDParameter | VariableType::RealParameter)
39        )
40    }
41
42    /// Variable corresponds to events
43    pub fn is_event(&self) -> bool {
44        matches!(self.encoding, VariableEncoding::Event)
45    }
46
47    /// Variable has real encoding
48    pub fn is_real(&self) -> bool {
49        matches!(self.encoding, VariableEncoding::Real)
50    }
51
52    /// Types that should default to signed integer conversion
53    pub fn is_integer_type(&self) -> bool {
54        matches!(
55            self.variable_type,
56            Some(
57                VariableType::VCDInteger
58                    | VariableType::Int
59                    | VariableType::ShortInt
60                    | VariableType::LongInt
61            )
62        )
63    }
64}
65
66impl<VarId1, ScopeId1> VariableMeta<VarId1, ScopeId1> {
67    pub fn map_ids<VarId2, ScopeId2>(
68        self,
69        var_fn: impl FnMut(VarId1) -> VarId2,
70        scope_fn: impl FnMut(ScopeId1) -> ScopeId2,
71    ) -> VariableMeta<VarId2, ScopeId2> {
72        VariableMeta {
73            var: self.var.map_ids(var_fn, scope_fn),
74            num_bits: self.num_bits,
75            variable_type: self.variable_type,
76            index: self.index,
77            direction: self.direction,
78            enum_map: self.enum_map,
79            encoding: self.encoding,
80            variable_type_name: self.variable_type_name,
81        }
82    }
83}