| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- //! [`Book`]: the transfer-policy scope that gates which accounts and assets
- //! take part.
- use crate::account::AccountFlags;
- use crate::ids::{AccountId, AssetId, BookId};
- use serde::{Deserialize, Serialize};
- /// A Book is a transfer policy scope. It gates which accounts and assets can
- /// take part in a transfer.
- ///
- /// It is not the chronological entry log, because the transfer log plays that
- /// role. It does not partition balances, and it is not a tenancy boundary.
- /// Balances are global, and no book restricts a read. A transfer that names
- /// [`DEFAULT_BOOK`](crate::DEFAULT_BOOK) with no such book stored commits with
- /// no policy applied.
- #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
- pub struct Book {
- /// Stable identity for this book.
- pub id: BookId,
- /// Human-readable name.
- pub name: String,
- /// Participation rules for this book.
- pub policy: BookPolicy,
- }
- /// The rules a [`Book`] applies. An empty field means "no restriction".
- #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
- pub struct BookPolicy {
- /// If non-empty, these assets alone can appear in a movement.
- pub allowed_assets: Vec<AssetId>,
- /// If non-empty, an account with ANY of these flags can take part.
- pub allowed_flags: AccountFlags,
- /// If non-empty, these specific accounts can take part, on top of the ones
- /// the flags admit.
- pub allowed_accounts: Vec<AccountId>,
- }
- /// The builder that assembles a [`Book`].
- pub struct BookBuilder {
- book: Book,
- }
- impl BookBuilder {
- /// Create a new book builder with the given name.
- pub fn new(name: impl Into<String>) -> Self {
- Self {
- book: Book {
- id: BookId::generate(),
- name: name.into(),
- policy: BookPolicy {
- allowed_assets: Vec::new(),
- allowed_flags: AccountFlags::empty(),
- allowed_accounts: Vec::new(),
- },
- },
- }
- }
- /// Set the book id explicitly.
- pub fn id(mut self, id: BookId) -> Self {
- self.book.id = id;
- self
- }
- /// Add an allowed asset.
- pub fn allow_asset(mut self, asset: impl Into<AssetId>) -> Self {
- self.book.policy.allowed_assets.push(asset.into());
- self
- }
- /// Set the allowed account flags. An account with ANY of them can take
- /// part.
- pub fn allow_flags(mut self, flags: AccountFlags) -> Self {
- self.book.policy.allowed_flags = flags;
- self
- }
- /// Add a specific allowed account.
- pub fn allow_account(mut self, account: AccountId) -> Self {
- self.book.policy.allowed_accounts.push(account);
- self
- }
- /// Consume the builder and return the [`Book`].
- pub fn build(self) -> Book {
- self.book
- }
- }
|