qraft-core 0.1.2

Core type system, query model, decoding, and SQL lowering primitives for qraft.
Documentation
//! Spans into interned query text and binary data.

/// A span pointing to either static text or interned query text.
///
/// The distinction matters because some strings are borrowed from `'static`
/// source while others live inside the query's data buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct TextSpan(pub TextSource);

impl TextSpan {
    pub(crate) const fn new_static(s: &'static str) -> Self {
        Self(TextSource::StaticText(s))
    }

    pub(crate) fn new(start: u32, len: u32) -> Self {
        Self(TextSource::Text(Span { start, len }))
    }

    pub(crate) fn new_span(span: Span) -> Self {
        Self(TextSource::Text(span))
    }

    pub(crate) fn offset(&self, delta: u32) -> TextSpan {
        match self.0 {
            TextSource::Text(span) => TextSpan::new_span(span.offset(delta)),
            _ => *self,
        }
    }
}

/// Backing storage for a `TextSpan`.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TextSource {
    StaticText(&'static str),
    Text(Span),
}

/// A byte range inside the query's interned data buffer.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Span {
    /// Starting byte offset.
    pub start: u32,
    /// Length in bytes.
    pub len: u32,
}

impl Span {
    /// Creates a new byte span.
    pub const fn new(start: u32, len: u32) -> Self {
        Self { start, len }
    }

    pub(crate) fn offset(&self, delta: u32) -> Span {
        Span {
            start: self.start + delta,
            len: self.len,
        }
    }
}