stock.rs (6305B)
1 //! Typed private stock commands through one selected authenticated attachment. 2 use crate::{Client, CommandContext, OperationContext, Result, WorkspaceSession}; 3 use radroots_domain::{ 4 CommandDigest, OperationId, PoolId, 5 stock::{AvailableStockCorrection, OptionMappingKey, OptionPoolMapping, PoolView}, 6 }; 7 pub use radroots_protocol::command::{CreatePoolInput, MapOptionInput, PoolCorrectionInput}; 8 pub use radroots_storage::inventory::{ 9 InventoryCheckpointRef, InventoryOutcome, InventoryReceipt, StockDeficit, StockSnapshot, 10 }; 11 pub use radroots_storage::inventory::{ 12 StockEvidenceOutcome, StockEvidenceReceipt, StockEvidenceRequest, 13 }; 14 15 /// Borrowed access to this client's explicitly selected private stock owner. 16 pub struct Stock<'a> { 17 client: &'a Client, 18 scope: WorkspaceSession, 19 } 20 impl<'a> Stock<'a> { 21 pub(crate) const fn new(client: &'a Client, scope: WorkspaceSession) -> Self { 22 Self { client, scope } 23 } 24 /// Retains already obtained original evidence through actual append and 25 /// caller loss. Failed or uncertain capture remains owned for explicit retry 26 /// or close draining. This supplies no signing or installation permission. 27 pub async fn capture_evidence( 28 &self, 29 request: &StockEvidenceRequest, 30 ) -> Result<StockEvidenceOutcome> { 31 self.client 32 .session 33 .capture_stock_evidence(self.client.inventory()?, &self.scope, request) 34 .await 35 } 36 /// Retries this client's retained original captures without obtaining new 37 /// signatures, changing captured bytes or renewing their original windows. 38 pub async fn drain_evidence(&self) -> Result<()> { 39 self.client.session.drain_stock_evidence().await 40 } 41 /// Creates a private pool with actual atomic conservation and idempotency. 42 /// The report claims neither Nostr signing nor transport delivery. 43 pub async fn create_pool( 44 &self, 45 context: CommandContext, 46 minimum: InventoryCheckpointRef, 47 input: CreatePoolInput, 48 ) -> Result<InventoryOutcome> { 49 self.client 50 .session 51 .create_stock_pool( 52 self.client.inventory()?, 53 &self.scope, 54 context, 55 minimum, 56 input, 57 ) 58 .await 59 } 60 /// Applies an explicit positive addition or physical-stock removal. 61 /// Qualified hosts retain deficits if installed original backing is lost. 62 pub async fn adjust( 63 &self, 64 context: CommandContext, 65 minimum: InventoryCheckpointRef, 66 input: PoolCorrectionInput, 67 ) -> Result<InventoryOutcome> { 68 if input.kind() == AvailableStockCorrection::Spoil { 69 return Err(invalid(&context)); 70 } 71 self.correct(context, minimum, input).await 72 } 73 /// Records physical spoilage and retains affected installed obligations. 74 /// Preparatory and quarantine claims require their explicit resolution. 75 pub async fn spoil( 76 &self, 77 context: CommandContext, 78 minimum: InventoryCheckpointRef, 79 input: PoolCorrectionInput, 80 ) -> Result<InventoryOutcome> { 81 if input.kind() != AvailableStockCorrection::Spoil { 82 return Err(invalid(&context)); 83 } 84 self.correct(context, minimum, input).await 85 } 86 async fn correct( 87 &self, 88 context: CommandContext, 89 minimum: InventoryCheckpointRef, 90 input: PoolCorrectionInput, 91 ) -> Result<InventoryOutcome> { 92 self.client 93 .session 94 .correct_stock_pool( 95 self.client.inventory()?, 96 &self.scope, 97 context, 98 minimum, 99 input, 100 ) 101 .await 102 } 103 /// Updates an owner-private option mapping with exact expected progress. 104 pub async fn map_option( 105 &self, 106 context: CommandContext, 107 minimum: InventoryCheckpointRef, 108 input: MapOptionInput, 109 ) -> Result<InventoryOutcome> { 110 self.client 111 .session 112 .map_stock_option( 113 self.client.inventory()?, 114 &self.scope, 115 context, 116 minimum, 117 input, 118 ) 119 .await 120 } 121 /// Inspects current physical stock and pool-backed installed obligations at 122 /// one confirmed checkpoint. Historical correction receipts are separate. 123 pub async fn inspect( 124 &self, 125 minimum: InventoryCheckpointRef, 126 pool: PoolId, 127 ) -> Result<Option<StockSnapshot>> { 128 self.client 129 .session 130 .inspect_stock_pool(self.client.inventory()?, &self.scope, minimum, pool) 131 .await 132 } 133 /// Reads the actual protected pool under an independently retained minimum. 134 pub async fn pool( 135 &self, 136 minimum: InventoryCheckpointRef, 137 pool: PoolId, 138 ) -> Result<Option<PoolView>> { 139 self.client 140 .session 141 .stock_pool(self.client.inventory()?, &self.scope, minimum, pool) 142 .await 143 } 144 /// Reads a private mapping; absence supplies no default allocation. 145 pub async fn option_mapping( 146 &self, 147 minimum: InventoryCheckpointRef, 148 key: OptionMappingKey, 149 ) -> Result<Option<OptionPoolMapping>> { 150 self.client 151 .session 152 .stock_option_mapping(self.client.inventory()?, &self.scope, minimum, key) 153 .await 154 } 155 /// Recovers an original private effect, without executing a fresh command. 156 pub async fn operation( 157 &self, 158 minimum: InventoryCheckpointRef, 159 operation: OperationId, 160 digest: Option<CommandDigest>, 161 ) -> Result<Option<InventoryReceipt>> { 162 self.client 163 .session 164 .inventory_operation( 165 self.client.inventory()?, 166 &self.scope, 167 minimum, 168 operation, 169 digest, 170 ) 171 .await 172 } 173 } 174 fn invalid(context: &CommandContext) -> crate::Error { 175 crate::Error::operation( 176 radroots_storage::command::OperationError::InvalidCommand, 177 OperationContext::proposed(Some(context.data().operation_id())), 178 ) 179 }