olai-store 0.0.6

Generic, TAO-inspired object and association store with field-role enforcement
Documentation
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Hierarchical resource names and their dotted, backtick-escaped string form.
//!
//! A [`ResourceName`] is a path of field-name segments (`["catalog", "schema", "table"]`)
//! identifying a resource within a taxonomy's hierarchy. It round-trips to and from a dotted
//! string via [`Display`] and [`FromStr`](std::str::FromStr): segments join with `.`, and a segment containing a
//! dot, whitespace, other non-alphanumeric character, or a leading digit is wrapped in backticks
//! (`` ` ``) with any literal backtick doubled — the same quoting rule SQL uses for identifiers.
//! [`parse_column_name_list`](ResourceName::parse_column_name_list) parses a comma-separated
//! list of such names.

use crate::{Error, Result};

use std::borrow::Borrow;
use std::fmt::{Display, Formatter};
use std::hash::{Hash, Hasher};
use std::iter::Peekable;
use std::ops::Deref;
use std::sync::LazyLock;

use serde::{Deserialize, Serialize};

/// A shared, empty [`ResourceName`] (a zero-segment path).
///
/// Useful as a default or as the root of a hierarchy without allocating a fresh empty name.
pub static EMPTY_RESOURCE_NAME: LazyLock<ResourceName> =
    LazyLock::new(|| ResourceName::new(&[] as &[String]));

/// A (possibly nested) resource name: a path of field-name segments.
///
/// Segments are ordered outermost-first (`["catalog", "schema", "table"]`). The name derefs to
/// `&[String]`, and [`Display`]/[`FromStr`](std::str::FromStr) convert to and from the dotted, backtick-escaped
/// string form described in the [module docs](self).
///
/// # Examples
///
/// ```
/// use olai_store::ResourceName;
///
/// let name: ResourceName = "catalog.schema.table".parse().unwrap();
/// assert_eq!(name.path(), ["catalog", "schema", "table"]);
/// assert_eq!(name.to_string(), "catalog.schema.table");
///
/// // A segment needing quoting round-trips through backticks.
/// let quoted = ResourceName::new(["my catalog", "t.1"]);
/// assert_eq!(quoted.to_string(), "`my catalog`.`t.1`");
/// assert_eq!(quoted.to_string().parse::<ResourceName>().unwrap(), quoted);
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "sqlx", derive(sqlx::Type))]
#[cfg_attr(feature = "sqlx", sqlx(transparent, no_pg_array))]
pub struct ResourceName(Vec<String>);

impl ResourceName {
    /// Creates a new resource name from an iterator of field names.
    pub fn new<A>(iter: impl IntoIterator<Item = A>) -> Self
    where
        Self: FromIterator<A>,
    {
        iter.into_iter().collect()
    }

    /// Naively splits a string at dots to create a resource name.
    ///
    /// This method is _NOT_ recommended for production use, as it does not attempt to interpret
    /// special characters in field names.
    pub fn from_naive_str_split(name: impl AsRef<str>) -> Self {
        Self::new(name.as_ref().split(FIELD_SEPARATOR))
    }

    /// Parses a comma-separated list of resource names, properly accounting for escapes and special
    /// characters.
    ///
    /// Surrounding whitespace around each name is ignored; an empty input yields an empty list.
    ///
    /// # Errors
    ///
    /// Returns [`Error::Generic`] if any name is malformed — for example an
    /// unterminated backtick quote or an invalid character following a segment.
    ///
    /// [`Error::Generic`]: crate::Error::Generic
    pub fn parse_column_name_list(names: impl AsRef<str>) -> Result<Vec<ResourceName>> {
        let names = names.as_ref();
        let chars = &mut names.chars().peekable();

        drop_leading_whitespace(chars);
        let mut ending = match chars.peek() {
            Some(_) => FieldEnding::NextColumn,
            None => FieldEnding::InputExhausted,
        };

        let mut cols = vec![];
        while ending == FieldEnding::NextColumn {
            let (col, new_ending) = parse_resource_name(chars)?;
            cols.push(col);
            ending = new_ending;
        }
        Ok(cols)
    }

