1use std::fmt;
9
10#[derive(Debug, Clone, PartialEq)]
14pub enum JsonValue {
15 Null,
16 Bool(bool),
17 Number(f64),
18 String(String),
19 Array(Vec<JsonValue>),
20 Object(Vec<(String, JsonValue)>),
21}
22
23impl JsonValue {
24 pub fn null() -> Self {
25 JsonValue::Null
26 }
27
28 pub fn bool(b: bool) -> Self {
29 JsonValue::Bool(b)
30 }
31
32 pub fn number(n: impl Into<f64>) -> Self {
33 JsonValue::Number(n.into())
34 }
35
36 pub fn string(s: impl Into<String>) -> Self {
37 JsonValue::String(s.into())
38 }
39
40 pub fn object<I, K>(entries: I) -> Self
41 where
42 I: IntoIterator<Item = (K, JsonValue)>,
43 K: Into<String>,
44 {
45 JsonValue::Object(entries.into_iter().map(|(k, v)| (k.into(), v)).collect())
46 }
47
48 pub fn array<I>(items: I) -> Self
49 where
50 I: IntoIterator<Item = JsonValue>,
51 {
52 JsonValue::Array(items.into_iter().collect())
53 }
54
55 pub fn as_object(&self) -> Option<&[(String, JsonValue)]> {
56 match self {
57 JsonValue::Object(entries) => Some(entries.as_slice()),
58 _ => None,
59 }
60 }
61
62 pub fn to_json_string(&self) -> String {
63 let mut out = String::new();
64 write_json(self, &mut out);
65 out
66 }
67}
68
69fn write_json(value: &JsonValue, out: &mut String) {
70 match value {
71 JsonValue::Null => out.push_str("null"),
72 JsonValue::Bool(b) => out.push_str(if *b { "true" } else { "false" }),
73 JsonValue::Number(n) => {
74 if n.fract() == 0.0 && n.is_finite() {
75 out.push_str(&format!("{}", *n as i64));
76 } else {
77 out.push_str(&format!("{n}"));
78 }
79 }
80 JsonValue::String(s) => {
81 out.push('"');
82 for c in s.chars() {
83 match c {
84 '"' => out.push_str("\\\""),
85 '\\' => out.push_str("\\\\"),
86 '\n' => out.push_str("\\n"),
87 '\r' => out.push_str("\\r"),
88 '\t' => out.push_str("\\t"),
89 c if (c as u32) < 0x20 => {
90 out.push_str(&format!("\\u{:04x}", c as u32));
91 }
92 c => out.push(c),
93 }
94 }
95 out.push('"');
96 }
97 JsonValue::Array(items) => {
98 out.push('[');
99 for (i, item) in items.iter().enumerate() {
100 if i > 0 {
101 out.push(',');
102 }
103 write_json(item, out);
104 }
105 out.push(']');
106 }
107 JsonValue::Object(entries) => {
108 out.push('{');
109 for (i, (k, v)) in entries.iter().enumerate() {
110 if i > 0 {
111 out.push(',');
112 }
113 write_json(&JsonValue::String(k.clone()), out);
114 out.push(':');
115 write_json(v, out);
116 }
117 out.push('}');
118 }
119 }
120}
121
122#[derive(Debug, Clone, PartialEq)]
125pub enum ValueOut {
126 Null,
127 Bool(bool),
128 Integer(i64),
129 Float(f64),
130 String(String),
131}
132
133impl fmt::Display for ValueOut {
134 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
135 match self {
136 ValueOut::Null => f.write_str("null"),
137 ValueOut::Bool(b) => write!(f, "{b}"),
138 ValueOut::Integer(n) => write!(f, "{n}"),
139 ValueOut::Float(n) => write!(f, "{n}"),
140 ValueOut::String(s) => write!(f, "{s}"),
141 }
142 }
143}
144
145#[derive(Debug, Clone)]
148pub struct QueryResult {
149 pub statement: String,
150 pub affected: u64,
151 pub columns: Vec<String>,
152 pub rows: Vec<Vec<(String, ValueOut)>>,
153}
154
155#[derive(Debug, Clone, PartialEq)]
156pub struct KvWatchEvent {
157 pub key: String,
158 pub op: String,
159 pub before: serde_json::Value,
160 pub after: serde_json::Value,
161 pub lsn: u64,
162 pub committed_at: u64,
163 pub dropped_event_count: u64,
164}
165
166#[cfg(any(feature = "redwire", feature = "http"))]
167impl QueryResult {
168 pub fn from_envelope(value: serde_json::Value) -> Self {
174 let obj = value.as_object().cloned().unwrap_or_default();
175 let statement = obj
176 .get("statement")
177 .and_then(|v| v.as_str())
178 .unwrap_or("")
179 .to_string();
180 let affected = obj.get("affected").and_then(|v| v.as_u64()).unwrap_or(0);
181 Self {
182 statement,
183 affected,
184 columns: Vec::new(),
185 rows: Vec::new(),
186 }
187 }
188}
189
190#[derive(Debug, Clone)]
191pub struct InsertResult {
192 pub affected: u64,
193 pub id: Option<String>,
195}