use bstr::{BStr, ByteSlice};
use byteyarn::{ByteYarn, YarnRef};
use crate::{State, StateRef};
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone)]
pub struct Value(ByteYarn);
#[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]
pub struct ValueRef<'a>(YarnRef<'a, [u8]>);
impl<'a> ValueRef<'a> {
pub fn from_bytes(input: &'a [u8]) -> Self {
Self(YarnRef::from(input))
}
}
impl ValueRef<'_> {
pub fn as_bstr(&self) -> &BStr {
self.0.as_bytes().as_bstr()
}
pub fn to_owned(self) -> Value {
self.into()
}
}
impl<'a> From<&'a str> for ValueRef<'a> {
fn from(v: &'a str) -> Self {
ValueRef(v.as_bytes().into())
}
}
impl<'a> From<ValueRef<'a>> for Value {
fn from(v: ValueRef<'a>) -> Self {
Value(
v.0.immortalize()
.map_or_else(|| v.0.to_boxed_bytes().into(), YarnRef::to_box),
)
}
}
impl From<&str> for Value {
fn from(v: &str) -> Self {
Value(
ByteYarn::inlined(v.as_bytes())
.unwrap_or_else(|| ByteYarn::from_boxed_bytes(v.as_bytes().to_vec().into_boxed_slice())),
)
}
}
impl Value {
pub fn as_ref(&self) -> ValueRef<'_> {
ValueRef(self.0.as_ref())
}
}
impl StateRef<'_> {
pub fn is_unspecified(&self) -> bool {
matches!(self, StateRef::Unspecified)
}
pub fn is_set(&self) -> bool {
matches!(self, StateRef::Set | StateRef::Value(_))
}
pub fn is_unset(&self) -> bool {
matches!(self, StateRef::Unset)
}
pub fn as_bstr(&self) -> Option<&BStr> {
match self {
StateRef::Value(v) => Some(v.as_bstr()),
_ => None,
}
}
}
impl<'a> StateRef<'a> {
pub fn from_bytes(input: &'a [u8]) -> Self {
Self::Value(ValueRef::from_bytes(input))
}
}
impl<'a> StateRef<'a> {
pub fn to_owned(self) -> State {
self.into()
}
}
impl<'a> State {
pub fn as_ref(&'a self) -> StateRef<'a> {
match self {
State::Value(v) => StateRef::Value(v.as_ref()),
State::Set => StateRef::Set,
State::Unset => StateRef::Unset,
State::Unspecified => StateRef::Unspecified,
}
}
}
impl<'a> From<StateRef<'a>> for State {
fn from(s: StateRef<'a>) -> Self {
match s {
StateRef::Value(v) => State::Value(v.into()),
StateRef::Set => State::Set,
StateRef::Unset => State::Unset,
StateRef::Unspecified => State::Unspecified,
}
}
}