hinge 0.1.0

SQL-native ELT engine — dependency graph resolved automatically from FROM/JOIN clauses, parallel execution, single binary
Documentation
use crate::domain::asset::error::AssetError;

/// The type of database object an asset produces.
///
/// Declared via `-- @kind: <value>` at the top of the SQL file.
/// Defaults to [`View`](AssetKind::View) when the header is absent.
///
/// # SQL emitted per database
///
/// | Kind | PostgreSQL | ClickHouse | DuckDB |
/// |------|-----------|-----------|--------|
/// | `View` | `CREATE OR REPLACE VIEW` | `CREATE OR REPLACE VIEW` | `CREATE OR REPLACE VIEW` |
/// | `Table` | `CREATE TABLE … AS SELECT` | `CREATE OR REPLACE TABLE` | `CREATE OR REPLACE TABLE` |
/// | `MaterializedView` | `CREATE MATERIALIZED VIEW … REFRESH` | `CREATE MATERIALIZED VIEW` | mapped to `Table` |
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum AssetKind {
    /// A persisted table rebuilt from scratch on each run.
    Table,
    /// A standard SQL view (`CREATE OR REPLACE VIEW`). Default.
    View,
    /// A pre-computed view (PostgreSQL / ClickHouse native; mapped to `Table` on DuckDB).
    MaterializedView,
}

impl AssetKind {
    pub fn as_str(&self) -> &'static str {
        match self {
            AssetKind::Table => "table",
            AssetKind::View => "view",
            AssetKind::MaterializedView => "materialized_view",
        }
    }

    pub fn can_refresh(&self) -> bool {
        matches!(self, AssetKind::MaterializedView)
    }
}

impl std::fmt::Display for AssetKind {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for AssetKind {
    type Err = AssetError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "table" => Ok(AssetKind::Table),
            "view" => Ok(AssetKind::View),
            "materialized_view" | "matview" => Ok(AssetKind::MaterializedView),
            other => Err(AssetError::InvalidKind(other.to_string())),
        }
    }
}