Skip to main content

Crate safe_en

Crate safe_en 

Source
Expand description

§SafeEn

Local database solution with clean and strict data integrity.

SafeEn stores a whole database in a single portable file. Every column has a declared type, every value is checked against it on insert, and every file carries a checksum so corruption is reported rather than silently loaded. The library itself has no dependencies.

§Usage

use safe_en::{
    table::{TableRow, TypeDefs},
    Database,
};

let mut db = Database::new();
db.set_name("users");

db.create_table(
    "users",
    vec![
        TableRow::new("id", TypeDefs::I64),
        TableRow::new("email", TypeDefs::String),
    ],
)
.unwrap();

db.table("users")
    .unwrap()
    .insert(vec![1_i64.into(), "ahmet@mail.com".into()])
    .unwrap();

let matches = db
    .table("users")
    .unwrap()
    .get_where(|row| row.row("email").is("ahmet@mail.com"));

assert_eq!(matches.len(), 1);

You can find more examples here.

§File format

Files written by this release start with the magic bytes SFEN, a format version, and end with a CRC-32 of the payload. Files written by SafeEn 1.x have none of those and are detected and read automatically, so existing databases keep working; saving always writes the current version.

§no_std

The crate is no_std and needs only alloc. The default std feature adds Database::save and Database::load, which are thin filesystem wrappers over Database::to_bytes and Database::from_bytes. Those two are the real interface and are always available, so on a target with no filesystem – embedded, or a browser via WebAssembly – you hand the bytes to whatever storage you do have:

let bytes = db.to_bytes();
// ... write `bytes` to flash, localStorage, IndexedDB, a socket ...
let restored = safe_en::Database::from_bytes(&bytes).unwrap();
safe_en = { version = "2.0", default-features = false }

Bytes that reach Database::from_bytes are frequently outside your control – a browser’s local storage is editable by the user, and flash can be damaged. The loader therefore never panics on malformed input, which matters most on WebAssembly, where a panic aborts the entire module.

Re-exports§

pub use durable::Durable;
pub use durable::DurableError;
pub use error::SaveError;std
pub use error::ConstraintError;
pub use error::LoadError;
pub use error::TableError;
pub use filter::Filter;
pub use filter::Op;

Modules§

durable
A database that survives a crash A database that survives a crash.
error
Error types Error types returned by SafeEn.
filter
Runtime-built filters Filters that can be built at runtime.
query
Query values and comparisons Values and comparisons used when filtering rows.
storage
Where a database’s bytes live Where a database’s bytes live.
table
Database table
utils
Database utils Byte-level encoding and decoding for the on-disk format.

Macros§

coltype
Builds a TypeDefs from a short type name.
columns
Builds a Vec<TableRow> – a table’s column definitions.
database
Builds a whole Database from a schema.
fields
Builds a Vec<Entry> – values named by column.
query
Builds a row filter closure from a comparison expression.
row
Builds a row for Table::insert.

Structs§

Database
An in-memory database of typed tables that can be saved to a single file.