71 lines
1.9 KiB
Rust
71 lines
1.9 KiB
Rust
mod decode;
|
|
|
|
use std::collections::HashMap;
|
|
|
|
pub use decode::*;
|
|
|
|
#[derive(Eq, PartialEq)]
|
|
pub enum Term<'a> {
|
|
Str(&'a [u8]),
|
|
Dict(&'a [u8], HashMap<&'a [u8], Term<'a>>),
|
|
List(&'a [u8], Vec<NonListTerm<'a>>),
|
|
}
|
|
|
|
#[derive(Eq, PartialEq)]
|
|
pub enum NonListTerm<'a> {
|
|
Str(&'a [u8]),
|
|
Dict(&'a [u8], HashMap<&'a [u8], Term<'a>>),
|
|
}
|
|
|
|
impl<'a> From<NonListTerm<'a>> for Term<'a> {
|
|
fn from(x: NonListTerm<'a>) -> Term<'a> {
|
|
match x {
|
|
NonListTerm::Str(s) => Term::Str(s),
|
|
NonListTerm::Dict(raw, d) => Term::Dict(raw, d),
|
|
}
|
|
}
|
|
}
|
|
|
|
// ----
|
|
|
|
pub fn debug<'a>(x: &'a [u8]) -> &'a str {
|
|
std::str::from_utf8(x).unwrap_or("<invalid ascii>")
|
|
}
|
|
|
|
impl<'a> std::fmt::Debug for Term<'a> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
match self {
|
|
Term::Str(s) => write!(f, "Str(`{}`)", debug(s)),
|
|
Term::Dict(raw, d) => {
|
|
write!(f, "Dict<`{}`", debug(raw))?;
|
|
for (k, v) in d.iter() {
|
|
write!(f, "\n `{}`={:?}", debug(k), v)?;
|
|
}
|
|
write!(f, ">")
|
|
}
|
|
Term::List(raw, l) => {
|
|
write!(f, "List[`{}`", debug(raw))?;
|
|
for i in l.iter() {
|
|
write!(f, "\n {:?}", i)?;
|
|
}
|
|
write!(f, "]")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl<'a> std::fmt::Debug for NonListTerm<'a> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
match self {
|
|
NonListTerm::Str(s) => write!(f, "Str(`{}`)", debug(s)),
|
|
NonListTerm::Dict(raw, d) => {
|
|
write!(f, "Dict<`{}`", debug(raw))?;
|
|
for (k, v) in d.iter() {
|
|
write!(f, "\n `{}`={:?}", debug(k), v)?;
|
|
}
|
|
write!(f, ">")
|
|
}
|
|
}
|
|
}
|
|
}
|