BRIN/src/ir/mod.rs
2025-07-21 06:50:35 +02:00

87 lines
2.3 KiB
Rust

//use nanoid::nanoid;
use std::borrow::Cow;
use crate::grammar::Range;
/* Any format will be parsed into an intermediate, json-like object.
Where possible, zero-copying should be done (???!);
Each object consists of an ordered list of fields:
- Immediate fields, which can be accessed immediately
- Dependant fields, that depend on possibly another field.
Each field has:
- A UUID to uniquely identify the field
- A type and a length, which includes endianess information.
We might have a partial deserialization. In this case
the concrete value of a field will be unknown and what info
is needed to retrive it must be shown.
We might have different options in deserializing.
In this case we must return all possilbe valid results.
*/
/// Tag to uniquely represent a particular field
pub type FieldTag = &'static str;
/// Primitive value types
#[derive(PartialEq, Debug)]
pub(crate) enum Atom<'a> {
/// Bottom
Unknown {
depends_on: FieldTag,
},
Bool(bool),
// unsigned numbers
U8(u8),
U16(u16),
U32(u32),
U64(u64),
U128(u128),
UX(usize, Cow<'a, [u8]>),
// signed numbers
I8(i8),
I16(i16),
I32(i32),
I64(i64),
I128(i128),
IX(usize, Cow<'a, [u8]>),
// float
F32(f32),
F64(f64),
// strings
Utf8(Cow<'a, str>),
}
pub type OwnedAtom = Atom<'static>;
impl<'a> Atom<'a> {
fn to_static(self) -> OwnedAtom {
match self {
Atom::Unknown { depends_on } => todo!(),
Atom::Utf8(cow) => Atom::Utf8(cow.into_owned().into()),
Atom::UX(v, cow) => Atom::UX(v, cow.into_owned().into()),
Atom::IX(v, cow) => Atom::IX(v, cow.into_owned().into()),
Atom::Bool(b) => Atom::Bool(b),
Atom::U8(v) => Atom::U8(v),
Atom::U16(v) => Atom::U16(v),
Atom::U32(v) => Atom::U32(v),
Atom::U64(v) => Atom::U64(v),
Atom::U128(v) => Atom::U128(v),
Atom::I8(v) => Atom::I8(v),
Atom::I16(v) => Atom::I16(v),
Atom::I32(v) => Atom::I32(v),
Atom::I64(v) => Atom::I64(v),
Atom::I128(v) => Atom::I128(v),
Atom::F32(v) => Atom::F32(v),
Atom::F64(v) => Atom::F64(v),
}
}
}
pub struct ParsedField<'a> {
span: Range,
name: FieldTag,
value: Atom<'a>,
}