turso-orm 0.1.4

An async ORM dedicated to the Turso database, with a typed entity API
Documentation
//! Static identifiers and enum iteration, modeled by [`IdenStatic`] and [`Iterable`].
//!
//! Table and column names are known at compile time, so the entity layer
//! works with `&'static str` identifiers instead of allocating `Ident`s and
//! only converts at the SQL boundary. [`Iterable`] gives the query builders a
//! way to walk every column, key or relation of an entity without reflection;
//! the derive macros fill in `ALL` in declaration order so that generated
//! `SELECT` lists and `INSERT` column lists are stable.
//!
//! [`Static`] is the bridge to `turso_sql`: it is re-exported through the
//! crate's `__private` module for generated code and is not part of the
//! public API.

use turso_sql::{Ident, IntoIden};

/// An identifier known at compile time, such as a table or column name.
pub trait IdenStatic: Copy + Send + Sync + std::fmt::Debug + 'static {
    /// The unquoted name.
    fn as_str(&self) -> &'static str;

    /// Converts the name into an owned SQL identifier.
    fn into_ident(self) -> Ident {
        Ident::new_static(self.as_str())
    }
}

/// An enumeration whose variants can be listed: columns, primary keys and relations.
///
/// Generated by the derive macros; `ALL` is in declaration order.
pub trait Iterable: Sized + Copy + 'static {
    /// Every variant, in declaration order.
    const ALL: &'static [Self];

    /// Iterates over the variants in declaration order.
    fn iter() -> std::iter::Copied<std::slice::Iter<'static, Self>> {
        Self::ALL.iter().copied()
    }
}

/// A wrapper that lets any [`IdenStatic`] be used where a [`turso_sql::IntoIden`] is expected.
#[derive(Clone, Copy, Debug)]
pub struct Static<T: IdenStatic>(pub T);

impl<T: IdenStatic> IntoIden for Static<T> {
    fn into_iden(self) -> Ident {
        self.0.into_ident()
    }
}