    /// Joins this name with another, concatenating their fields into a single nested path.
    pub fn join(&self, right: &ResourceName) -> ResourceName {
        [self.clone(), right.clone()].into_iter().collect()
    }

    /// The path of field names for this resource name.
    pub fn path(&self) -> &[String] {
        &self.0
    }

    /// Consumes this resource name and returns the path of field names.
    pub fn into_inner(self) -> Vec<String> {
        self.0
    }

    /// Returns `true` if this name starts with the given prefix.
    ///
    /// The comparison is segment-wise, not by string prefix: a name matches `prefix` when its
    /// first `prefix.len()` segments equal `prefix`'s segments. An empty prefix matches every name.
    /// Used to scope listings to a namespace subtree.
    ///
    /// # Examples
    ///
    /// ```
    /// use olai_store::ResourceName;
    ///
    /// let name = ResourceName::new(["catalog", "schema", "table"]);
    /// assert!(name.prefix_matches(&ResourceName::new(["catalog", "schema"])));
    /// assert!(!name.prefix_matches(&ResourceName::new(["catalog", "other"])));
    /// ```
    ///
    /// Segments are compared case-insensitively for ASCII, matching the
    /// `COLLATE NOCASE` folding the SQLite backend applies to the `name` column
    /// (so namespace prefix listing agrees with `get_by_name` equality). Case
    /// folding is ASCII-only; non-ASCII segments compare byte-for-byte.
    ///
    /// ```
    /// use olai_store::ResourceName;
    ///
    /// let name = ResourceName::new(["Catalog", "Schema", "table"]);
    /// assert!(name.prefix_matches(&ResourceName::new(["catalog", "schema"])));
    /// ```
    pub fn prefix_matches(&self, prefix: &ResourceName) -> bool {
        if self.len() < prefix.len() {
            return false;
        }
        prefix
            .iter()
            .zip(self.iter())
            .all(|(a, b)| a.eq_ignore_ascii_case(b))
    }

    /// Compare two resource names for equality, folding ASCII case per segment.
    ///
    /// This mirrors the `COLLATE NOCASE` folding the SQLite backend applies to
    /// the `name` column, so in-memory name lookups agree with the persistent
    /// backend: `Catalog` and `catalog` are the same name. Case folding is
    /// ASCII-only; non-ASCII segments compare byte-for-byte.
    ///
    /// # Examples
    ///
    /// ```
    /// use olai_store::ResourceName;
    ///
    /// assert!(ResourceName::new(["Catalog"]).eq_ignore_ascii_case(&ResourceName::new(["catalog"])));
    /// assert!(!ResourceName::new(["a", "b"]).eq_ignore_ascii_case(&ResourceName::new(["a"])));
    /// ```
    pub fn eq_ignore_ascii_case(&self, other: &ResourceName) -> bool {
        self.len() == other.len()
            && self
                .iter()
                .zip(other.iter())
                .all(|(a, b)| a.eq_ignore_ascii_case(b))
    }
}

impl<A: Into<String>> FromIterator<A> for ResourceName {
    fn from_iter<T: IntoIterator<Item = A>>(iter: T) -> Self {
        let path = iter.into_iter().map(|s| s.into()).collect();
        Self(path)
    }
}

impl From<Vec<String>> for ResourceName {
    fn from(path: Vec<String>) -> Self {
        Self(path)
    }
}

impl FromIterator<ResourceName> for ResourceName {
    fn from_iter<T: IntoIterator<Item = ResourceName>>(iter: T) -> Self {
        let path = iter.into_iter().flat_map(|c| c.into_iter()).collect();
        Self(path)
    }
}

impl IntoIterator for ResourceName {
    type Item = String;
    type IntoIter = std::vec::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        self.0.into_iter()
    }
}

