Rust

helpers

Module

Identifiers (id), roles (role) and permissions (permission).

Contents

3 submodules.

helpers::id

Own page

Document identifiers: unique generates one, custom takes one given.

use utopia_database::helpers::id::custom;
Rust
pub fn custom(id: impl Into<String>) -> String

An ID given by the caller, as it is.

use utopia_database::helpers::id::unique;
Rust
pub fn unique() -> Result<String>

A new unique ID: 13 hex characters of the current time (uniqid()), then PADDING random hex characters (20 characters in all).

Rust
use utopia_database::helpers::id;

let id = id::unique().unwrap();
assert_eq!(id.len(), 20);
assert!(id.bytes().all(|c| c.is_ascii_hexdigit()));
use utopia_database::helpers::id::unique_padded;
Rust
pub fn unique_padded(padding: i64) -> Result<String>

A new unique ID with padding random hex characters after the 13 of uniqid() (none when padding is 0 or less).

Rust
pub const PADDING: i64 = 7

The random hex characters unique appends to the time-based part.

helpers::permission

Own page

[Permission]: an action granted to a Role, written action("role") (read("any"), update("team:123/owner")).

use utopia_database::helpers::permission::Permission;
Rust
pub struct Permission

A permission: an action and the role it is granted to.

Rust
use utopia_database::helpers::permission::{Permission, PermissionType};
use utopia_database::helpers::role::Role;

let p = Permission::parse("read(\"user:123\")").unwrap();
assert_eq!(p.permission(), PermissionType::Read);
assert_eq!(p.role().identifier(), "123");
assert_eq!(Permission::read(&Role::any()), "read(\"any\")");
assert_eq!(
    Permission::aggregate(&["write(\"any\")"], &PermissionType::ALL).unwrap(),
    ["create(\"any\")", "update(\"any\")", "delete(\"any\")"]
);
Also implements Display.
use utopia_database::helpers::permission::PermissionType;
Rust
pub enum PermissionType

The action a permission grants.

helpers::role

Own page

[Role]: who a permission is granted to (any, users, user:<id>, team:<id>/<dimension>, ...).

use utopia_database::helpers::role::Role;
Rust
pub struct Role

A role: a name, an optional identifier and an optional dimension, written name:identifier/dimension.

Rust
use utopia_database::helpers::role::Role;

let role = Role::parse("team:123/owner").unwrap();
assert_eq!(role.name().as_str(), "team");
assert_eq!(role.identifier(), "123");
assert_eq!(role.dimension(), "owner");
assert_eq!(Role::user("123", "verified").to_string(), "user:123/verified");
assert_eq!(Role::parse("users/a/b").unwrap_err().message(), "Only one dimension can be provided");
Also implements Display.
use utopia_database::helpers::role::RoleName;
Rust
pub enum RoleName

Undocumented.