diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-01-14 20:28:54 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-01-14 20:28:54 +0100 |
| commit | abf25f340c11111369b69c13c34d8fed9d4f0da8 (patch) | |
| tree | 1e913a4affe3303a17a222c8215a51424d3b71b6 /common/object/src/path.rs | |
| parent | b5ff460e938779be4eeab292c2cc1d436b93c137 (diff) | |
| download | jellything-abf25f340c11111369b69c13c34d8fed9d4f0da8.tar jellything-abf25f340c11111369b69c13c34d8fed9d4f0da8.tar.bz2 jellything-abf25f340c11111369b69c13c34d8fed9d4f0da8.tar.zst | |
db binning and sorts
Diffstat (limited to 'common/object/src/path.rs')
| -rw-r--r-- | common/object/src/path.rs | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/common/object/src/path.rs b/common/object/src/path.rs new file mode 100644 index 0000000..7db8798 --- /dev/null +++ b/common/object/src/path.rs @@ -0,0 +1,38 @@ +/* + 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::marker::PhantomData; + +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +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 + } +} |