surfer_translation_types/
result.rs1#[cfg(feature = "wasm_plugins")]
2use extism_convert::{FromBytes, Json, ToBytes};
3use serde::{Deserialize, Serialize};
4
5use crate::ValueKind;
6
7#[cfg_attr(feature = "wasm_plugins", derive(FromBytes, ToBytes))]
8#[cfg_attr(feature = "wasm_plugins", encoding(Json))]
9#[derive(Clone, Serialize, Deserialize)]
10pub struct TranslationResult {
11 pub val: ValueRepr,
12 pub subfields: Vec<SubFieldTranslationResult>,
13 pub kind: ValueKind,
14}
15
16impl TranslationResult {
17 pub fn single_string(s: impl Into<String>, kind: ValueKind) -> Self {
18 TranslationResult {
19 val: ValueRepr::String(s.into()),
20 subfields: vec![],
21 kind,
22 }
23 }
24}
25
26#[derive(Clone, Serialize, Deserialize)]
29pub enum ValueRepr {
30 Bit(char),
31 Bits(u32, String),
33 String(String),
35 Tuple,
37 Struct,
39 Enum {
43 idx: usize,
44 name: String,
45 },
46 Array,
48 NotPresent,
52 Event,
53}
54
55#[derive(Clone, Serialize, Deserialize)]
56pub struct SubFieldFlatTranslationResult {
57 pub names: Vec<String>,
58 pub value: Option<TranslatedValue>,
59}
60
61pub struct HierFormatResult {
63 pub names: Vec<String>,
64 pub this: Option<TranslatedValue>,
65 pub fields: Vec<HierFormatResult>,
68}
69
70impl HierFormatResult {
71 pub fn collect_into(self, into: &mut Vec<SubFieldFlatTranslationResult>) {
72 into.push(SubFieldFlatTranslationResult {
73 names: self.names,
74 value: self.this,
75 });
76 self.fields.into_iter().for_each(|r| r.collect_into(into));
77 }
78}
79
80#[derive(Clone, Serialize, Deserialize)]
81pub struct SubFieldTranslationResult {
82 pub name: String,
83 pub result: TranslationResult,
84}
85
86impl SubFieldTranslationResult {
87 pub fn new(name: &impl ToString, result: TranslationResult) -> Self {
88 SubFieldTranslationResult {
89 name: name.to_string(),
90 result,
91 }
92 }
93}
94
95#[derive(Clone, PartialEq, Serialize, Deserialize)]
96pub struct TranslatedValue {
97 pub value: String,
98 pub kind: ValueKind,
99}
100
101impl TranslatedValue {
102 #[must_use]
103 pub fn from_basic_translate(result: (String, ValueKind)) -> Self {
104 TranslatedValue {
105 value: result.0,
106 kind: result.1,
107 }
108 }
109
110 pub fn new(value: &impl ToString, kind: ValueKind) -> Self {
111 TranslatedValue {
112 value: value.to_string(),
113 kind,
114 }
115 }
116}