impl Deref for ResourceName {
    type Target = [String];

    fn deref(&self) -> &[String] {
        &self.0
    }
}

impl Borrow<[String]> for ResourceName {
    fn borrow(&self) -> &[String] {
        self
    }
}

impl Borrow<[String]> for &ResourceName {
    fn borrow(&self) -> &[String] {
        self
    }
}

impl Hash for ResourceName {
    fn hash<H: Hasher>(&self, hasher: &mut H) {
        (**self).hash(hasher)
    }
}

impl Display for ResourceName {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        for (i, s) in self.iter().enumerate() {
            use std::fmt::Write as _;

            if i > 0 {
                f.write_char(FIELD_SEPARATOR)?;
            }

            let digit_char = |c: char| c.is_ascii_digit();
            if s.is_empty() || s.starts_with(digit_char) || s.contains(|c| !is_simple_char(c)) {
                f.write_char(FIELD_ESCAPE_CHAR)?;
                for c in s.chars() {
                    f.write_char(c)?;
                    if c == FIELD_ESCAPE_CHAR {
                        f.write_char(c)?;
                    }
                }
                f.write_char(FIELD_ESCAPE_CHAR)?;
            } else {
                f.write_str(s)?;
            }
        }
        Ok(())
    }
}

fn is_simple_char(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '_'
}

fn drop_leading_whitespace(iter: &mut Peekable<impl Iterator<Item = char>>) {
    while iter.next_if(|c| c.is_whitespace()).is_some() {}
}

impl std::str::FromStr for ResourceName {
    type Err = Error;

    /// Parses the dotted, backtick-escaped string form (see the [module docs](self)).
    ///
    /// # Errors
    ///
    /// Returns [`Error::Generic`] if the string is malformed — an
    /// unterminated backtick quote, an invalid character after a segment, or a trailing comma
    /// (parse a comma-separated list with
    /// [`parse_column_name_list`](ResourceName::parse_column_name_list) instead).
    ///
    /// [`Error::Generic`]: crate::Error::Generic
    fn from_str(s: &str) -> Result<Self> {
        match parse_resource_name(&mut s.chars().peekable())? {
            (_, FieldEnding::NextColumn) => Err(Error::generic("Trailing comma in column name")),
            (col, _) => Ok(col),
        }
    }
}

type Chars<'a> = Peekable<std::str::Chars<'a>>;

#[derive(PartialEq)]
enum FieldEnding {
    InputExhausted,
    NextField,
    NextColumn,
}

const FIELD_ESCAPE_CHAR: char = '`';
const FIELD_SEPARATOR: char = '.';
const COLUMN_SEPARATOR: char = ',';

fn parse_resource_name(chars: &mut Chars<'_>) -> Result<(ResourceName, FieldEnding)> {
    drop_leading_whitespace(chars);
    let mut ending = if chars.peek().is_none() {
        FieldEnding::InputExhausted
    } else if chars.next_if_eq(&COLUMN_SEPARATOR).is_some() {
        FieldEnding::NextColumn
    } else {
        FieldEnding::NextField
    };

    let mut path = vec![];
    while ending == FieldEnding::NextField {
        drop_leading_whitespace(chars);
        let field_name = match chars.next_if_eq(&FIELD_ESCAPE_CHAR) {
            Some(_) => parse_escaped_field_name(chars)?,
            None => parse_simple_field_name(chars)?,
        };

        ending = match chars.find(|c| !c.is_whitespace()) {
            None => FieldEnding::InputExhausted,
            Some(FIELD_SEPARATOR) => FieldEnding::NextField,
            Some(COLUMN_SEPARATOR) => FieldEnding::NextColumn,
            Some(other) => {
                return Err(Error::generic(format!(
                    "Invalid character {other:?} after field {field_name:?}",
                )));
            }
        };
        path.push(field_name);
    }
    Ok((ResourceName::new(path), ending))
}

