1pub use semilattice_database_session::{SearchResult, SessionSearchResult};
2
3use indexmap::IndexMap;
4use serde::Serialize;
5use std::sync::Arc;
6
7#[derive(Debug, Clone, PartialEq)]
8pub enum WildDocValue {
9 Null,
10 Bool(bool),
11 Number(serde_json::Number),
12 String(Arc<String>),
13 Array(Vec<WildDocValue>),
14 Object(Vars),
15 Binary(Vec<u8>),
16 SearchResult(Arc<SearchResult>),
17 SessionSearchResult(Arc<SessionSearchResult>),
18}
19pub type Vars = IndexMap<Arc<String>, WildDocValue>;
20
21impl Serialize for WildDocValue {
22 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
23 where
24 S: serde::Serializer,
25 {
26 match self {
27 Self::Null => serializer.serialize_none(),
28 Self::Bool(v) => v.serialize(serializer),
29 Self::Number(v) => v.serialize(serializer),
30 Self::String(v) => v.serialize(serializer),
31 Self::Array(v) => v.serialize(serializer),
32 Self::Object(v) => v.serialize(serializer),
33 Self::Binary(v) => v.serialize(serializer),
34 Self::SearchResult(_v) => "SearchResult".serialize(serializer), Self::SessionSearchResult(_v) => "SessionSearchResult".serialize(serializer),
36 }
37 }
38}
39
40impl From<serde_json::Value> for WildDocValue {
41 fn from(value: serde_json::Value) -> Self {
42 match value {
43 serde_json::Value::Null => Self::Null,
44 serde_json::Value::Bool(v) => Self::Bool(v),
45 serde_json::Value::Number(v) => Self::Number(v),
46 serde_json::Value::String(v) => Self::String(Arc::new(v)),
47 serde_json::Value::Array(v) => {
48 Self::Array(v.into_iter().map(|v| Self::from(v)).collect())
49 }
50 serde_json::Value::Object(v) => Self::Object(
51 v.into_iter()
52 .map(|(k, v)| (Arc::new(k), Self::from(v)))
53 .collect(),
54 ),
55 }
56 }
57}
58impl From<serde_json::Number> for WildDocValue {
59 fn from(value: serde_json::Number) -> Self {
60 Self::Number(value)
61 }
62}
63
64impl std::fmt::Display for WildDocValue {
65 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
66 match self {
67 Self::Null => {
68 write!(f, "null")?;
69 Ok(())
70 }
71 Self::Bool(v) => v.fmt(f),
72 Self::Number(v) => v.fmt(f),
73 Self::String(v) => v.fmt(f),
74 Self::Array(v) => {
75 write!(f, "[")?;
76 let mut iter = v.into_iter();
77 if let Some(i) = iter.next() {
78 i.fmt(f)?;
79 }
80 for i in iter {
81 write!(f, " , ")?;
82 i.fmt(f)?;
83 }
84 write!(f, "]")
85 }
86 Self::Object(v) => {
87 write!(f, "{{")?;
88 let mut iter = v.into_iter();
89 if let Some((k, v)) = iter.next() {
90 write!(f, "\"{}\" : ", k)?;
91 v.fmt(f)?;
92 }
93 for (k, v) in iter {
94 write!(f, " , \"{}\" : ", k)?;
95 v.fmt(f)?;
96 }
97 write!(f, "}}")
98 }
99 Self::Binary(v) => {
100 write!(f, "{:?}", v)
101 }
102 Self::SearchResult(v) => {
103 write!(f, "{:?}", v)
104 }
105 Self::SessionSearchResult(v) => {
106 write!(f, "{:?}", v)
107 }
108 }
109 }
110}
111
112impl WildDocValue {
113 #[inline(always)]
114 pub fn as_string(&self) -> Arc<String> {
115 match self {
116 Self::String(s) => Arc::clone(s),
117 Self::Binary(value) => Arc::new(unsafe { std::str::from_utf8_unchecked(value) }.into()),
118 _ => Arc::new(self.to_string()),
119 }
120 }
121
122 #[inline(always)]
123 pub fn is_object(&self) -> bool {
124 match self {
125 Self::Object(_) => true,
126 _ => false,
127 }
128 }
129
130 #[inline(always)]
131 pub fn is_null(&self) -> bool {
132 match self {
133 Self::Null => true,
134 _ => false,
135 }
136 }
137
138 #[inline(always)]
139 pub fn as_bool(&self) -> Option<&bool> {
140 match self {
141 Self::Bool(v) => Some(v),
142 _ => None,
143 }
144 }
145}