book.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //! [`Book`]: the transfer-policy scope that gates which accounts and assets
  2. //! take part.
  3. use crate::account::AccountFlags;
  4. use crate::ids::{AccountId, AssetId, BookId};
  5. use serde::{Deserialize, Serialize};
  6. /// A Book is a transfer policy scope. It gates which accounts and assets can
  7. /// take part in a transfer.
  8. ///
  9. /// It is not the chronological entry log, because the transfer log plays that
  10. /// role. It does not partition balances, and it is not a tenancy boundary.
  11. /// Balances are global, and no book restricts a read. A transfer that names
  12. /// [`DEFAULT_BOOK`](crate::DEFAULT_BOOK) with no such book stored commits with
  13. /// no policy applied.
  14. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  15. pub struct Book {
  16. /// Stable identity for this book.
  17. pub id: BookId,
  18. /// Human-readable name.
  19. pub name: String,
  20. /// Participation rules for this book.
  21. pub policy: BookPolicy,
  22. }
  23. /// The rules a [`Book`] applies. An empty field means "no restriction".
  24. #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
  25. pub struct BookPolicy {
  26. /// If non-empty, these assets alone can appear in a movement.
  27. pub allowed_assets: Vec<AssetId>,
  28. /// If non-empty, an account with ANY of these flags can take part.
  29. pub allowed_flags: AccountFlags,
  30. /// If non-empty, these specific accounts can take part, on top of the ones
  31. /// the flags admit.
  32. pub allowed_accounts: Vec<AccountId>,
  33. }
  34. /// The builder that assembles a [`Book`].
  35. pub struct BookBuilder {
  36. book: Book,
  37. }
  38. impl BookBuilder {
  39. /// Create a new book builder with the given name.
  40. pub fn new(name: impl Into<String>) -> Self {
  41. Self {
  42. book: Book {
  43. id: BookId::generate(),
  44. name: name.into(),
  45. policy: BookPolicy {
  46. allowed_assets: Vec::new(),
  47. allowed_flags: AccountFlags::empty(),
  48. allowed_accounts: Vec::new(),
  49. },
  50. },
  51. }
  52. }
  53. /// Set the book id explicitly.
  54. pub fn id(mut self, id: BookId) -> Self {
  55. self.book.id = id;
  56. self
  57. }
  58. /// Add an allowed asset.
  59. pub fn allow_asset(mut self, asset: impl Into<AssetId>) -> Self {
  60. self.book.policy.allowed_assets.push(asset.into());
  61. self
  62. }
  63. /// Set the allowed account flags. An account with ANY of them can take
  64. /// part.
  65. pub fn allow_flags(mut self, flags: AccountFlags) -> Self {
  66. self.book.policy.allowed_flags = flags;
  67. self
  68. }
  69. /// Add a specific allowed account.
  70. pub fn allow_account(mut self, account: AccountId) -> Self {
  71. self.book.policy.allowed_accounts.push(account);
  72. self
  73. }
  74. /// Consume the builder and return the [`Book`].
  75. pub fn build(self) -> Book {
  76. self.book
  77. }
  78. }