Skip to main content

Crate databoard

Crate databoard 

Source
Expand description

§databoard

Implementation of a hierarchical key-value-store with the possibility to do a remapping from a level up to its parent level.

It supports both std and no_std environments (requires alloc).

  • Each entry is a dataport port, so values can be accessed through the databoard as well as through port handles connected to the entry.
  • The value accessors are async, as they wait for the value to be unlocked; the non-waiting variants try_get and try_set fail on a locked value instead.
  • The databoard implements the tinyscript Environment, so scripts can read and write its entries.
  • The local entries of a databoard can be saved with backup and written back with restore.

§Hierarchy

The following restrictions apply:

  • Each level has at most one parent.
  • There is no remapping or access down the hierarchy.
  • The remapping is evaluated recursively up the levels.

You can do the remapping either

  • fully automatic,
  • fully manual or
  • do an automatic remapping with manual overrides.

For controlling the access in the hierarchy there are two kinds of special keys:

  • keys prefixed with an @ redirect to the top level databoard of the hierarchy,
  • keys prefixed with an _ restrict the access to the current databoard.

A key is resolved in the following order:

  1. A key @name is resolved as name by the top level databoard.
  2. A key _name is stored as name in the current databoard.
  3. A key with a manual remapping is resolved by its remapping target, see below.
  4. Any other key is resolved by the parent, if automatic remapping is enabled and there is a parent, otherwise it is stored in the current databoard.

§Remapping targets

TargetMeaning
{name}the entry name of the parent
{@name}the entry name of the top level databoard
{_name}the entry name of the parent, but not further up the hierarchy
{=}shortcut for {key}, the entry of the parent with the same name
literal, 42, {x: 1, y: 2}a constant, readable with get_from_str into a type implementing FromStr

A target that is not wrapped in braces, or that contains any of the characters :, " and ', is a constant.

§Usage

A standalone databoard:

use databoard::prelude::*;

// instantiation of a default Databoard
let mut databoard = Databoard::default();
// setting the value creates the entry, it may fail, so it returns a `Result`
databoard.set("test", 42).await.unwrap();
// getting the value may fail, so it returns a `Result`
let value = databoard.get::<i32>("test").await.unwrap();
assert_eq!(value, 42);
// replacing the value returns the previous value
let old = databoard.replace("test", 43).await.unwrap();
assert_eq!(old, Some(42));
// removing the entry returns its port, which still holds the value
let port = databoard.remove("test").unwrap();
assert_eq!(port.try_get::<i32>(), Ok(Some(43)));

Hierarchical usage:

use databoard::prelude::*;

let mut top_level = Databoard::default();
// this creates a databoard with automatic remapping to parent
let level1 = Databoard::with_parent(top_level.clone());
// some remapping rules
let mut remappings = RemappingList::default();
remappings.add("test".into(), "{test}".into()).unwrap();
remappings.add("other_test".into(), "{test}".into()).unwrap();
remappings.add("limit".into(), "100".into()).unwrap();
// this creates a databoard with manual remapping to parent using the defined remapping rules
let level2 = Databoard::with(Some(level1.clone()), Some(remappings), false);

// sets the value in the top `databoard`
top_level.set("test", 42).await.unwrap();
// but it can also be accessed from the two other levels
let value1: i32 = level1.get("test").await.unwrap();
let value2: i32 = level2.get("other_test").await.unwrap();
assert_eq!((value1, value2), (42, 42));
// a constant from the remappings
let limit: u8 = level2.get_from_str("limit").await.unwrap();
assert_eq!(limit, 100);

Port handles and backup:

use databoard::prelude::*;

let mut databoard = Databoard::default();
databoard.set("counter", 0_u32).await.unwrap();

// a handle is connected to the entry, every kind of handle is available for every entry
let handle = databoard.in_out_handle::<u32>("counter").unwrap();
let backup = databoard.backup().unwrap();
*handle.write().await = Some(1);
assert_eq!(databoard.get::<u32>("counter").await.unwrap(), 1);

// restoring keeps the connection, so the handle sees the restored value
databoard.restore(&backup).unwrap();
assert_eq!(*handle.read().await, Some(0));

§Features

  • std (default): use the standard library. Without it the crate is no_std and requires alloc.

The minimum supported Rust version is 1.95.0.

§License

Licensed with the fair use “NGMC” license, see license file

§Contribution

Any contribution intentionally submitted for inclusion in the work by you, shall be licensed with the same “NGMC” license, without any additional terms or conditions.

Modules§

prelude
Most commonly used interfaces of databoard.

Structs§

Databoard
A hierarchical, thread safe name/value store. The name is something that can be converted into an Arc<str>. The value can be anything that implements AnyPortValue, which are the traits Any, Send, Sync, Debug and Clone. Each entry is stored as an InOut port.
DataboardBackup
A backup of the entries in the local database of a Databoard, created by Databoard::backup and written back by Databoard::restore.
RemappingList
A mutable list of manual remappings of a Databoard to its parent, each a key with a target.

Enums§

Error
Things that may go wrong using the Databoard.
RemappingTarget
Target of a remapping entry, created from its textual form with FromStr, see RemappingList for the syntax.

Functions§

check_board_pointer
Returns the name within the braces if key is a Databoard pointer, see is_board_pointer.
check_local_key
Returns the name without the prefix _ if key is a key for the local database of a Databoard.
check_top_level_key
Returns the name without the prefix @ if key is a key for the top level Databoard.
is_board_pointer
Returns true if key is a pointer into a Databoard, otherwise false.
is_const_assignment
Returns true if key is a constant assignment, otherwise false.
strip_board_pointer
Returns the name within the braces if key is a Databoard pointer, otherwise None.

Type Aliases§

Arc
A thread safe reference counting pointer. Uses portable-atomic, so it also works on targets without native atomic operations.
BlockingMutex
A mutex for short, synchronous accesses from within a critical section.
Mutex
An async mutex, which may be held across .await points.
MutexGuard
The guard of a locked Mutex, releasing the lock when dropped.
RawMutex
The raw mutex used by all locks. On std each lock has its own reentrant mutex instead of one process-wide critical section.
RwLock
An async reader-writer lock, allowing multiple readers or a single writer.
RwLockReadGuard
The guard of a shared read access to a RwLock, releasing the access when dropped.
RwLockWriteGuard
The guard of an exclusive write access to a RwLock, releasing the access when dropped.