Crate exemplar

source ·
Expand description

Exemplar is a boilerplate eliminator for rusqlite.

Getting Started

A taste of what you can do:

#[derive(Debug, PartialEq, Model)]
#[table("users")]
#[check("../tests/schema.sql")]
struct User {
   username: String,
   #[bind(bind_path)]
   #[extr(extr_path)]
   home_dir: PathBuf,
   #[column("pwd")]
   password: Vec<u8>,
}
 
fn main() -> Result<()> {
    let conn = Connection::open_in_memory()?;
 
    conn.execute_batch(
        include_str!("../tests/schema.sql")
    )?;
 
    let alice = User {
        username: "Alice".to_owned(),
        home_dir: "/var/home/alice".into(),
        password: b"hunter2".to_vec()
    };
 
    let bob = User {
        username: "Bob".to_owned(),
        home_dir: "/var/home/robert".into(),
        password: b"password".to_vec()
    };
 
    alice.insert(&conn)?;
    bob.insert(&conn)?;
 
    let mut stmt = conn.prepare("SELECT * FROM users ORDER BY username ASC")?;
    let mut iter = stmt.query_and_then([], User::from_row)?;
 
    assert_eq!(alice, iter.next().unwrap()?);
    assert_eq!(bob, iter.next().unwrap()?);
 
    Ok(())
}

Exemplar is based around the Model trait, which has its own derive macro.

  • See the aformentioned macro’s documentation to get started.
  • For handling enums in models, check out the sql_enum macro.
  • For working with “anonymous” record types, look at the record macro.

Macros

  • Generate an “anonymous” record struct that implements from_row.
  • Generate an SQL-compatible field-less enum.

Structs

Enums

Traits

  • An interface for types that model SQLite database tables.

Type Aliases

  • Type alias for the outcome of converting a value to an SQL-friendly representation.
  • Type alias for the outcome of extracting a value from a Row.
  • Type alias for a boxed slice of named query parameters.

Derive Macros