#vec #condition #string #git #conddb

bin+lib simple_conddb

Simple Condition Database library implemented in Rust

1 unstable release

0.1.0 Apr 8, 2025

#31 in #condition

Download history 13/week @ 2025-11-09 28/week @ 2025-11-16 23/week @ 2025-11-23 23/week @ 2025-11-30 107/week @ 2025-12-07 83/week @ 2025-12-14 52/week @ 2025-12-21 45/week @ 2025-12-28 68/week @ 2026-01-04 46/week @ 2026-01-11 52/week @ 2026-01-18 57/week @ 2026-01-25 32/week @ 2026-02-01 44/week @ 2026-02-08 64/week @ 2026-02-15 37/week @ 2026-02-22

194 downloads per month

Apache-2.0

43KB
1K SLoC

simple_conddb

Simple Condition Database library implemented in Rust, based on Git CondDB.


lib.rs:

Simple Conditions Database library.

The Conditions Database (CondDB) implementation is based on a CondDB type that access a storage layer via a Backend trait. The Backend maps a &str identifier (e.g. a filesystem path) to a Payload type which must implement the IntoCondition trait, so that the CondDB instance can use the information in the Payload instance returned by the Backend.

Examples

A minimal in-memory CondDB implementation can be as simple as

use simple_conddb::backend::{Backend, ConditionCollection};
use simple_conddb::conddb::CondDB;
use simple_conddb::iov::Iov;
use std::collections::HashMap;

type MyPayload = ConditionCollection<String, Iov<u32>>;
struct MyBackend(HashMap<String, MyPayload>);

// Backend implementation
impl Backend for MyBackend {
    type Configuration = HashMap<String, Self::Payload>;
    type Payload = MyPayload;
    fn connect(config: Self::Configuration) -> Result<Self, String> {
        Ok(MyBackend(config))
    }
    fn get(&self, path: &str) -> Option<Self::Payload> {
        self.0.get(path).cloned()
    }
    fn set(&mut self, path: &str, payload: Self::Payload) {
        self.0.insert(String::from(path), payload);
    }
}

// Instantiate the backend and fill it with some data
let mut be = MyBackend::connect(HashMap::new()).unwrap();
be.set(
    "payload1",
    ConditionCollection::Payload(String::from("data 1")),
);
be.set(
    "payload2",
    ConditionCollection::Payload(String::from("data 2")),
);
be.set(
    "condition",
    ConditionCollection::Timeline(vec![
        (Iov::new(0, 100), String::from("payload1")),
        (Iov::new(100, 200), String::from("payload2")),
        (Iov::new(200, u32::MAX), String::from("payload1")),
    ]),
);

// Make a conditions database
let cdb = CondDB::new(be);

// Access conditions
assert_eq!(
    cdb.get("condition", 150).unwrap(),
    (String::from("data 2"), Iov::new(100, 200))
);
assert_eq!(
    cdb.get("condition", 500).unwrap(),
    (String::from("data 1"), Iov::new(200, u32::MAX))
);

The simple memory Backend implementation is also provided with a library call:

use simple_conddb::backend::{Backend, ConditionCollection};
use simple_conddb::backend::mem::make_mem_backend;
use simple_conddb::conddb::CondDB;
use simple_conddb::iov::Iov;

// Instantiate the backend and fill it with some data
let mut be = make_mem_backend();
be.set(
    "payload1",
    ConditionCollection::Payload(String::from("data 1")),
);
be.set(
    "payload2",
    ConditionCollection::Payload(String::from("data 2")),
);
be.set(
    "condition",
    ConditionCollection::Timeline(vec![
        (Iov::new(0, 100), String::from("payload1")),
        (Iov::new(100, 200), String::from("payload2")),
        (Iov::new(200, u32::MAX), String::from("payload1")),
    ]),
);

// Make a conditions database
let cdb = CondDB::new(be);

// Access conditions
assert_eq!(
    cdb.get("condition", 150).unwrap(),
    (String::from("data 2"), Iov::new(100, 200))
);
assert_eq!(
    cdb.get("condition", 500).unwrap(),
    (String::from("data 1"), Iov::new(200, u32::MAX))
);

Crate features

The memory backends is always enabled and, by default also filesystem, JSON and Git backends are enabled, but the last three can be disabled with default-features = false and enabling the required ones with the features fs, json and git.

A simple CLI to query a fs or git backend is available under the feature cli.

Dependencies

~0–17MB
~191K SLoC