rusty_value/
value.rs

1use std::{collections::HashMap, ffi::OsString};
2
3/// Represents a generic rust value
4#[derive(Clone, Debug, PartialEq)]
5pub enum Value {
6    Primitive(Primitive),
7    Struct(Struct),
8    Enum(Enum),
9    Map(HashMap<HashableValue, Value>),
10    List(Vec<Value>),
11    None,
12}
13
14/// Represents an enum with a given variant
15/// And fields depending on that variant
16#[derive(Clone, Debug, PartialEq)]
17pub struct Enum {
18    pub name: String,
19    pub variant: String,
20    pub fields: Fields,
21}
22
23/// Represents a struct with fields
24#[derive(Clone, Debug, PartialEq)]
25pub struct Struct {
26    pub name: String,
27    pub fields: Fields,
28}
29
30/// Fields of a struct or an enum that are either named, unnamed or not defined (Unit enums/structs)
31#[derive(Clone, Debug, PartialEq)]
32pub enum Fields {
33    Named(HashMap<String, Value>),
34    Unnamed(Vec<Value>),
35    Unit,
36}
37
38/// A rust primitive value
39#[derive(Clone, Debug, PartialEq, PartialOrd)]
40pub enum Primitive {
41    Integer(Integer),
42    Float(Float),
43    String(String),
44    OsString(OsString),
45    Char(char),
46    Bool(bool),
47}
48
49/// A primitive integer value
50#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
51pub enum Integer {
52    USize(usize),
53    ISize(isize),
54    U8(u8),
55    I8(i8),
56    U16(u16),
57    I16(i16),
58    U32(u32),
59    I32(i32),
60    U64(u64),
61    I64(i64),
62    U128(u128),
63    I128(i128),
64}
65
66/// A primitive float value
67#[derive(Clone, Debug, PartialEq, PartialOrd)]
68pub enum Float {
69    F32(f32),
70    F64(f64),
71}
72
73/// A value that can be used as a key inside a hash map
74#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
75pub enum HashableValue {
76    Primitive(HashablePrimitive),
77    List(Vec<HashableValue>),
78    None,
79}
80
81/// A primitive that can be used as a hash map key
82#[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord, Hash)]
83pub enum HashablePrimitive {
84    Integer(Integer),
85    String(String),
86    OsString(OsString),
87    Char(char),
88    Bool(bool),
89}
90
91impl ToString for HashablePrimitive {
92    fn to_string(&self) -> String {
93        match self {
94            HashablePrimitive::Integer(i) => i.to_string(),
95            HashablePrimitive::String(s) => s.to_owned(),
96            HashablePrimitive::Char(c) => c.to_string(),
97            HashablePrimitive::Bool(b) => b.to_string(),
98            HashablePrimitive::OsString(o) => o.to_string_lossy().into_owned(),
99        }
100    }
101}
102
103impl ToString for Integer {
104    fn to_string(&self) -> String {
105        match self {
106            Integer::USize(n) => n.to_string(),
107            Integer::ISize(n) => n.to_string(),
108            Integer::U8(n) => n.to_string(),
109            Integer::I8(n) => n.to_string(),
110            Integer::U16(n) => n.to_string(),
111            Integer::I16(n) => n.to_string(),
112            Integer::U32(n) => n.to_string(),
113            Integer::I32(n) => n.to_string(),
114            Integer::U64(n) => n.to_string(),
115            Integer::I64(n) => n.to_string(),
116            Integer::U128(n) => n.to_string(),
117            Integer::I128(n) => n.to_string(),
118        }
119    }
120}
121
122impl ToString for Float {
123    fn to_string(&self) -> String {
124        match self {
125            Float::F32(f) => f.to_string(),
126            Float::F64(f) => f.to_string(),
127        }
128    }
129}