litl-val 0.2.0

A memory efficient representation of JSON values
Documentation
use deepsize::DeepSizeOf;
use std::fmt::Debug;
use thiserror::Error;

use crate::ValRef;

use super::{key_e::KeyE, val::Val};

#[derive(Clone, Hash, Eq, PartialEq, PartialOrd, Ord)]
pub struct Key(pub(crate) Val);

impl Key {
    pub fn as_str(&self) -> &str {
        self.as_ref()
    }

    pub fn into_string(self) -> String {
        match self.into() {
            KeyE::ShortString(s) => s.as_ref().to_string(),
            KeyE::LongString(s) => s.into_string(),
        }
    }
}

impl AsRef<str> for Key {
    fn as_ref(&self) -> &str {
        match self.0.direct_ref() {
            ValRef::String(s) => s,
            _ => unreachable!(),
        }
    }
}

impl Debug for Key {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.as_ref().fmt(f)
    }
}

#[derive(Error, Debug)]
pub enum ValKeyError {
    #[error("Invalid key")]
    InvalidKey,
}

impl DeepSizeOf for Key {
    fn deep_size_of_children(&self, context: &mut deepsize::Context) -> usize {
        self.0.deep_size_of_children(context)
    }
}