fn parse_simple_field_name(chars: &mut Chars<'_>) -> Result<String> {
    let mut name = String::new();
    let mut first = true;
    while let Some(c) = chars.next_if(|c| is_simple_char(*c)) {
        if first && c.is_ascii_digit() {
            return Err(Error::generic(format!(
                "Unescaped field name cannot start with a digit {c:?}"
            )));
        }
        name.push(c);
        first = false;
    }
    Ok(name)
}

fn parse_escaped_field_name(chars: &mut Chars<'_>) -> Result<String> {
    let mut name = String::new();
    loop {
        match chars.next() {
            Some(FIELD_ESCAPE_CHAR) if chars.next_if_eq(&FIELD_ESCAPE_CHAR).is_none() => break,
            Some(c) => name.push(c),
            None => {
                return Err(Error::generic(format!(
                    "No closing {FIELD_ESCAPE_CHAR:?} after field {name:?}"
                )));
            }
        }
    }
    Ok(name)
}

#[cfg(test)]
mod test {
    use super::*;

    impl ResourceName {
        fn empty() -> Self {
            Self::new(&[] as &[String])
        }
    }

    #[test]
    fn test_column_name_methods() {
        let simple: ResourceName = "x".parse().unwrap();
        let nested: ResourceName = "x.y".parse().unwrap();

        assert_eq!(simple.path(), ["x"]);
        assert_eq!(nested.path(), ["x", "y"]);

        assert_eq!(simple.clone().into_inner(), ["x"]);
        assert_eq!(nested.clone().into_inner(), ["x", "y"]);

        let name: &[String] = &nested;
        assert_eq!(name, &["x", "y"]);

        let name: ResourceName = ["x", "y"].into_iter().collect();
        assert_eq!(name, nested);

        let name: ResourceName = [&nested, &simple].into_iter().cloned().collect();
        assert_eq!(name, ResourceName::new(["x", "y", "x"]));
    }

    #[test]
    fn test_column_name_from_str() {
        let cases = [
            ("", Some(ResourceName::empty())),
            (".", Some(ResourceName::new(["", ""]))),
            ("  .  ", Some(ResourceName::new(["", ""]))),
            (" ", Some(ResourceName::empty())),
            ("0", None),
            (".a", Some(ResourceName::new(["", "a"]))),
            ("a.", Some(ResourceName::new(["a", ""]))),
            ("  a  .  ", Some(ResourceName::new(["a", ""]))),
            ("a..b", Some(ResourceName::new(["a", "", "b"]))),
            ("`a", None),
            ("a`", None),
            ("a`b`", None),
            ("`a`b", None),
            ("`a``b`", Some(ResourceName::new(["a`b"]))),
            ("  `a``b`  ", Some(ResourceName::new(["a`b"]))),
            ("`a`` b`", Some(ResourceName::new(["a` b"]))),
            ("a", Some(ResourceName::new(["a"]))),
            ("a0", Some(ResourceName::new(["a0"]))),
            ("`a`", Some(ResourceName::new(["a"]))),
            ("  `a`  ", Some(ResourceName::new(["a"]))),
            ("` `", Some(ResourceName::new([" "]))),
            ("  ` `  ", Some(ResourceName::new([" "]))),
            ("`0`", Some(ResourceName::new(["0"]))),
            ("`.`", Some(ResourceName::new(["."]))),
            ("`.`.`.`", Some(ResourceName::new([".", "."]))),
            ("` `.` `", Some(ResourceName::new([" ", " "]))),
            ("a.b", Some(ResourceName::new(["a", "b"]))),
            ("a b", None),
            ("a.`b`", Some(ResourceName::new(["a", "b"]))),
            ("`a`.b", Some(ResourceName::new(["a", "b"]))),
            ("`a`.`b`", Some(ResourceName::new(["a", "b"]))),
            ("`a`.`b`.`c`", Some(ResourceName::new(["a", "b", "c"]))),
            ("`a``.`b```", None),
            ("`a```.`b``", None),
            ("`a```.`b```", Some(ResourceName::new(["a`", "b`"]))),
            ("`a.`b``.c`", None),
            ("`a.``b`.c`", None),
            ("`a.``b``.c`", Some(ResourceName::new(["a.`b`.c"]))),
            ("a`.b``", None),
        ];
        for (input, expected_output) in cases {
            let output: Result<ResourceName> = input.parse();
            match (&output, &expected_output) {
                (Ok(output), Some(expected_output)) => {
                    assert_eq!(output, expected_output, "from {input}")
                }
                (Err(_), None) => {}
                _ => panic!("Expected {input} to parse as {expected_output:?}, got {output:?}"),
            }
        }
    }

