hinge 0.1.0

SQL-native ELT engine — dependency graph resolved automatically from FROM/JOIN clauses, parallel execution, single binary
Documentation
use std::collections::BTreeMap;
use std::fmt;

mod error;
mod kind;
mod reference;
mod source;

pub use error::AssetError;
pub use kind::AssetKind;
pub use reference::AssetReference;
pub use source::AssetSource;

/// A SQL asset: a named SQL fragment that produces a relation in a database.
///
/// An asset is uniquely identified by its [`AssetReference`] (`schema.name`).
/// It carries the SQL body ([`AssetSource`]), the output type ([`AssetKind`]),
/// and any optional header directives parsed from `-- @key: value` comments.
///
/// Assets are constructed by [`BuildGraph::from_dir`] — you rarely need to
/// build them manually outside of tests.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct Asset {
    kind: AssetKind,
    reference: AssetReference,
    source: AssetSource,
    headers: BTreeMap<String, String>,
}

impl Asset {
    /// Create an asset with no header directives.
    pub fn new(kind: AssetKind, reference: AssetReference, source: AssetSource) -> Self {
        Self::with_headers(kind, reference, source, BTreeMap::new())
    }

    pub fn with_headers(
        kind: AssetKind,
        reference: AssetReference,
        source: AssetSource,
        headers: BTreeMap<String, String>,
    ) -> Self {
        Self { kind, reference, source, headers }
    }

    pub fn kind(&self) -> AssetKind {
        self.kind
    }

    pub fn reference(&self) -> &AssetReference {
        &self.reference
    }

    pub fn source(&self) -> &AssetSource {
        &self.source
    }

    pub fn header(&self, key: &str) -> Option<&str> {
        self.headers.get(key).map(String::as_str)
    }

    pub fn can_refresh(&self) -> bool {
        self.kind.can_refresh()
    }
}

impl fmt::Display for Asset {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.reference)
    }
}