syncweb_core/folder/
share.rs1use iroh_docs::{DocTicket, NamespaceId};
9
10use crate::error::Result;
11
12use super::SyncwebFolder;
13
14#[derive(Clone, Copy, Debug, Default)]
16#[non_exhaustive]
17pub struct ShareOptions {
18 pub writable: bool,
20 pub pin: bool,
22 pub persist: bool,
24}
25
26impl ShareOptions {
27 #[must_use]
28 pub const fn new(writable: bool) -> Self {
29 Self {
30 writable,
31 pin: false,
32 persist: false,
33 }
34 }
35
36 #[must_use]
37 pub const fn with_pin(mut self, pin: bool) -> Self {
38 self.pin = pin;
39 self
40 }
41
42 #[must_use]
43 pub const fn with_persist(mut self, persist: bool) -> Self {
44 self.persist = persist;
45 self
46 }
47}
48
49#[derive(Clone, Debug)]
51#[non_exhaustive]
52pub struct ShareResult {
53 pub namespace: NamespaceId,
54 pub writable: bool,
55 pub ticket: DocTicket,
56 pub url: String,
57 pub pinned: usize,
58}
59
60impl ShareResult {
61 #[must_use]
62 pub const fn access(&self) -> &'static str {
63 if self.writable { "write" } else { "read" }
64 }
65}
66
67pub async fn share_folder<F>(
78 folder: &SyncwebFolder,
79 options: ShareOptions,
80 persist: F,
81) -> Result<ShareResult>
82where
83 F: FnOnce(NamespaceId, &str, &DocTicket) -> Result<()>,
84{
85 let pinned = if options.pin {
86 folder.pin_all_content().await?
87 } else {
88 0
89 };
90 let ticket = folder.ticket(options.writable).await?;
91 let result = ShareResult {
92 namespace: folder.namespace_id(),
93 writable: options.writable,
94 ticket,
95 url: String::new(),
96 pinned,
97 };
98 if options.persist {
99 persist(result.namespace, result.access(), &result.ticket)?;
100 }
101 let url = crate::uri::folder_url(result.namespace, &result.ticket);
102 Ok(ShareResult { url, ..result })
103}