1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! Typed property and query values.
use std::fmt;
use serde::{Deserialize, Serialize};
use crate::error::DbError;
/// Supported scalar property types.
///
/// # Performance
///
/// Copying and comparing a type tag are `O(1)`.
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum PropertyType {
/// Boolean property.
Boolean,
/// Signed 64-bit integer property.
Integer,
/// UTF-8 text property.
Text,
}
/// One typed property value.
///
/// # Performance
///
/// Copying is `O(value length)` for text and `O(1)` otherwise.
#[derive(Clone, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum PropertyValue {
/// Boolean value.
Boolean(bool),
/// Signed integer value.
Integer(i64),
/// UTF-8 text value.
Text(String),
}
impl PropertyValue {
/// Returns this value's type tag.
///
/// # Performance
///
/// This function is `O(1)`.
#[must_use]
pub const fn value_type(&self) -> PropertyType {
match self {
Self::Boolean(_value) => PropertyType::Boolean,
Self::Integer(_value) => PropertyType::Integer,
Self::Text(_value) => PropertyType::Text,
}
}
/// Returns the text payload, or `None` for a non-text value.
///
/// # Performance
///
/// This function is `O(1)`.
#[must_use]
pub fn as_text(&self) -> Option<&str> {
match self {
Self::Text(value) => Some(value),
Self::Boolean(_) | Self::Integer(_) => None,
}
}
/// Returns the integer payload, or `None` for a non-integer value.
///
/// # Performance
///
/// This function is `O(1)`.
#[must_use]
pub const fn as_int(&self) -> Option<i64> {
match self {
Self::Integer(value) => Some(*value),
Self::Boolean(_) | Self::Text(_) => None,
}
}
/// Returns the integer payload narrowed to `usize`, or `None` when the value
/// is not an integer or falls outside `usize` range.
///
/// # Performance
///
/// This function is `O(1)`.
#[must_use]
pub fn as_count(&self) -> Option<usize> {
match self {
Self::Integer(value) => usize::try_from(*value).ok(),
Self::Boolean(_) | Self::Text(_) => None,
}
}
/// Returns the boolean payload, or `None` for a non-boolean value.
///
/// # Performance
///
/// This function is `O(1)`.
#[must_use]
pub const fn as_bool(&self) -> Option<bool> {
match self {
Self::Boolean(value) => Some(*value),
Self::Integer(_) | Self::Text(_) => None,
}
}
}
impl From<bool> for PropertyValue {
/// Wraps a boolean value.
///
/// # Performance
///
/// This function is `O(1)`.
fn from(value: bool) -> Self {
Self::Boolean(value)
}
}
impl From<i64> for PropertyValue {
/// Wraps a signed 64-bit integer value.
///
/// # Performance
///
/// This function is `O(1)`.
fn from(value: i64) -> Self {
Self::Integer(value)
}
}
impl From<&str> for PropertyValue {
/// Copies a string slice into an owned text value.
///
/// # Performance
///
/// This function is `O(value length)`.
fn from(value: &str) -> Self {
Self::Text(value.to_owned())
}
}
impl From<String> for PropertyValue {
/// Takes ownership of a string as a text value.
///
/// # Performance
///
/// This function is `O(1)`.
fn from(value: String) -> Self {
Self::Text(value)
}
}
impl TryFrom<u64> for PropertyValue {
type Error = DbError;
/// Narrows an unsigned 64-bit value into a signed [`PropertyValue::Integer`].
///
/// # Errors
///
/// Returns [`DbError::ValueOutOfRange`] when the value exceeds `i64::MAX`.
///
/// # Performance
///
/// This function is `O(1)`.
fn try_from(value: u64) -> Result<Self, Self::Error> {
i64::try_from(value)
.map(Self::Integer)
.map_err(|_overflow| DbError::ValueOutOfRange)
}
}
impl TryFrom<usize> for PropertyValue {
type Error = DbError;
/// Narrows a pointer-width unsigned value into a signed
/// [`PropertyValue::Integer`].
///
/// # Errors
///
/// Returns [`DbError::ValueOutOfRange`] when the value exceeds `i64::MAX`.
///
/// # Performance
///
/// This function is `O(1)`.
fn try_from(value: usize) -> Result<Self, Self::Error> {
i64::try_from(value)
.map(Self::Integer)
.map_err(|_overflow| DbError::ValueOutOfRange)
}
}
impl fmt::Display for PropertyValue {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Boolean(value) => write!(formatter, "{value}"),
Self::Integer(value) => write!(formatter, "{value}"),
Self::Text(value) => formatter.write_str(value),
}
}
}
/// Property value parser used by CLI, HTTP, `OxQL`.
///
/// # Errors
///
/// Returns the original token when it cannot be parsed as a supported value.
///
/// # Performance
///
/// This function is `O(token.len())`.
pub(crate) fn parse_value_token(token: &str) -> Result<PropertyValue, String> {
let trimmed = token.trim();
if trimmed == "true" {
return Ok(PropertyValue::Boolean(true));
}
if trimmed == "false" {
return Ok(PropertyValue::Boolean(false));
}
if let Ok(value) = trimmed.parse::<i64>() {
return Ok(PropertyValue::Integer(value));
}
parse_quoted(trimmed).map(PropertyValue::Text)
}
/// Parses one single- or double-quoted token.
fn parse_quoted(token: &str) -> Result<String, String> {
let single = token
.strip_prefix('\'')
.and_then(|text| text.strip_suffix('\''));
let double = token
.strip_prefix('"')
.and_then(|text| text.strip_suffix('"'));
single
.or(double)
.map_or_else(|| Err(token.to_owned()), |value| Ok(value.to_owned()))
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use super::*;
#[test]
fn from_scalars_roundtrip_through_accessors() {
assert_eq!(PropertyValue::from(true).as_bool(), Some(true));
assert_eq!(PropertyValue::from(7_i64).as_int(), Some(7));
assert_eq!(PropertyValue::from("hi").as_text(), Some("hi"));
assert_eq!(
PropertyValue::from(String::from("hi")).as_text(),
Some("hi")
);
}
#[test]
fn accessors_reject_mismatched_types() {
let text = PropertyValue::from("x");
assert_eq!(text.as_int(), None);
assert_eq!(text.as_bool(), None);
assert_eq!(text.as_count(), None);
}
proptest! {
#[test]
fn integer_roundtrips(value in any::<i64>()) {
prop_assert_eq!(PropertyValue::from(value).as_int(), Some(value));
}
#[test]
fn boolean_roundtrips(value in any::<bool>()) {
prop_assert_eq!(PropertyValue::from(value).as_bool(), Some(value));
}
#[test]
fn text_roundtrips(value in ".*") {
let parsed = PropertyValue::from(value.as_str());
prop_assert_eq!(parsed.as_text(), Some(value.as_str()));
}
#[test]
fn try_from_u64_matches_checked_narrowing(value in any::<u64>()) {
match i64::try_from(value) {
Ok(expected) => prop_assert_eq!(
PropertyValue::try_from(value).ok().and_then(|parsed| parsed.as_int()),
Some(expected)
),
Err(_overflow) => prop_assert!(PropertyValue::try_from(value).is_err()),
}
}
#[test]
fn as_count_matches_checked_conversion(value in any::<i64>()) {
prop_assert_eq!(PropertyValue::Integer(value).as_count(), usize::try_from(value).ok());
}
}
}