aboutsummaryrefslogtreecommitdiff
path: root/common/object/src/path.rs
blob: 0751ff0d7927481df84b3d8ad14ac910293b9d8d (plain)
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
/*
    This file is part of jellything (https://codeberg.org/metamuffin/jellything)
    which is licensed under the GNU Affero General Public License (version 3); see /COPYING.
    Copyright (C) 2026 metamuffin <metamuffin.org>
*/

use crate::{Object, Tag, TypedTag};
use std::{fmt::Display, marker::PhantomData};

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Path(pub Vec<Tag>);

impl Path {
    pub fn get_matching_value<'a>(&self, ob: Object<'a>) -> Option<&'a [u8]> {
        fn recurse<'a>(ob: Object<'a>, path: &[Tag]) -> Option<&'a [u8]> {
            if path.len() > 1 {
                recurse(ob.get(TypedTag(path[0], PhantomData))?, path)
            } else {
                ob.get(TypedTag(path[0], PhantomData))
            }
        }
        recurse(ob, &self.0)
    }
    pub fn get_matching_values<'a>(&self, ob: Object<'a>) -> Vec<&'a [u8]> {
        fn recurse<'a>(ob: Object<'a>, out: &mut Vec<&'a [u8]>, path: &[Tag]) {
            if path.len() > 1 {
                for nested in ob.iter(TypedTag(path[0], PhantomData::<Object>)) {
                    recurse(nested, out, &path[1..]);
                }
            } else {
                out.extend(ob.iter(TypedTag(path[0], PhantomData::<&[u8]>)));
            }
        }
        let mut out = Vec::new();
        recurse(ob, &mut out, &self.0);
        out
    }
}

impl Display for Path {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        for (i, c) in self.0.iter().enumerate() {
            if i > 0 {
                write!(f, ".")?;
            }
            write!(f, "{c}")?;
        }
        Ok(())
    }
}