uri-template-core 0.1.0

RFC6570 URI Template library for Rust, with parsing, expansion, compile-time checked macros, and no_std support.
Documentation
pub use alloc::borrow::Cow;

use crate::UriTemplate;
use core::ops::Range;

#[doc(hidden)]
pub fn deconstruct_fields(this: UriTemplate) -> (Cow<'static, str>, Cow<'static, [Piece]>) {
    (this.raw, this.pieces)
}

#[doc(hidden)]
pub const fn from_static(raw: &'static str, pieces: &'static [Piece]) -> UriTemplate {
    UriTemplate {
        raw: Cow::Borrowed(raw),
        pieces: Cow::Borrowed(pieces),
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Piece {
    Literal(Literal),
    Expression {
        operator: Option<Operator>,
        variables: Cow<'static, [Variable]>,
    },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Literal {
    Fine(Range<usize>),
    Encoded {
        range: Range<usize>,
        decoded: Result<Cow<'static, str>, Cow<'static, [u8]>>,
    },
    NeedEncode {
        range: Range<usize>,
        encoded: Cow<'static, str>,
    },
}

impl Literal {
    pub const fn range(&self) -> &Range<usize> {
        match self {
            Literal::Fine(range)
            | Literal::Encoded { range, .. }
            | Literal::NeedEncode { range, .. } => range,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Modifier {
    /// {var:3}
    Prefix(u16),
    /// {var*}
    Explode,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Variable {
    pub range: Range<usize>,
    pub modifier: Option<Modifier>,
}

impl Variable {
    pub const fn range(&self) -> &Range<usize> {
        &self.range
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Operator {
    /// `{+var}`
    Plus,
    /// `{/var}`
    Slash,
    /// `{.var}`
    Dot,
    /// `{;var}`
    Semi,
    /// `{?var}`
    Question,
    /// `{&var}`
    Ampersand,
    /// `{#var}`
    Hash,
    /// "=" / "," / "!" / "@" / "|"
    Reserved(u8),
}