diff options
| author | metamuffin <metamuffin@disroot.org> | 2026-02-19 23:11:18 +0100 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2026-02-19 23:11:18 +0100 |
| commit | 2b57e045de6f4a588f1aea58a5d616199dec4cfb (patch) | |
| tree | 9171e1b80004ea68bf4b06b5bac31f0c5a87c935 /common/object | |
| parent | 768688e34073e7430d92293fb0a995c7dc24cdf5 (diff) | |
| download | jellything-2b57e045de6f4a588f1aea58a5d616199dec4cfb.tar jellything-2b57e045de6f4a588f1aea58a5d616199dec4cfb.tar.bz2 jellything-2b57e045de6f4a588f1aea58a5d616199dec4cfb.tar.zst | |
query parser
Diffstat (limited to 'common/object')
| -rw-r--r-- | common/object/src/lib.rs | 2 | ||||
| -rw-r--r-- | common/object/src/path.rs | 18 | ||||
| -rw-r--r-- | common/object/src/value.rs | 35 |
3 files changed, 51 insertions, 4 deletions
diff --git a/common/object/src/lib.rs b/common/object/src/lib.rs index 3e60d58..c057163 100644 --- a/common/object/src/lib.rs +++ b/common/object/src/lib.rs @@ -3,7 +3,7 @@ which is licensed under the GNU Affero General Public License (version 3); see /COPYING. Copyright (C) 2026 metamuffin <metamuffin.org> */ -#![feature(iter_array_chunks)] +#![feature(iter_array_chunks, strip_circumfix)] mod buffer; pub mod debug; diff --git a/common/object/src/path.rs b/common/object/src/path.rs index 0751ff0..4779cd5 100644 --- a/common/object/src/path.rs +++ b/common/object/src/path.rs @@ -5,7 +5,7 @@ */ use crate::{Object, Tag, TypedTag}; -use std::{fmt::Display, marker::PhantomData}; +use std::{fmt::Display, marker::PhantomData, str::FromStr}; #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] pub struct Path(pub Vec<Tag>); @@ -48,3 +48,19 @@ impl Display for Path { Ok(()) } } +impl FromStr for Path { + type Err = &'static str; + fn from_str(s: &str) -> Result<Self, Self::Err> { + Ok(Self( + s.split(".") + .map(|e| { + e.as_bytes() + .try_into() + .map_err(|_| "path component not 4 bytes") + .map(Tag::new) + }) + .collect::<Result<Vec<Tag>, &'static str>>()?, + )) + } +} + diff --git a/common/object/src/value.rs b/common/object/src/value.rs index 3db7e7c..8371a79 100644 --- a/common/object/src/value.rs +++ b/common/object/src/value.rs @@ -5,7 +5,7 @@ */ use crate::{Object, ObjectBuffer, Tag}; -use std::borrow::Cow; +use std::{borrow::Cow, fmt::Display, str::FromStr}; pub trait ValueLoad<'a>: ValueStore + Sized { const ALIGNED: bool; @@ -54,7 +54,7 @@ impl ValueType { } } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, PartialEq)] pub enum Value<'a> { Tag(Tag), U32(u32), @@ -95,6 +95,37 @@ impl From<Tag> for Value<'static> { } } +impl Display for Value<'_> { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + Value::Tag(tag) => write!(f, "{tag}"), + Value::U32(x) => write!(f, "{x}"), + Value::U64(x) => write!(f, "{x}"), + Value::I64(x) => write!(f, "{x}"), + Value::String(x) => write!(f, "{x:?}"), + Value::Binary(x) => write!(f, "{x:?}"), + } + } +} +impl FromStr for Value<'static> { + type Err = &'static str; + fn from_str(s: &str) -> Result<Self, Self::Err> { + Ok(if s.len() == 4 { + Value::Tag(Tag::new(s.as_bytes().try_into().unwrap())) + } else if let Some(s) = s.strip_suffix("i64") { + Value::I64(s.parse().map_err(|_| "invalid i64 literal")?) + } else if let Some(s) = s.strip_suffix("u64") { + Value::U64(s.parse().map_err(|_| "invalid u64 literal")?) + } else if let Some(s) = s.strip_suffix("u32") { + Value::U32(s.parse().map_err(|_| "invalid u32 literal")?) + } else if let Some(s) = s.strip_circumfix("\"", "\"") { + Value::String(s.to_owned().into()) + } else { + return Err("invalid value literal"); + }) + } +} + pub trait ValueStore { fn get_type(&self) -> ValueType; fn store_aligned(&self, _buf: &mut Vec<u32>) {} |