2022-11-17 21:53:36 +00:00
|
|
|
//! Functions to decode nettext and helpers to map it to data structures
|
|
|
|
|
2022-11-17 10:48:43 +00:00
|
|
|
mod decode;
|
2022-11-18 13:15:30 +00:00
|
|
|
mod error;
|
2022-11-17 10:48:43 +00:00
|
|
|
|
|
|
|
use std::collections::HashMap;
|
|
|
|
|
2022-11-22 13:56:58 +00:00
|
|
|
#[cfg(any(feature = "dryoc"))]
|
2022-11-17 21:53:36 +00:00
|
|
|
use crate::crypto;
|
2022-11-18 17:59:06 +00:00
|
|
|
|
2022-12-15 13:45:27 +00:00
|
|
|
use crate::debug;
|
|
|
|
|
2022-11-17 10:48:43 +00:00
|
|
|
pub use decode::*;
|
2022-11-18 15:26:44 +00:00
|
|
|
pub use error::*;
|
2022-11-17 10:48:43 +00:00
|
|
|
|
2022-11-17 15:35:06 +00:00
|
|
|
/// A parsed nettext term, with many helpers for destructuring
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// Lifetime 'a is the lifetime of the buffer containing the encoded data.
|
|
|
|
///
|
|
|
|
/// Lifetime 'b is the lifetime of another Term from which this one is borrowed, when it
|
|
|
|
/// is returned by one of the helper functions, or 'static when first returned from
|
|
|
|
/// `decode()`
|
|
|
|
#[derive(Eq, PartialEq, Debug)]
|
2022-11-18 15:26:44 +00:00
|
|
|
pub struct Term<'a, 'b>(pub(crate) AnyTerm<'a, 'b>);
|
2022-11-17 13:58:27 +00:00
|
|
|
|
|
|
|
#[derive(Eq, PartialEq, Clone)]
|
|
|
|
pub(crate) enum AnyTerm<'a, 'b> {
|
2022-11-17 10:48:43 +00:00
|
|
|
Str(&'a [u8]),
|
2022-11-17 13:58:27 +00:00
|
|
|
Dict(&'a [u8], HashMap<&'a [u8], AnyTerm<'a, 'b>>),
|
|
|
|
DictRef(&'a [u8], &'b HashMap<&'a [u8], AnyTerm<'a, 'b>>),
|
2022-12-15 13:45:27 +00:00
|
|
|
List(&'a [u8], Vec<AnyTerm<'a, 'b>>),
|
|
|
|
ListRef(&'a [u8], &'b [AnyTerm<'a, 'b>]),
|
2022-12-15 12:32:38 +00:00
|
|
|
Seq(&'a [u8], Vec<NonSeqTerm<'a, 'b>>),
|
|
|
|
SeqRef(&'a [u8], &'b [NonSeqTerm<'a, 'b>]),
|
2022-11-17 10:48:43 +00:00
|
|
|
}
|
|
|
|
|
2022-11-17 13:58:27 +00:00
|
|
|
#[derive(Eq, PartialEq, Clone)]
|
2022-12-15 12:32:38 +00:00
|
|
|
pub(crate) enum NonSeqTerm<'a, 'b> {
|
2022-11-17 10:48:43 +00:00
|
|
|
Str(&'a [u8]),
|
2022-11-17 13:58:27 +00:00
|
|
|
Dict(&'a [u8], HashMap<&'a [u8], AnyTerm<'a, 'b>>),
|
|
|
|
DictRef(&'a [u8], &'b HashMap<&'a [u8], AnyTerm<'a, 'b>>),
|
2022-12-15 13:45:27 +00:00
|
|
|
List(&'a [u8], Vec<AnyTerm<'a, 'b>>),
|
|
|
|
ListRef(&'a [u8], &'b [AnyTerm<'a, 'b>]),
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
impl<'a, 'b> From<NonSeqTerm<'a, 'b>> for AnyTerm<'a, 'b> {
|
|
|
|
fn from(x: NonSeqTerm<'a, 'b>) -> AnyTerm<'a, 'b> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match x {
|
2022-12-15 12:32:38 +00:00
|
|
|
NonSeqTerm::Str(s) => AnyTerm::Str(s),
|
|
|
|
NonSeqTerm::Dict(raw, d) => AnyTerm::Dict(raw, d),
|
|
|
|
NonSeqTerm::DictRef(raw, d) => AnyTerm::DictRef(raw, d),
|
2022-12-15 13:45:27 +00:00
|
|
|
NonSeqTerm::List(raw, l) => AnyTerm::List(raw, l),
|
|
|
|
NonSeqTerm::ListRef(raw, l) => AnyTerm::ListRef(raw, l),
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 10:48:43 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
impl<'a, 'b> TryFrom<AnyTerm<'a, 'b>> for NonSeqTerm<'a, 'b> {
|
2022-11-17 13:58:27 +00:00
|
|
|
type Error = ();
|
2022-12-15 12:32:38 +00:00
|
|
|
fn try_from(x: AnyTerm<'a, 'b>) -> Result<NonSeqTerm<'a, 'b>, ()> {
|
2022-11-17 10:48:43 +00:00
|
|
|
match x {
|
2022-12-15 12:32:38 +00:00
|
|
|
AnyTerm::Str(s) => Ok(NonSeqTerm::Str(s)),
|
|
|
|
AnyTerm::Dict(raw, d) => Ok(NonSeqTerm::Dict(raw, d)),
|
|
|
|
AnyTerm::DictRef(raw, d) => Ok(NonSeqTerm::DictRef(raw, d)),
|
2022-12-15 13:45:27 +00:00
|
|
|
AnyTerm::List(raw, l) => Ok(NonSeqTerm::List(raw, l)),
|
|
|
|
AnyTerm::ListRef(raw, l) => Ok(NonSeqTerm::ListRef(raw, l)),
|
2022-11-17 13:58:27 +00:00
|
|
|
_ => Err(()),
|
2022-11-17 10:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 13:58:27 +00:00
|
|
|
impl<'a> From<AnyTerm<'a, 'static>> for Term<'a, 'static> {
|
|
|
|
fn from(x: AnyTerm<'a, 'static>) -> Term<'a, 'static> {
|
|
|
|
Term(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---- PUBLIC IMPLS ----
|
|
|
|
|
|
|
|
impl<'a, 'b> Term<'a, 'b> {
|
2022-11-17 15:35:06 +00:00
|
|
|
// ---- STRUCTURAL MAPPINGS ----
|
|
|
|
|
2022-11-17 13:58:27 +00:00
|
|
|
/// Get the term's raw representation
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
|
|
|
/// let term = decode(b"hello world").unwrap();
|
|
|
|
/// assert_eq!(term.raw(), b"hello world");
|
|
|
|
/// ```
|
|
|
|
pub fn raw(&self) -> &'a [u8] {
|
|
|
|
self.0.raw()
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:23:01 +00:00
|
|
|
/// Get the term's raw representation as an str
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
|
|
|
/// let term = decode(b"hello { a = x, b = y }").unwrap();
|
|
|
|
/// assert_eq!(term.raw_str().unwrap(), "hello { a = x, b = y }");
|
|
|
|
/// ```
|
|
|
|
pub fn raw_str(&self) -> Result<&'a str, TypeError> {
|
|
|
|
Ok(std::str::from_utf8(self.0.raw())?)
|
|
|
|
}
|
|
|
|
|
2022-11-17 13:58:27 +00:00
|
|
|
/// If the term is a single string, get that string
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term1 = decode(b"hello").unwrap();
|
|
|
|
/// assert_eq!(term1.str().unwrap(), "hello");
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// let term2 = decode(b"hello world").unwrap();
|
|
|
|
/// assert!(term2.str().is_err());
|
|
|
|
/// ```
|
2022-11-17 15:35:06 +00:00
|
|
|
pub fn str(&self) -> Result<&'a str, TypeError> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match &self.0 {
|
2022-11-17 15:35:06 +00:00
|
|
|
AnyTerm::Str(s) => Ok(std::str::from_utf8(s)?),
|
2022-11-17 13:58:27 +00:00
|
|
|
_ => Err(TypeError::WrongType("STR")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
/// If the term is a single string, or a sequence containing only strings,
|
2022-11-17 13:58:27 +00:00
|
|
|
/// get its raw representation
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term1 = decode(b"hello world").unwrap();
|
|
|
|
/// assert_eq!(term1.string().unwrap(), "hello world");
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// let term2 = decode(b"hello { a= 5}").unwrap();
|
|
|
|
/// assert!(term2.string().is_err());
|
|
|
|
/// ```
|
2022-11-17 15:35:06 +00:00
|
|
|
pub fn string(&self) -> Result<&'a str, TypeError> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match &self.0 {
|
2022-11-17 15:35:06 +00:00
|
|
|
AnyTerm::Str(s) => Ok(std::str::from_utf8(s)?),
|
2022-12-15 12:32:38 +00:00
|
|
|
AnyTerm::Seq(r, l) if l.iter().all(|x| matches!(x, NonSeqTerm::Str(_))) => {
|
2022-11-17 15:35:06 +00:00
|
|
|
Ok(std::str::from_utf8(r)?)
|
|
|
|
}
|
2022-11-17 13:58:27 +00:00
|
|
|
_ => Err(TypeError::WrongType("STRING")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
/// Return a sequence of terms made from this term.
|
|
|
|
/// If it is a str or a dict, returns a seq of a single term.
|
|
|
|
/// If it is a sequence, that's the seq of terms we return.
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term1 = decode(b"hello").unwrap();
|
2022-12-15 12:32:38 +00:00
|
|
|
/// let seq1 = term1.seq();
|
|
|
|
/// assert_eq!(seq1.len(), 1);
|
|
|
|
/// assert_eq!(seq1[0].str().unwrap(), "hello");
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term2 = decode(b"hello world").unwrap();
|
2022-12-15 12:32:38 +00:00
|
|
|
/// let seq2 = term2.seq();
|
|
|
|
/// assert_eq!(seq2.len(), 2);
|
|
|
|
/// assert_eq!(seq2[0].str().unwrap(), "hello");
|
|
|
|
/// assert_eq!(seq2[1].str().unwrap(), "world");
|
2022-11-17 13:58:27 +00:00
|
|
|
/// ```
|
2022-12-15 12:32:38 +00:00
|
|
|
pub fn seq(&self) -> Vec<Term<'a, '_>> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match self.0.mkref() {
|
2022-12-15 12:32:38 +00:00
|
|
|
AnyTerm::SeqRef(_r, l) => l.iter().map(|x| Term(x.mkref().into())).collect::<Vec<_>>(),
|
2022-11-17 13:58:27 +00:00
|
|
|
x => vec![Term(x)],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
/// Same as `.seq()`, but deconstructs it in a const length array,
|
2022-11-17 15:35:06 +00:00
|
|
|
/// dynamically checking if there are the correct number of items.
|
2022-12-15 12:32:38 +00:00
|
|
|
/// This allows to directly bind the resulting seq into discrete variables.
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term1 = decode(b"hello").unwrap();
|
2022-12-15 12:32:38 +00:00
|
|
|
/// let [s1] = term1.seq_of().unwrap();
|
2022-11-17 15:35:06 +00:00
|
|
|
/// assert_eq!(s1.str().unwrap(), "hello");
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term2 = decode(b"hello world").unwrap();
|
2022-12-15 12:32:38 +00:00
|
|
|
/// let [s2a, s2b] = term2.seq_of().unwrap();
|
2022-11-17 15:35:06 +00:00
|
|
|
/// assert_eq!(s2a.str().unwrap(), "hello");
|
|
|
|
/// assert_eq!(s2b.str().unwrap(), "world");
|
2022-11-17 13:58:27 +00:00
|
|
|
/// ```
|
2022-12-15 12:32:38 +00:00
|
|
|
pub fn seq_of<const N: usize>(&self) -> Result<[Term<'a, '_>; N], TypeError> {
|
|
|
|
let seq = self.seq();
|
|
|
|
let seq_len = seq.len();
|
|
|
|
seq.try_into()
|
|
|
|
.map_err(|_| TypeError::WrongLength(seq_len, N))
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
/// Same as `.seq_of()`, but only binds the first N-1 terms.
|
2022-11-17 13:58:27 +00:00
|
|
|
/// If there are exactly N terms, the last one is bound to the Nth return variable.
|
2022-12-15 12:32:38 +00:00
|
|
|
/// If there are more then N terms, the remaining terms are bound to a new seq term
|
2022-11-17 13:58:27 +00:00
|
|
|
/// that is returned as the Nth return variable.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term1 = decode(b"hello world").unwrap();
|
2022-12-15 12:32:38 +00:00
|
|
|
/// let [s1a, s1b] = term1.seq_of_first().unwrap();
|
2022-11-17 15:35:06 +00:00
|
|
|
/// assert_eq!(s1a.str().unwrap(), "hello");
|
|
|
|
/// assert_eq!(s1b.str().unwrap(), "world");
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let term2 = decode(b"hello mighty world").unwrap();
|
2022-12-15 12:32:38 +00:00
|
|
|
/// let [s2a, s2b] = term2.seq_of_first().unwrap();
|
2022-11-17 15:35:06 +00:00
|
|
|
/// assert_eq!(s2a.str().unwrap(), "hello");
|
2022-12-15 12:32:38 +00:00
|
|
|
/// assert_eq!(s2b.seq().len(), 2);
|
2022-11-17 13:58:27 +00:00
|
|
|
/// assert_eq!(s2b.raw(), b"mighty world");
|
|
|
|
/// ```
|
2022-12-15 12:32:38 +00:00
|
|
|
pub fn seq_of_first<const N: usize>(&self) -> Result<[Term<'a, '_>; N], TypeError> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match self.0.mkref() {
|
2022-12-15 12:32:38 +00:00
|
|
|
AnyTerm::SeqRef(raw, seq) => match seq.len().cmp(&N) {
|
|
|
|
std::cmp::Ordering::Less => Err(TypeError::WrongLength(seq.len(), N)),
|
|
|
|
std::cmp::Ordering::Equal => Ok(seq
|
2022-11-17 14:02:33 +00:00
|
|
|
.iter()
|
|
|
|
.map(|x| Term(x.mkref().into()))
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.try_into()
|
|
|
|
.unwrap()),
|
|
|
|
std::cmp::Ordering::Greater => {
|
2022-11-17 13:58:27 +00:00
|
|
|
let mut ret = Vec::with_capacity(N);
|
2022-12-15 12:32:38 +00:00
|
|
|
for item in seq[0..N - 1].iter() {
|
2022-11-17 13:58:27 +00:00
|
|
|
ret.push(Term(item.mkref().into()));
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
let remaining_begin = seq[N - 1].raw().as_ptr() as usize;
|
2022-11-17 13:58:27 +00:00
|
|
|
let remaining_offset = remaining_begin - raw.as_ptr() as usize;
|
|
|
|
let remaining_raw = &raw[remaining_offset..];
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
ret.push(Term(AnyTerm::SeqRef(remaining_raw, &seq[N - 1..])));
|
2022-11-17 13:58:27 +00:00
|
|
|
|
|
|
|
Ok(ret.try_into().unwrap())
|
|
|
|
}
|
2022-11-17 14:02:33 +00:00
|
|
|
},
|
2022-11-17 13:58:27 +00:00
|
|
|
x if N == 1 => Ok([Term(x)]
|
|
|
|
.into_iter()
|
|
|
|
.collect::<Vec<_>>()
|
|
|
|
.try_into()
|
|
|
|
.unwrap()),
|
|
|
|
_ => Err(TypeError::WrongLength(1, N)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 15:35:06 +00:00
|
|
|
/// Checks term is a dictionnary and returns hashmap of inner terms.
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
|
|
|
/// let term = decode(b"{ k1 = v1, k2 = v2 }").unwrap();
|
|
|
|
/// let dict = term.dict().unwrap();
|
2022-11-17 15:35:06 +00:00
|
|
|
/// assert_eq!(dict.get("k1").unwrap().str().unwrap(), "v1");
|
|
|
|
/// assert_eq!(dict.get("k2").unwrap().str().unwrap(), "v2");
|
2022-11-17 13:58:27 +00:00
|
|
|
/// ```
|
2022-11-17 15:35:06 +00:00
|
|
|
pub fn dict(&self) -> Result<HashMap<&'a str, Term<'a, '_>>, TypeError> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match self.0.mkref() {
|
2022-11-17 15:35:06 +00:00
|
|
|
AnyTerm::DictRef(_, d) => {
|
|
|
|
let mut res = HashMap::with_capacity(d.len());
|
|
|
|
for (k, t) in d.iter() {
|
|
|
|
res.insert(std::str::from_utf8(k)?, Term(t.mkref()));
|
|
|
|
}
|
|
|
|
Ok(res)
|
|
|
|
}
|
2022-11-17 13:58:27 +00:00
|
|
|
_ => Err(TypeError::WrongType("DICT")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks term is a dictionnary whose keys are exactly those supplied,
|
2022-12-15 12:32:38 +00:00
|
|
|
/// and returns the associated values as a seq.
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-11-17 14:06:42 +00:00
|
|
|
/// let term = decode(b"{ k1 = v1, k2 = v2, k3 = v3 }").unwrap();
|
2022-11-17 15:35:06 +00:00
|
|
|
/// let [s1, s2] = term.dict_of(["k1", "k2"], true).unwrap();
|
|
|
|
/// assert_eq!(s1.str().unwrap(), "v1");
|
|
|
|
/// assert_eq!(s2.str().unwrap(), "v2");
|
2022-11-17 14:06:42 +00:00
|
|
|
///
|
2022-11-17 15:56:55 +00:00
|
|
|
/// assert!(term.dict_of(["k1", "k2"], false).is_err());
|
2022-11-17 13:58:27 +00:00
|
|
|
/// ```
|
2022-11-17 15:35:06 +00:00
|
|
|
pub fn dict_of<const N: usize, T: AsRef<[u8]>>(
|
2022-11-17 13:58:27 +00:00
|
|
|
&self,
|
2022-11-17 15:35:06 +00:00
|
|
|
keys: [T; N],
|
2022-11-17 14:06:42 +00:00
|
|
|
allow_extra_keys: bool,
|
2022-11-17 13:58:27 +00:00
|
|
|
) -> Result<[Term<'a, '_>; N], TypeError> {
|
|
|
|
match self.0.mkref() {
|
|
|
|
AnyTerm::DictRef(_, dict) => {
|
|
|
|
// Check all required keys exist in dictionnary
|
|
|
|
for k in keys.iter() {
|
2022-11-17 15:35:06 +00:00
|
|
|
if !dict.contains_key(k.as_ref()) {
|
|
|
|
return Err(TypeError::MissingKey(debug(k.as_ref()).to_string()));
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 14:06:42 +00:00
|
|
|
if !allow_extra_keys {
|
|
|
|
// Check that dictionnary contains no extraneous keys
|
|
|
|
for k in dict.keys() {
|
2022-11-17 15:35:06 +00:00
|
|
|
if !keys.iter().any(|k2| k2.as_ref() == *k) {
|
2022-11-17 14:06:42 +00:00
|
|
|
return Err(TypeError::UnexpectedKey(debug(k).to_string()));
|
|
|
|
}
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 15:35:06 +00:00
|
|
|
Ok(keys.map(|k| Term(dict.get(k.as_ref()).unwrap().mkref())))
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
_ => Err(TypeError::WrongType("DICT")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Checks term is a dictionnary whose keys are included in those supplied,
|
2022-12-15 12:32:38 +00:00
|
|
|
/// and returns the associated values as a seq of options.
|
2022-11-17 13:58:27 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-11-17 14:06:42 +00:00
|
|
|
/// let term = decode(b"{ k1 = v1, k2 = v2, k4 = v4 }").unwrap();
|
2022-11-17 15:56:55 +00:00
|
|
|
/// let [s1, s2, s3] = term.dict_of_opt(["k1", "k2", "k3"], true).unwrap();
|
2022-11-17 15:35:06 +00:00
|
|
|
/// assert_eq!(s1.unwrap().str().unwrap(), "v1");
|
|
|
|
/// assert_eq!(s2.unwrap().str().unwrap(), "v2");
|
2022-11-17 13:58:27 +00:00
|
|
|
/// assert!(s3.is_none());
|
2022-11-17 14:06:42 +00:00
|
|
|
///
|
2022-11-17 15:35:06 +00:00
|
|
|
/// assert!(term.dict_of_opt(["k1", "k2", "k3"], false).is_err());
|
2022-11-17 13:58:27 +00:00
|
|
|
/// ```
|
2022-11-17 15:35:06 +00:00
|
|
|
pub fn dict_of_opt<const N: usize, T: AsRef<[u8]>>(
|
2022-11-17 13:58:27 +00:00
|
|
|
&self,
|
2022-11-17 15:35:06 +00:00
|
|
|
keys: [T; N],
|
2022-11-17 14:06:42 +00:00
|
|
|
allow_extra_keys: bool,
|
2022-11-17 13:58:27 +00:00
|
|
|
) -> Result<[Option<Term<'a, '_>>; N], TypeError> {
|
|
|
|
match self.0.mkref() {
|
|
|
|
AnyTerm::DictRef(_, dict) => {
|
2022-11-17 14:06:42 +00:00
|
|
|
if !allow_extra_keys {
|
|
|
|
// Check that dictionnary contains no extraneous keys
|
|
|
|
for k in dict.keys() {
|
2022-11-17 15:35:06 +00:00
|
|
|
if !keys.iter().any(|x| x.as_ref() == *k) {
|
2022-11-17 14:06:42 +00:00
|
|
|
return Err(TypeError::UnexpectedKey(debug(k).to_string()));
|
|
|
|
}
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-17 15:35:06 +00:00
|
|
|
Ok(keys.map(|k| dict.get(k.as_ref()).map(|x| Term(x.mkref()))))
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
_ => Err(TypeError::WrongType("DICT")),
|
|
|
|
}
|
|
|
|
}
|
2022-11-17 15:35:06 +00:00
|
|
|
|
2022-12-15 13:45:27 +00:00
|
|
|
/// Checks if the term is a list, and if so, return its elements in a vec.
|
2022-11-18 00:59:00 +00:00
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
2022-12-15 13:45:27 +00:00
|
|
|
/// let term2 = decode(b"[ hello, world ]").unwrap();
|
|
|
|
/// let seq2 = term2.list().unwrap();
|
|
|
|
/// assert_eq!(seq2.len(), 2);
|
|
|
|
/// assert_eq!(seq2[0].str().unwrap(), "hello");
|
|
|
|
/// assert_eq!(seq2[1].str().unwrap(), "world");
|
2022-11-18 00:59:00 +00:00
|
|
|
/// ```
|
2022-12-15 13:45:27 +00:00
|
|
|
pub fn list(&self) -> Result<Vec<Term<'a, '_>>, TypeError> {
|
2022-11-18 00:59:00 +00:00
|
|
|
match self.0.mkref() {
|
2022-12-15 13:45:27 +00:00
|
|
|
AnyTerm::ListRef(_r, l) => Ok(l.iter().map(|x| Term(x.mkref().into())).collect::<Vec<_>>()),
|
|
|
|
_ => Err(TypeError::WrongType("LIST")),
|
2022-11-18 00:59:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 15:35:06 +00:00
|
|
|
// ---- TYPE CASTS ----
|
|
|
|
|
|
|
|
/// Try to interpret this str as an i64
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
|
|
|
/// let term = decode(b"42").unwrap();
|
|
|
|
/// assert_eq!(term.int().unwrap(), 42);
|
|
|
|
/// ```
|
|
|
|
pub fn int(&self) -> Result<i64, TypeError> {
|
|
|
|
self.str()?
|
|
|
|
.parse::<i64>()
|
|
|
|
.map_err(|_| TypeError::WrongType("INT"))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to interpret this string as base64-encoded bytes (uses URL-safe, no-padding encoding)
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
|
|
|
/// let term = decode(b"aGVsbG8sIHdvcmxkIQ").unwrap();
|
|
|
|
/// assert_eq!(term.bytes().unwrap(), b"hello, world!");
|
|
|
|
/// ```
|
|
|
|
pub fn bytes(&self) -> Result<Vec<u8>, TypeError> {
|
2022-11-17 23:20:42 +00:00
|
|
|
let decode = |encoded| {
|
|
|
|
base64::decode_config(encoded, base64::URL_SAFE_NO_PAD)
|
|
|
|
.map_err(|_| TypeError::WrongType("BYTES"))
|
2022-11-17 15:35:06 +00:00
|
|
|
};
|
2022-11-17 23:20:42 +00:00
|
|
|
match self.0.mkref() {
|
|
|
|
AnyTerm::Str(encoded) => {
|
|
|
|
if encoded == b"." {
|
|
|
|
Ok(vec![])
|
|
|
|
} else {
|
|
|
|
decode(encoded)
|
|
|
|
}
|
|
|
|
}
|
2022-12-15 12:32:38 +00:00
|
|
|
AnyTerm::SeqRef(_, seq) => {
|
2022-11-17 23:20:42 +00:00
|
|
|
let mut ret = Vec::with_capacity(128);
|
2022-12-15 12:32:38 +00:00
|
|
|
for term in seq.iter() {
|
|
|
|
if let NonSeqTerm::Str(encoded) = term {
|
2022-11-17 23:20:42 +00:00
|
|
|
ret.extend(decode(encoded)?)
|
|
|
|
} else {
|
|
|
|
return Err(TypeError::WrongType("BYTES"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
_ => Err(TypeError::WrongType("BYTES")),
|
|
|
|
}
|
2022-11-17 15:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to interpret this string as base64-encoded bytes,
|
|
|
|
/// with an exact length.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
|
|
|
///
|
|
|
|
/// let term = decode(b"aGVsbG8sIHdvcmxkIQ").unwrap();
|
|
|
|
/// assert_eq!(&term.bytes_exact::<13>().unwrap(), b"hello, world!");
|
|
|
|
/// ```
|
|
|
|
pub fn bytes_exact<const N: usize>(&self) -> Result<[u8; N], TypeError> {
|
|
|
|
let bytes = self.bytes()?;
|
|
|
|
let bytes_len = bytes.len();
|
|
|
|
bytes
|
|
|
|
.try_into()
|
|
|
|
.map_err(|_| TypeError::WrongLength(bytes_len, N))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---- CRYPTO HELPERS ----
|
|
|
|
|
2022-11-22 13:56:58 +00:00
|
|
|
#[cfg(feature = "dryoc")]
|
2022-11-17 15:35:06 +00:00
|
|
|
impl<'a, 'b> Term<'a, 'b> {
|
|
|
|
/// Try to interpret this string as a Blake2b512 digest (32-bytes base64 encoded)
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use nettext::dec::decode;
|
2022-11-22 13:56:58 +00:00
|
|
|
/// use nettext::crypto::generichash::GenericHash;
|
2022-11-17 15:35:06 +00:00
|
|
|
///
|
|
|
|
/// let term = decode(b"{
|
|
|
|
/// message = hello,
|
2022-11-22 13:56:58 +00:00
|
|
|
/// hash = Mk3PAn3UowqTLEQfNlol6GsXPe-kuOWJSCU0cbgbcs8,
|
2022-11-17 15:35:06 +00:00
|
|
|
/// }").unwrap();
|
|
|
|
/// let [msg, hash] = term.dict_of(["message", "hash"], false).unwrap();
|
2022-11-22 13:56:58 +00:00
|
|
|
/// let expected_hash = GenericHash::hash_with_defaults(msg.raw(), None::<&Vec<u8>>).unwrap();
|
2022-11-22 14:29:04 +00:00
|
|
|
/// assert_eq!(hash.hash().unwrap(), expected_hash);
|
2022-11-17 15:35:06 +00:00
|
|
|
/// ```
|
2022-11-22 14:29:04 +00:00
|
|
|
pub fn hash(&self) -> Result<crypto::generichash::Hash, TypeError> {
|
2022-11-22 13:56:58 +00:00
|
|
|
Ok(crypto::generichash::Hash::from(self.bytes_exact()?))
|
2022-11-17 15:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to interpret this string as an ed25519 keypair (64 bytes base64 encoded)
|
2022-11-22 13:56:58 +00:00
|
|
|
pub fn keypair(&self) -> Result<crypto::SigningKeyPair, TypeError> {
|
|
|
|
let secret_key = crypto::sign::SecretKey::from(self.bytes_exact()?);
|
|
|
|
Ok(crypto::SigningKeyPair::from_secret_key(secret_key))
|
2022-11-17 15:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to interpret this string as an ed25519 public key (32 bytes base64 encoded)
|
2022-11-22 13:56:58 +00:00
|
|
|
pub fn public_key(&self) -> Result<crypto::sign::PublicKey, TypeError> {
|
|
|
|
Ok(crypto::sign::PublicKey::from(self.bytes_exact()?))
|
2022-11-17 15:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to interpret this string as an ed25519 secret key (32 bytes base64 encoded)
|
2022-11-22 13:56:58 +00:00
|
|
|
pub fn secret_key(&self) -> Result<crypto::sign::SecretKey, TypeError> {
|
|
|
|
Ok(crypto::sign::SecretKey::from(self.bytes_exact()?))
|
2022-11-17 15:35:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Try to interpret this string as an ed25519 signature (64 bytes base64 encoded)
|
2022-11-22 13:56:58 +00:00
|
|
|
pub fn signature(&self) -> Result<crypto::sign::Signature, TypeError> {
|
|
|
|
Ok(crypto::sign::Signature::from(self.bytes_exact()?))
|
2022-11-17 15:35:06 +00:00
|
|
|
}
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---- INTERNAL IMPLS ----
|
|
|
|
|
|
|
|
impl<'a, 'b> AnyTerm<'a, 'b> {
|
|
|
|
fn raw(&self) -> &'a [u8] {
|
|
|
|
match self {
|
|
|
|
AnyTerm::Str(s) => s,
|
|
|
|
AnyTerm::Dict(r, _)
|
|
|
|
| AnyTerm::DictRef(r, _)
|
2022-12-15 13:45:27 +00:00
|
|
|
| AnyTerm::List(r, _)
|
|
|
|
| AnyTerm::ListRef(r, _)
|
2022-12-15 12:32:38 +00:00
|
|
|
| AnyTerm::Seq(r, _)
|
|
|
|
| AnyTerm::SeqRef(r, _) => r,
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 15:26:44 +00:00
|
|
|
pub(crate) fn mkref(&self) -> AnyTerm<'a, '_> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match &self {
|
|
|
|
AnyTerm::Str(s) => AnyTerm::Str(s),
|
2022-11-17 14:02:33 +00:00
|
|
|
AnyTerm::Dict(r, d) => AnyTerm::DictRef(r, d),
|
2022-11-17 13:58:27 +00:00
|
|
|
AnyTerm::DictRef(r, d) => AnyTerm::DictRef(r, d),
|
2022-12-15 13:45:27 +00:00
|
|
|
AnyTerm::List(r, l) => AnyTerm::ListRef(r, l),
|
|
|
|
AnyTerm::ListRef(r, l) => AnyTerm::ListRef(r, l),
|
2022-12-15 12:32:38 +00:00
|
|
|
AnyTerm::Seq(r, l) => AnyTerm::SeqRef(r, &l[..]),
|
|
|
|
AnyTerm::SeqRef(r, l) => AnyTerm::SeqRef(r, l),
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
impl<'a, 'b> NonSeqTerm<'a, 'b> {
|
2022-11-17 13:58:27 +00:00
|
|
|
fn raw(&self) -> &'a [u8] {
|
|
|
|
match &self {
|
2022-12-15 12:32:38 +00:00
|
|
|
NonSeqTerm::Str(s) => s,
|
|
|
|
NonSeqTerm::Dict(r, _) | NonSeqTerm::DictRef(r, _) => r,
|
2022-12-15 13:45:27 +00:00
|
|
|
NonSeqTerm::List(r, _) | NonSeqTerm::ListRef(r, _) => r,
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
fn mkref(&self) -> NonSeqTerm<'a, '_> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match &self {
|
2022-12-15 12:32:38 +00:00
|
|
|
NonSeqTerm::Str(s) => NonSeqTerm::Str(s),
|
|
|
|
NonSeqTerm::Dict(r, d) => NonSeqTerm::DictRef(r, d),
|
|
|
|
NonSeqTerm::DictRef(r, d) => NonSeqTerm::DictRef(r, d),
|
2022-12-15 13:45:27 +00:00
|
|
|
NonSeqTerm::List(r, l) => NonSeqTerm::ListRef(r, l),
|
|
|
|
NonSeqTerm::ListRef(r, l) => NonSeqTerm::ListRef(r, l),
|
2022-11-17 13:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-17 15:35:06 +00:00
|
|
|
// ---- DISPLAY REPR = Raw nettext representation ----
|
2022-11-17 13:58:27 +00:00
|
|
|
|
|
|
|
impl<'a, 'b> std::fmt::Display for AnyTerm<'a, 'b> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"{}",
|
|
|
|
std::str::from_utf8(self.raw()).map_err(|_| Default::default())?
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> std::fmt::Display for Term<'a, 'b> {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---- DEBUG REPR ----
|
2022-11-17 10:48:43 +00:00
|
|
|
|
2022-11-17 13:58:27 +00:00
|
|
|
impl<'a, 'b> std::fmt::Debug for AnyTerm<'a, 'b> {
|
2022-11-17 10:48:43 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
2022-11-17 13:58:27 +00:00
|
|
|
match self.mkref() {
|
|
|
|
AnyTerm::Str(s) => write!(f, "Str(`{}`)", debug(s)),
|
|
|
|
AnyTerm::DictRef(raw, d) => {
|
2022-11-17 10:48:43 +00:00
|
|
|
write!(f, "Dict<`{}`", debug(raw))?;
|
|
|
|
for (k, v) in d.iter() {
|
|
|
|
write!(f, "\n `{}`={:?}", debug(k), v)?;
|
|
|
|
}
|
|
|
|
write!(f, ">")
|
|
|
|
}
|
2022-12-15 13:45:27 +00:00
|
|
|
AnyTerm::ListRef(raw, l) => {
|
|
|
|
write!(f, "List[`{}`", debug(raw))?;
|
|
|
|
for i in l.iter() {
|
|
|
|
write!(f, "\n {:?}", i)?;
|
|
|
|
}
|
|
|
|
write!(f, "]")
|
|
|
|
}
|
2022-12-15 12:32:38 +00:00
|
|
|
AnyTerm::SeqRef(raw, l) => {
|
|
|
|
write!(f, "Seq[`{}`", debug(raw))?;
|
2022-11-17 10:48:43 +00:00
|
|
|
for i in l.iter() {
|
|
|
|
write!(f, "\n {:?}", i)?;
|
|
|
|
}
|
|
|
|
write!(f, "]")
|
|
|
|
}
|
2022-11-17 13:58:27 +00:00
|
|
|
_ => unreachable!(),
|
2022-11-17 10:48:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-15 12:32:38 +00:00
|
|
|
impl<'a, 'b> std::fmt::Debug for NonSeqTerm<'a, 'b> {
|
2022-11-17 10:48:43 +00:00
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
|
2022-11-18 17:59:06 +00:00
|
|
|
AnyTerm::from(self.mkref()).fmt(f)
|
2022-11-17 10:48:43 +00:00
|
|
|
}
|
|
|
|
}
|