syncweb_core/folder/
share.rs1use iroh_docs::{DocTicket, NamespaceId, api::protocol::AddrInfoOptions};
9
10use crate::error::Result;
11
12use super::SyncwebFolder;
13
14#[derive(Clone, Copy, Debug)]
16#[non_exhaustive]
17pub struct ShareOptions {
18 pub writable: bool,
20 pub pin: bool,
22 pub persist: bool,
24 pub addr_info: AddrInfoOptions,
28}
29
30impl ShareOptions {
31 #[must_use]
32 pub const fn new(writable: bool) -> Self {
33 Self {
34 writable,
35 pin: false,
36 persist: false,
37 addr_info: AddrInfoOptions::RelayAndAddresses,
38 }
39 }
40 #[must_use]
41 pub const fn with_pin(mut self, pin: bool) -> Self {
42 self.pin = pin;
43 self
44 }
45
46 #[must_use]
47 pub const fn with_persist(mut self, persist: bool) -> Self {
48 self.persist = persist;
49 self
50 }
51
52 #[must_use]
53 pub const fn with_addr_info(mut self, addr_info: AddrInfoOptions) -> Self {
54 self.addr_info = addr_info;
55 self
56 }
57}
58
59#[derive(Clone, Debug)]
61#[non_exhaustive]
62pub struct ShareResult {
63 pub namespace: NamespaceId,
64 pub writable: bool,
65 pub ticket: DocTicket,
66 pub url: String,
67 pub pinned: usize,
68}
69
70impl ShareResult {
71 #[must_use]
72 pub const fn access(&self) -> &'static str {
73 if self.writable { "write" } else { "read" }
74 }
75}
76
77pub async fn share_folder<F>(folder: &SyncwebFolder, options: ShareOptions, persist: F) -> Result<ShareResult>
88where
89 F: FnOnce(NamespaceId, &str, &DocTicket) -> Result<()>,
90{
91 let pinned = if options.pin {
92 folder.pin_all_content().await?
93 } else {
94 0
95 };
96 let ticket = folder.ticket_with_options(options.writable, options.addr_info).await?;
97 let result = ShareResult {
98 namespace: folder.namespace_id(),
99 writable: options.writable,
100 ticket,
101 url: String::new(),
102 pinned,
103 };
104 if options.persist {
105 persist(result.namespace, result.access(), &result.ticket)?;
106 }
107 let url = crate::uri::folder_url(result.namespace, &result.ticket);
108 Ok(ShareResult { url, ..result })
109}