    #[test]
    fn test_column_name_to_string() {
        let cases = [
            ("", ResourceName::empty()),
            ("``.``", ResourceName::new(["", ""])),
            ("``.a", ResourceName::new(["", "a"])),
            ("a.``", ResourceName::new(["a", ""])),
            ("a.``.b", ResourceName::new(["a", "", "b"])),
            ("a", ResourceName::new(["a"])),
            ("a0", ResourceName::new(["a0"])),
            ("`a `", ResourceName::new(["a "])),
            ("` `", ResourceName::new([" "])),
            ("`0`", ResourceName::new(["0"])),
            ("`.`", ResourceName::new(["."])),
            ("`.`.`.`", ResourceName::new([".", "."])),
            ("` `.` `", ResourceName::new([" ", " "])),
            ("a.b", ResourceName::new(["a", "b"])),
            ("a.b.c", ResourceName::new(["a", "b", "c"])),
            ("a.`b.c`.d", ResourceName::new(["a", "b.c", "d"])),
            ("`a```.`b```", ResourceName::new(["a`", "b`"])),
        ];
        for (expected_output, input) in cases {
            let output = input.to_string();
            assert_eq!(output, expected_output);

            let parsed: ResourceName = output.parse().expect(&output);
            assert_eq!(parsed, input);
        }

        let cases = [
            ("  `a`  ", "a", ResourceName::new(["a"])),
            ("  `a0`  ", "a0", ResourceName::new(["a0"])),
            ("  `a`  .  `b`  ", "a.b", ResourceName::new(["a", "b"])),
        ];
        for (input, expected_output, expected_parsed) in cases {
            let parsed: ResourceName = input.parse().unwrap();
            assert_eq!(parsed, expected_parsed);
            assert_eq!(parsed.to_string(), expected_output);
        }
    }

    #[test]
    fn test_parse_column_name_list() {
        let cases = [
            ("", Some(vec![])),
            (
                "  ,  ",
                Some(vec![ResourceName::empty(), ResourceName::empty()]),
            ),
            ("  a  ", Some(vec![ResourceName::new(["a"])])),
            (
                "  ,  a  ",
                Some(vec![ResourceName::empty(), ResourceName::new(["a"])]),
            ),
            (
                "  a  ,  ",
                Some(vec![ResourceName::new(["a"]), ResourceName::empty()]),
            ),
            (
                "a  ,  b",
                Some(vec![ResourceName::new(["a"]), ResourceName::new(["b"])]),
            ),
            ("`a, b`", Some(vec![ResourceName::new(["a, b"])])),
            (
                "a.b, c",
                Some(vec![
                    ResourceName::new(["a", "b"]),
                    ResourceName::new(["c"]),
                ]),
            ),
        ];
        for (input, expected_output) in cases {
            let output = ResourceName::parse_column_name_list(input);
            match (&output, &expected_output) {
                (Ok(output), Some(expected_output)) => {
                    assert_eq!(output, expected_output, "from \"{input}\"")
                }
                (Err(_), None) => {}
                _ => panic!("Expected {input} to parse as {expected_output:?}, got {output:?}"),
            }
        }
    }
}