~quf/tocs

7bcd70f7799893c511e11c7d7b2d876bbd0c3701 — Lukas Himbert 1 year, 2 months ago c883f37
utility functions
4 files changed, 98 insertions(+), 1 deletions(-)

M tocs/src/io/ascii.rs
M tocs/src/lib.rs
M tocs/src/tbl/mod.rs
M tocs/src/tbl/schema.rs
M tocs/src/io/ascii.rs => tocs/src/io/ascii.rs +6 -1
@@ 47,7 47,7 @@ impl<const PADDING: usize> PaddedAsciiString<PADDING> {
        })
    }

    pub fn from_eco_string(str_excluding_terminator_or_padding: &ecow::EcoString) -> Result<Self, PaddedStringError> {
    pub fn from_eco_string(str_excluding_terminator_or_padding: ecow::EcoString) -> Result<Self, PaddedStringError> {
        _ = Self::P;
        check_str(str_excluding_terminator_or_padding.as_str())?;
        Ok(PaddedAsciiString {


@@ 55,6 55,11 @@ impl<const PADDING: usize> PaddedAsciiString<PADDING> {
        })
    }

    /// Convert data to an ecostring. The result will not be null-terminated or padded.
    pub fn to_eco_string(&self) -> ecow::EcoString {
        self.s.clone()
    }

    pub fn from_ascii(bytes_excluding_terminator_or_padding: &[u8]) -> Result<Self, PaddedStringError> {
        _ = Self::P;
        check_bytes(bytes_excluding_terminator_or_padding)?;

M tocs/src/lib.rs => tocs/src/lib.rs +2 -0
@@ 2,6 2,8 @@

mod io;

pub use io::{PaddedAsciiStr, PaddedAsciiString, PaddedByteStr, PaddedByteString, PaddedUtf8Str, PaddedUtf8String};

#[cfg(feature = "compression")]
pub mod compression;


M tocs/src/tbl/mod.rs => tocs/src/tbl/mod.rs +10 -0
@@ 153,6 153,16 @@ pub enum ParseTblResult {
    Err(DeserializeError),
}

impl ParseTblResult {
    pub fn ignore_warnings(self) -> Result<Tbl, DeserializeError> {
        match self {
            ParseTblResult::Ok(tbl) => Ok(tbl),
            ParseTblResult::Warn { tbl, .. } => Ok(tbl),
            ParseTblResult::Err(e) => Err(e),
        }
    }
}

impl ReadTblResult {
    pub fn ignore_warnings(self) -> Result<Tbl, ReadError> {
        match self {

M tocs/src/tbl/schema.rs => tocs/src/tbl/schema.rs +80 -0
@@ 348,6 348,86 @@ impl AtomicValue {
            AtomicValue::X(data) => AtomicType::X(data.len()),
        }
    }

    /// Get value as `i8`. Panics if the type doesn't match.
    pub fn expect_i8(&self) -> i8 {
        match self {
            AtomicValue::I8(x) => *x,
            _ => panic!("expected i8, but got a {:?}", self.r#type()),
        }
    }

    /// Get value as `i16`. Panics if the type doesn't match.
    pub fn expect_i16(&self) -> i16 {
        match self {
            AtomicValue::I16(x) => *x,
            _ => panic!("expected i16, but got a {:?}", self.r#type()),
        }
    }

    /// Get value as `i32`. Panics if the type doesn't match.
    pub fn expect_i32(&self) -> i32 {
        match self {
            AtomicValue::I32(x) => *x,
            _ => panic!("expected i32, but got a {:?}", self.r#type()),
        }
    }

    /// Get value as `u8`. Panics if the type doesn't match.
    pub fn expect_u8(&self) -> u8 {
        match self {
            AtomicValue::U8(x) => *x,
            _ => panic!("expected u8, but got a {:?}", self.r#type()),
        }
    }

    /// Get value as `u16`. Panucs if the type doesn't match.
    pub fn expect_u16(&self) -> u16 {
        match self {
            AtomicValue::U16(x) => *x,
            _ => panic!("expected u16, but got a {:?}", self.r#type()),
        }
    }

    /// Get value as `u32`. Panics if the type doesn't match.
    pub fn expect_u32(&self) -> u32 {
        match self {
            AtomicValue::U32(x) => *x,
            _ => panic!("expected u32, but got a {:?}", self.r#type()),
        }
    }

    /// Get value as `f32`. Panics if the type doesn't match.
    pub fn expect_f32(&self) -> f32 {
        match self {
            AtomicValue::F32(x) => *x,
            _ => panic!("expected f32, but got a {:?}", self.r#type()),
        }
    }

    /// Get value as a string. Panics if the type doesn't match.
    pub fn expect_c_utf8(self) -> ecow::EcoString {
        match self {
            AtomicValue::CUtf8(s) => s,
            _ => panic!("expected utf8 string, but got a {:?}", self.r#type()),
        }
    }

    /// Get value reference as a string. Panics if the type doesn't match.
    pub fn expect_c_utf8_as_ref(&self) -> &str {
        match self {
            AtomicValue::CUtf8(s) => s.as_str(),
            _ => panic!("expected utf8 string, but got a {:?}", self.r#type()),
        }
    }

    /// Get value reference as hex data. Panics if the type doesn't match.
    pub fn expect_data_as_ref(&self) -> &[u8] {
        match self {
            AtomicValue::X(data) => data.as_slice(),
            _ => panic!("expected data slice, but got a {:?}", self.r#type()),
        }
    }
}

// "Flatten" the schema by unrolling sequences and dereferencing references