inputs.rs (4129B)
1 use crate::Error; 2 use radroots::domain::{CorrelationId, OperationId, SourceId, ThreadId, UnixSeconds}; 3 use std::sync::Arc; 4 5 /// Explicit first-issue context for the currently supported known commands. 6 #[derive(Clone, uniffi::Record)] 7 pub struct CommandRequest { 8 pub operation_id: String, 9 pub first_issue_not_after: String, 10 pub correlation_id: Option<String>, 11 } 12 impl CommandRequest { 13 pub(crate) fn capture( 14 self, 15 scope: &radroots::WorkspaceSession, 16 ) -> Result<radroots::CommandContext, Error> { 17 let operation: OperationId = self 18 .operation_id 19 .parse() 20 .map_err(|_| Error::input("operation_id"))?; 21 let time: UnixSeconds = self 22 .first_issue_not_after 23 .parse() 24 .map_err(|_| Error::input("first_issue_not_after"))?; 25 let correlation = self 26 .correlation_id 27 .map(CorrelationId::new) 28 .transpose() 29 .map_err(|_| Error::input("correlation_id"))?; 30 scope 31 .command_context_with_correlation(operation, None, None, time, correlation) 32 .map_err(Error::from) 33 } 34 } 35 36 /// Validated shared public input. No participant or installation is inferred. 37 #[derive(uniffi::Object)] 38 pub struct ListingInput { 39 pub(crate) inner: radroots::listings::PublishInput, 40 } 41 #[uniffi::export] 42 impl ListingInput { 43 /// Runs the existing bounded, closed shared parser, before host effects. 44 #[uniffi::constructor] 45 pub fn parse(input: Vec<u8>) -> Result<Arc<Self>, Error> { 46 let inner = radroots::listings::PublishInput::parse(&input) 47 .map_err(|error| Error::command("listing_input", error))?; 48 Ok(Arc::new(Self { inner })) 49 } 50 } 51 52 /// Public reference descriptor; it authenticates no event, terms or stock. 53 #[derive(Clone, uniffi::Record)] 54 pub struct ListingReference { 55 pub author: String, 56 pub discriminator: String, 57 pub event_id: String, 58 } 59 impl ListingReference { 60 fn capture(&self) -> Result<radroots::inbox::ListingRevisionRef, Error> { 61 let author = public_key(&self.author, "listing_author")?; 62 let event: SourceId = self 63 .event_id 64 .parse() 65 .map_err(|_| Error::input("listing_event_id"))?; 66 radroots::inbox::ListingRevisionRef::new(author, &self.discriminator, event) 67 .map_err(|error| Error::command("listing_reference", error)) 68 } 69 } 70 71 /// Shared bounded private input, retained with the source's zeroizing owner. 72 #[derive(uniffi::Object)] 73 pub struct InquiryInput { 74 inner: radroots::inbox::InquireInput, 75 } 76 #[uniffi::export] 77 impl InquiryInput { 78 #[uniffi::constructor] 79 pub fn new( 80 thread_id: String, 81 peer: String, 82 source: Option<ListingReference>, 83 text: String, 84 ) -> Result<Arc<Self>, Error> { 85 let text = zeroize::Zeroizing::new(text); 86 let thread: ThreadId = thread_id.parse().map_err(|_| Error::input("thread_id"))?; 87 let peer = public_key(&peer, "peer")?; 88 let source = source.as_ref().map(ListingReference::capture).transpose()?; 89 let inner = radroots::inbox::InquireInput::new(thread, peer, source, &text) 90 .map_err(|error| Error::command("inquiry_input", error))?; 91 Ok(Arc::new(Self { inner })) 92 } 93 } 94 impl InquiryInput { 95 // Explicit bounded copy into the one execution owner. The retained input 96 // keeps its own zeroizing storage; there is no unprotected string cache. 97 pub(crate) fn captured_copy(&self) -> Result<radroots::inbox::InquireInput, Error> { 98 radroots::inbox::InquireInput::new( 99 self.inner.thread_id(), 100 self.inner.peer(), 101 self.inner.source().cloned(), 102 self.inner.text(), 103 ) 104 .map_err(|error| Error::command("inquiry_input", error)) 105 } 106 } 107 108 fn public_key(value: &str, field: &'static str) -> Result<radroots::identity::PublicKey, Error> { 109 let key = radroots::identity::PublicKey::from_hex(value).map_err(|_| Error::input(field))?; 110 if key.to_hex() != value { 111 return Err(Error::input(field)); 112 } 113 Ok(key) 114 }