client.rs (4281B)
1 use crate::{CommandRequest, Error, InquiryInput, ListingInput, Receipt, SessionState, StartMode}; 2 use radroots::domain::{CommandDigest, OperationId}; 3 use std::sync::Arc; 4 5 /// Holds the actual native owner; no FFI runtime or outbox is added. 6 #[derive(uniffi::Object)] 7 pub struct NativeClient { 8 inner: radroots::Client, 9 } 10 impl NativeClient { 11 /// Native host startup supplies its explicitly configured shared client. 12 /// This Rust constructor performs no additional attachment or activation. 13 pub fn from_native(inner: radroots::Client) -> Arc<Self> { 14 Arc::new(Self { inner }) 15 } 16 } 17 18 /// Holds an actual authenticated shared scope. Descriptors cannot construct it. 19 #[derive(uniffi::Object)] 20 pub struct Workspace { 21 inner: radroots::WorkspaceSession, 22 } 23 impl Workspace { 24 /// Injects a scope obtained by actual shared-client authentication. The 25 /// client still enforces that it owns this scope on every command/query. 26 pub fn from_session(inner: radroots::WorkspaceSession) -> Arc<Self> { 27 Arc::new(Self { inner }) 28 } 29 } 30 #[uniffi::export] 31 impl Workspace { 32 /// Releases this attachment's credential root without revoking its grant. 33 pub fn close(&self) { 34 self.inner.close(); 35 } 36 } 37 38 #[uniffi::export(async_runtime = "tokio")] 39 impl NativeClient { 40 pub fn state(&self) -> SessionState { 41 self.inner.state().into() 42 } 43 pub async fn start(&self, mode: StartMode) -> Result<SessionState, Error> { 44 self.inner 45 .start(mode.into()) 46 .await 47 .map(|v| v.state().into()) 48 .map_err(Error::from) 49 } 50 /// Completion is the shared owner's actual confirmed resource closure. 51 pub async fn close(&self) -> Result<(), Error> { 52 self.inner.close().await.map(|_| ()).map_err(Error::from) 53 } 54 pub async fn publish_listing( 55 &self, 56 workspace: Arc<Workspace>, 57 request: CommandRequest, 58 input: Arc<ListingInput>, 59 ) -> Result<Receipt, Error> { 60 let context = request.capture(&workspace.inner)?; 61 let receipt = self 62 .inner 63 .listings() 64 .publish(context, input.inner.clone()) 65 .await 66 .map_err(Error::from)?; 67 Receipt::listing(&receipt) 68 } 69 pub async fn inquire( 70 &self, 71 workspace: Arc<Workspace>, 72 request: CommandRequest, 73 input: Arc<InquiryInput>, 74 ) -> Result<Receipt, Error> { 75 let context = request.capture(&workspace.inner)?; 76 let receipt = self 77 .inner 78 .inbox() 79 .inquire(context, input.captured_copy()?) 80 .await 81 .map_err(Error::from)?; 82 Receipt::chat(&receipt) 83 } 84 /// Scoped observation only; absence never proves safe nonissuance. 85 pub async fn operation( 86 &self, 87 workspace: Arc<Workspace>, 88 operation_id: String, 89 expected_digest: Option<String>, 90 ) -> Result<Option<Receipt>, Error> { 91 let (operation, expected) = selector(&operation_id, expected_digest.as_deref())?; 92 self.inner 93 .operations(&workspace.inner) 94 .get_matching(operation, expected) 95 .await 96 .map_err(Error::from)? 97 .as_ref() 98 .map(Receipt::status) 99 .transpose() 100 } 101 /// Explicit continuation through the original shared owner and policy. 102 pub async fn resume( 103 &self, 104 workspace: Arc<Workspace>, 105 operation_id: String, 106 expected_digest: Option<String>, 107 ) -> Result<Receipt, Error> { 108 let (operation, expected) = selector(&operation_id, expected_digest.as_deref())?; 109 let receipt = self 110 .inner 111 .operations(&workspace.inner) 112 .resume(operation, expected) 113 .await 114 .map_err(Error::from)?; 115 Receipt::status(&receipt) 116 } 117 } 118 fn selector( 119 operation: &str, 120 expected: Option<&str>, 121 ) -> Result<(OperationId, Option<CommandDigest>), Error> { 122 let operation = operation 123 .parse() 124 .map_err(|_| Error::input("operation_id"))?; 125 let expected = expected 126 .map(str::parse) 127 .transpose() 128 .map_err(|_| Error::input("command_digest"))?; 129 Ok((operation, expected)) 130 }