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(u64, String),
33 String(String),
35 Tuple,
37 Struct,
39 Enum {
43 idx: usize,
44 name: String,
45 },
46 Array,
48 NotPresent,
51}
52
53#[derive(Clone, Serialize, Deserialize)]
54pub struct SubFieldFlatTranslationResult {
55 pub names: Vec<String>,
56 pub value: Option<TranslatedValue>,
57}
58
59pub struct HierFormatResult {
61 pub names: Vec<String>,
62 pub this: Option<TranslatedValue>,
63 pub fields: Vec<HierFormatResult>,
66}
67
68impl HierFormatResult {
69 pub fn collect_into(self, into: &mut Vec<SubFieldFlatTranslationResult>) {
70 into.push(SubFieldFlatTranslationResult {
71 names: self.names,
72 value: self.this,
73 });
74 self.fields.into_iter().for_each(|r| r.collect_into(into));
75 }
76}
77
78#[derive(Clone, Serialize, Deserialize)]
79pub struct SubFieldTranslationResult {
80 pub name: String,
81 pub result: TranslationResult,
82}
83
84impl SubFieldTranslationResult {
85 pub fn new(name: impl ToString, result: TranslationResult) -> Self {
86 SubFieldTranslationResult {
87 name: name.to_string(),
88 result,
89 }
90 }
91}
92
93#[derive(Clone, PartialEq, Serialize, Deserialize)]
94pub struct TranslatedValue {
95 pub value: String,
96 pub kind: ValueKind,
97}
98
99impl TranslatedValue {
100 pub fn from_basic_translate(result: (String, ValueKind)) -> Self {
101 TranslatedValue {
102 value: result.0,
103 kind: result.1,
104 }
105 }
106
107 pub fn new(value: impl ToString, kind: ValueKind) -> Self {
108 TranslatedValue {
109 value: value.to_string(),
110 kind,
111 }
112 }
113}