weirdnet-collab 0.1.1

Collaboration control for Weirdnet apps: project members and roles, signed allow and deny rules, owner consent grants, a decision with a reason chain, invites and a signed, mergeable audit log. App-agnostic: the app defines what a project, role and permission are.
Documentation
//! A decision and the reason chain behind it.
use crate::ids::{Permission, RecordId, RoleId};
use crate::records::{Duration, Effect};
use serde::{Deserialize, Serialize};
use weirdnet_contacts::UserId;

/// The answer.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Decision {
    /// Yes.
    Allowed,
    /// No.
    Denied,
    /// Not yet: the audio's owner has to consent first.
    NeedsConsent {
        /// Whose consent.
        owner: UserId,
    },
}

/// One step of the reasoning, in the order the rules ran.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Step {
    /// The caller is banned from the project.
    Banned,
    /// The caller is not a member.
    NotMember,
    /// The session is closed.
    SessionClosed,
    /// A rule for this person decided.
    MemberRule {
        /// The rule.
        rule: RecordId,
        /// What it says.
        effect: Effect,
        /// Who set it.
        set_by: UserId,
        /// When.
        set_ms: i64,
    },
    /// A rule for the person's role decided.
    RoleRule {
        /// The role.
        role: RoleId,
        /// The rule.
        rule: RecordId,
        /// What it says.
        effect: Effect,
        /// Who set it.
        set_by: UserId,
        /// When.
        set_ms: i64,
    },
    /// A matching rule had expired and was ignored.
    ExpiredRule {
        /// The rule.
        rule: RecordId,
    },
    /// No rule decided; the role's default did.
    RoleDefault {
        /// The role.
        role: RoleId,
        /// Whether it has the permission.
        granted: bool,
    },
    /// The audio is the caller's own: no consent needed.
    OwnAudio,
    /// A consent grant from the audio's owner covers it.
    Consent {
        /// The grant.
        grant: RecordId,
        /// The owner.
        owner: UserId,
        /// Once or always.
        duration: Duration,
    },
    /// The owner has not consented.
    ConsentMissing {
        /// The owner.
        owner: UserId,
    },
    /// A gated permission was asked for without saying whose audio it is.
    NoOwnerNamed,
    /// A switch only the caller's device can check.
    LocalGate {
        /// The switch.
        name: String,
    },
    /// Nothing allowed it.
    DefaultDeny,
}

/// A decision with its chain.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Explained {
    /// The asker.
    pub who: UserId,
    /// The permission.
    pub permission: Permission,
    /// The answer.
    pub decision: Decision,
    /// The steps that led to it.
    pub chain: Vec<Step>,
}

impl Explained {
    /// Whether it is a plain yes.
    pub fn allowed(&self) -> bool {
        self.decision == Decision::Allowed
    }
}