Skip to main content
QAROS Rust API Dev (main) · versions: Dev (main) · Developer Guide

qaros/
invite.rs

1use std::ptr::{self, NonNull};
2use std::rc::Rc;
3
4use qaros_sys::{self as sys, defaults};
5
6use crate::callbacks::BlockingOptions;
7use crate::error::{Error, Result, StatusCode, check};
8use crate::library::{LibraryInner, require_handle};
9use crate::session::SessionInner;
10use crate::strings;
11
12/// A `QarOnboardingInvite`: a full invite blob, onboarded with [`crate::OnboardMethod::Invite`].
13/// A co-process invite ([`crate::Session::request_onboarding_invite`]) or the launch invite
14/// QAROS hands an app it starts. A secret: hand it over a private channel (stdin, a pipe), never
15/// on a command line. A co-process invite names the requesting app on this PC and works for every
16/// start of the co-process while the app's session lives; onboarding with it fetches a fresh hub
17/// invite from the app on each attempt and retries until it joins.
18#[derive(Debug)]
19pub struct OnboardingInvite {
20    handle: NonNull<sys::QarOnboardingInvite>,
21    library: Rc<LibraryInner>,
22}
23
24impl OnboardingInvite {
25    pub(crate) fn request(
26        session: &Rc<SessionInner>,
27        app_tag: &str,
28        options: &BlockingOptions<'_>,
29    ) -> Result<Self> {
30        let api = session.api();
31        let app_tag_text = strings::to_c_string(app_tag, "app tag")?;
32        let mut init = defaults::qar_request_invite_init_default();
33        init.app_tag = app_tag_text.as_ptr();
34        let mut handle: *mut sys::QarOnboardingInvite = ptr::null_mut();
35        // SAFETY: live session; init is stamped and its tag outlives the call; options borrow
36        // the progress sender and token for the call; valid out-pointer.
37        let result = unsafe {
38            (api.qar_impl_session_request_onboarding_invite)(
39                session.handle.as_ptr(),
40                &raw const init,
41                options.progress_callback(),
42                options.progress_state(),
43                options.cancel_pointer(),
44                &raw mut handle,
45            )
46        };
47        check(api, "qar_session_request_onboarding_invite", result)?;
48        Ok(Self {
49            handle: require_handle(handle, "qar_session_request_onboarding_invite")?,
50            library: Rc::clone(session.library()),
51        })
52    }
53
54    /// Takes ownership of an invite handle the library handed over.
55    pub(crate) fn from_handle(
56        handle: NonNull<sys::QarOnboardingInvite>,
57        library: &Rc<LibraryInner>,
58    ) -> Self {
59        Self {
60            handle,
61            library: Rc::clone(library),
62        }
63    }
64
65    pub(crate) fn deserialize(library: &Rc<LibraryInner>, wire_bytes: &[u8]) -> Result<Self> {
66        let api = &library.api;
67        let mut handle: *mut sys::QarOnboardingInvite = ptr::null_mut();
68        // SAFETY: wire_bytes is readable for its length; valid out-pointer.
69        let result = unsafe {
70            (api.qar_impl_onboarding_invite_deserialize)(
71                wire_bytes.as_ptr(),
72                wire_bytes.len(),
73                &raw mut handle,
74            )
75        };
76        check(api, "qar_onboarding_invite_deserialize", result)?;
77        Ok(Self {
78            handle: require_handle(handle, "qar_onboarding_invite_deserialize")?,
79            library: Rc::clone(library),
80        })
81    }
82
83    /// Wire bytes to hand to the sibling instance.
84    pub fn serialize(&self) -> Result<Vec<u8>> {
85        let api = &self.library.api;
86        let mut size = 0_usize;
87        // SAFETY: live invite; valid out-pointer.
88        let result = unsafe {
89            (api.qar_impl_onboarding_invite_serialized_size)(self.handle.as_ptr(), &raw mut size)
90        };
91        check(api, "qar_onboarding_invite_serialized_size", result)?;
92        let mut bytes = vec![0_u8; size];
93        let mut written = 0_usize;
94        // SAFETY: the buffer is writable for its length.
95        let result = unsafe {
96            (api.qar_impl_onboarding_invite_serialize)(
97                self.handle.as_ptr(),
98                bytes.as_mut_ptr(),
99                bytes.len(),
100                &raw mut written,
101            )
102        };
103        check(api, "qar_onboarding_invite_serialize", result)?;
104        if written > bytes.len() {
105            return Err(Error::Api {
106                function: "qar_onboarding_invite_serialize",
107                code: StatusCode::ARGUMENT_NOT_SUPPORTED,
108                message: format!("needs {written} bytes after reporting {size}"),
109                causes: Vec::new(),
110            });
111        }
112        bytes.truncate(written);
113        Ok(bytes)
114    }
115
116    /// The `QarOnboardingInvite` handle, for the functions only the raw table of
117    /// [`crate::Library::raw_api`] reaches, such as `qar_onboarding_invite_handle_is_valid`.
118    ///
119    /// Taking the pointer is safe; using it is `unsafe`, under the rules of
120    /// [`crate::Session::as_raw`] for an invite: valid while this `OnboardingInvite` lives, never
121    /// destroyed by the caller (it is destroyed on drop).
122    pub fn as_raw(&self) -> *mut sys::QarOnboardingInvite {
123        self.handle.as_ptr()
124    }
125
126    pub(crate) fn as_ptr(&self) -> *const sys::QarOnboardingInvite {
127        self.handle.as_ptr()
128    }
129}
130
131impl Drop for OnboardingInvite {
132    fn drop(&mut self) {
133        // SAFETY: owned handle, destroyed once; onboarding borrows the invite
134        // only for the duration of the call.
135        unsafe {
136            (self.library.api.qar_impl_onboarding_invite_handle_destroy)(self.handle.as_ptr());
137        };
138    }
139}