1use crate::array::NeoArray;
2use crate::boolean::NeoBoolean;
3use crate::bytestring::NeoByteString;
4use crate::integer::NeoInteger;
5use crate::map::NeoMap;
6use crate::string::NeoString;
7
8#[cfg(feature = "serde")]
9use serde::{Deserialize, Serialize};
10
11use std::string::String;
13use std::vec::Vec;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
17#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
18pub struct NeoStruct {
19 fields: Vec<(String, NeoValue)>,
20}
21
22impl NeoStruct {
23 pub fn new() -> Self {
24 Self { fields: Vec::new() }
25 }
26
27 pub fn with_field(mut self, name: &str, value: NeoValue) -> Self {
28 self.fields.push((name.to_string(), value));
29 self
30 }
31
32 pub fn get_field(&self, name: &str) -> Option<&NeoValue> {
33 self.fields
34 .iter()
35 .find(|(field_name, _)| field_name == name)
36 .map(|(_, value)| value)
37 }
38
39 pub fn set_field(&mut self, name: &str, value: NeoValue) {
40 if let Some((_, field_value)) = self
41 .fields
42 .iter_mut()
43 .find(|(field_name, _)| field_name == name)
44 {
45 *field_value = value;
46 } else {
47 self.fields.push((name.to_string(), value));
48 }
49 }
50
51 pub fn insert(&mut self, name: NeoString, value: NeoValue) {
52 self.set_field(name.as_str(), value);
53 }
54
55 pub fn len(&self) -> usize {
56 self.fields.len()
57 }
58
59 pub fn is_empty(&self) -> bool {
60 self.fields.is_empty()
61 }
62
63 pub fn iter(&self) -> impl Iterator<Item = (&str, &NeoValue)> {
64 self.fields
65 .iter()
66 .map(|(name, value)| (name.as_str(), value))
67 }
68}
69
70impl Default for NeoStruct {
71 fn default() -> Self {
72 Self::new()
73 }
74}
75
76#[derive(Debug, Clone, PartialEq, Eq)]
78#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
79pub enum NeoValue {
80 Integer(NeoInteger),
81 Boolean(NeoBoolean),
82 ByteString(NeoByteString),
83 String(NeoString),
84 Array(NeoArray<NeoValue>),
85 Map(NeoMap<NeoValue, NeoValue>),
86 Struct(NeoStruct),
87 Null,
88}
89
90impl NeoValue {
91 pub fn is_null(&self) -> bool {
92 matches!(self, NeoValue::Null)
93 }
94
95 pub fn as_integer(&self) -> Option<NeoInteger> {
96 match self {
97 NeoValue::Integer(i) => Some(i.clone()),
98 _ => None,
99 }
100 }
101
102 pub fn as_boolean(&self) -> Option<NeoBoolean> {
103 match self {
104 NeoValue::Boolean(b) => Some(*b),
105 _ => None,
106 }
107 }
108
109 pub fn as_byte_string(&self) -> Option<&NeoByteString> {
110 match self {
111 NeoValue::ByteString(bs) => Some(bs),
112 _ => None,
113 }
114 }
115
116 pub fn as_string(&self) -> Option<&NeoString> {
117 match self {
118 NeoValue::String(s) => Some(s),
119 _ => None,
120 }
121 }
122
123 pub fn as_array(&self) -> Option<&NeoArray<NeoValue>> {
124 match self {
125 NeoValue::Array(a) => Some(a),
126 _ => None,
127 }
128 }
129
130 pub fn as_map(&self) -> Option<&NeoMap<NeoValue, NeoValue>> {
131 match self {
132 NeoValue::Map(m) => Some(m),
133 _ => None,
134 }
135 }
136
137 pub fn as_struct(&self) -> Option<&NeoStruct> {
138 match self {
139 NeoValue::Struct(s) => Some(s),
140 _ => None,
141 }
142 }
143}
144
145impl From<NeoInteger> for NeoValue {
146 fn from(value: NeoInteger) -> Self {
147 NeoValue::Integer(value)
148 }
149}
150
151impl From<NeoBoolean> for NeoValue {
152 fn from(value: NeoBoolean) -> Self {
153 NeoValue::Boolean(value)
154 }
155}
156
157impl From<NeoByteString> for NeoValue {
158 fn from(value: NeoByteString) -> Self {
159 NeoValue::ByteString(value)
160 }
161}
162
163impl From<NeoString> for NeoValue {
164 fn from(value: NeoString) -> Self {
165 NeoValue::String(value)
166 }
167}
168
169impl From<NeoArray<NeoValue>> for NeoValue {
170 fn from(value: NeoArray<NeoValue>) -> Self {
171 NeoValue::Array(value)
172 }
173}
174
175impl From<NeoMap<NeoValue, NeoValue>> for NeoValue {
176 fn from(value: NeoMap<NeoValue, NeoValue>) -> Self {
177 NeoValue::Map(value)
178 }
179}
180
181impl From<NeoStruct> for NeoValue {
182 fn from(value: NeoStruct) -> Self {
183 NeoValue::Struct(value)
184 }
185}