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,
31}
32
33impl<VarId, ScopeId> VariableMeta<VarId, ScopeId> {
34 pub fn is_parameter(&self) -> bool {
36 matches!(
37 self.variable_type,
38 Some(VariableType::VCDParameter | VariableType::RealParameter)
39 )
40 }
41
42 pub fn is_event(&self) -> bool {
44 matches!(self.encoding, VariableEncoding::Event)
45 }
46
47 pub fn is_real(&self) -> bool {
49 matches!(self.encoding, VariableEncoding::Real)
50 }
51
52 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}