Skip to main content

interstice_abi/interstice_value/
mod.rs

1mod convert;
2mod index_key;
3mod row;
4mod validate;
5
6use std::fmt::{Display, Write};
7
8pub use index_key::IndexKey;
9pub use validate::validate_value;
10
11use serde::{Deserialize, Serialize};
12
13#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
14pub enum IntersticeValue {
15    Void,
16    U8(u8),
17    U32(u32),
18    U64(u64),
19    I32(i32),
20    I64(i64),
21    F32(f32),
22    F64(f64),
23    Bool(bool),
24    String(String),
25
26    Vec(Vec<IntersticeValue>),
27    Option(Option<Box<IntersticeValue>>),
28    Tuple(Vec<IntersticeValue>),
29
30    Struct {
31        name: String,
32        fields: Vec<Field>,
33    },
34
35    Enum {
36        name: String,
37        variant: String,
38        value: Box<IntersticeValue>,
39    },
40}
41
42#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
43pub struct Field {
44    pub name: String,
45    pub value: IntersticeValue,
46}
47
48impl Display for IntersticeValue {
49    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50        self.fmt_with_indent(f, 0)
51    }
52}
53
54impl IntersticeValue {
55    fn fmt_with_indent(&self, f: &mut std::fmt::Formatter<'_>, indent: usize) -> std::fmt::Result {
56        let indent_str = "  ".repeat(indent);
57        let next_indent_str = "  ".repeat(indent + 1);
58
59        match self {
60            IntersticeValue::Void => write!(f, "void"),
61            IntersticeValue::U8(value) => write!(f, "{value}"),
62            IntersticeValue::U32(value) => write!(f, "{value}"),
63            IntersticeValue::U64(value) => write!(f, "{value}"),
64            IntersticeValue::I32(value) => write!(f, "{value}"),
65            IntersticeValue::I64(value) => write!(f, "{value}"),
66            IntersticeValue::F32(value) => write!(f, "{value}"),
67            IntersticeValue::F64(value) => write!(f, "{value}"),
68            IntersticeValue::Bool(value) => write!(f, "{value}"),
69            IntersticeValue::String(value) => write!(f, "\"{value}\""),
70            IntersticeValue::Vec(values) => {
71                write!(f, "[\n")?;
72                for value in values {
73                    write!(f, "{next_indent_str}")?;
74                    value.fmt_with_indent(f, indent + 1)?;
75                    write!(f, ",\n")?;
76                }
77                write!(f, "{indent_str}]")
78            }
79            IntersticeValue::Option(opt) => {
80                if let Some(inner) = opt {
81                    write!(f, "Some(")?;
82                    inner.fmt_with_indent(f, indent)?;
83                    write!(f, ")")
84                } else {
85                    write!(f, "None")
86                }
87            }
88            IntersticeValue::Tuple(values) => {
89                let values_str = values
90                    .iter()
91                    .map(|v| {
92                        let mut buf = String::new();
93                        let _ = write!(buf, "{}", v);
94                        buf
95                    })
96                    .collect::<Vec<_>>()
97                    .join(", ");
98                write!(f, "({values_str})")
99            }
100            IntersticeValue::Struct { name, fields } => {
101                write!(f, "{} {{\n", name)?;
102                for field in fields {
103                    write!(f, "{next_indent_str}{}: ", field.name)?;
104                    field.value.fmt_with_indent(f, indent + 1)?;
105                    write!(f, ",\n")?;
106                }
107                write!(f, "{indent_str}}}")
108            }
109            IntersticeValue::Enum {
110                name,
111                variant,
112                value,
113            } => {
114                write!(f, "{}::{}(", name, variant)?;
115                value.fmt_with_indent(f, indent)?;
116                write!(f, ")")
117            }
118        }
119    }
120}