surfer_translation_types/
variable_meta.rs1#[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)]
11pub struct VariableMeta<VarId, ScopeId> {
13 pub var: VariableRef<VarId, ScopeId>,
15 pub num_bits: Option<u32>,
17 pub variable_type: Option<VariableType>,
19 pub variable_type_name: Option<String>,
21 pub index: Option<VariableIndex>,
23 pub direction: Option<VariableDirection>,
25 pub enum_map: HashMap<String, String>,
28 pub encoding: VariableEncoding,
32}
33
34impl<VarId, ScopeId> VariableMeta<VarId, ScopeId> {
35 pub fn is_parameter(&self) -> bool {
37 matches!(
38 self.variable_type,
39 Some(VariableType::VCDParameter | VariableType::RealParameter)
40 )
41 }
42
43 pub fn is_event(&self) -> bool {
45 matches!(
46 self.variable_type,
47 Some(VariableType::VCDEvent | VariableType::EventParameter)
48 )
49 }
50
51 pub fn is_real(&self) -> bool {
53 matches!(self.encoding, VariableEncoding::Real)
54 }
55
56 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}