uri-template-core 0.1.0

RFC6570 URI Template library for Rust, with parsing, expansion, compile-time checked macros, and no_std support.
Documentation
use crate::value::{ToValue, Value, Values};
use alloc::borrow::Cow;
use alloc::collections::BTreeMap;
use core::{borrow::Borrow, cmp::Eq};

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct BTreeValues<'v, 's: 'v, K: Borrow<str> + Ord>(BTreeMap<K, Value<'v, 's>>);
impl<'v, 's, K: Borrow<str> + Ord> BTreeValues<'v, 's, K> {
    #[inline]
    pub fn new() -> Self {
        Self(BTreeMap::new())
    }
    #[inline]
    pub fn insert(&mut self, key: K, val: impl Into<Value<'v, 's>>) -> Option<Value<'v, 's>> {
        self.0.insert(key, val.into())
    }
    #[inline]
    pub fn remove(&mut self, key: &str) -> Option<Value<'v, 's>> {
        self.0.remove(key)
    }
    #[inline]
    pub fn get(&self, key: &str) -> Option<&Value<'v, 's>> {
        self.0.get(key)
    }
}

impl<'v, 's, K> Values<'v, 's> for &BTreeValues<'v, 's, K>
where
    K: Borrow<str> + Ord,
{
    #[inline]
    fn var(&self, name: &str) -> Option<Cow<'_, Value<'v, 's>>> {
        self.get(name).map(Cow::Borrowed)
    }
}

impl<'v, 's, K> Values<'v, 's> for &BTreeMap<K, Option<Value<'v, 's>>>
where
    K: Borrow<str> + Ord,
{
    #[inline]
    fn var(&self, name: &str) -> Option<Cow<'_, Value<'v, 's>>> {
        self.get(name).and_then(Option::as_ref).map(Cow::Borrowed)
    }
}

impl<'v, 's, K> Values<'v, 's> for &BTreeMap<K, Value<'v, 's>>
where
    K: Borrow<str> + Ord,
{
    #[inline]
    fn var(&self, name: &str) -> Option<Cow<'_, Value<'v, 's>>> {
        self.get(name).map(Cow::Borrowed)
    }
}

impl<'v, 's: 'v, K> Values<'v, 's> for &BTreeMap<K, Box<dyn ToValue<'v, 's>>>
where
    K: Borrow<str> + Ord,
{
    #[inline]
    fn var(&self, name: &str) -> Option<Cow<'_, Value<'v, 's>>> {
        self.get(name).map(|val| val.to_value()).map(Cow::Owned)
    }
}

impl<'v, 's: 'v, K> Values<'v, 's> for &BTreeMap<K, &'v dyn ToValue<'v, 's>>
where
    K: Borrow<str> + Ord,
{
    #[inline]
    fn var(&self, name: &str) -> Option<Cow<'_, Value<'v, 's>>> {
        self.get(name).map(|val| val.to_value()).map(Cow::Owned)
    }
}