Skip to main content

syncweb_core/
init.rs

1use std::collections::HashSet;
2use std::path::PathBuf;
3use std::sync::{Arc, RwLock};
4
5use iroh_docs::{DocTicket, NamespaceId};
6
7use crate::node::identity::IdentityManager;
8use crate::node::iroh_node::{IrohNode, RelayMode};
9
10/// Result of initializing a local synchronized folder.
11#[derive(Clone, Debug)]
12#[non_exhaustive]
13pub struct InitResult {
14    pub path: PathBuf,
15    pub namespace: NamespaceId,
16    pub ticket: DocTicket,
17    pub share_url: String,
18}
19
20impl InitResult {
21    #[must_use]
22    pub fn new(path: impl Into<PathBuf>, namespace: NamespaceId, ticket: DocTicket) -> Self {
23        let share_url = crate::uri::folder_url(namespace, &ticket);
24        Self {
25            path: path.into(),
26            namespace,
27            ticket,
28            share_url,
29        }
30    }
31}
32
33/// Open a node with default relay mode.
34///
35/// # Errors
36///
37/// Returns an error if the identity cannot be loaded or the node cannot be created.
38pub async fn open_node(data_dir: &std::path::Path) -> crate::error::Result<IrohNode> {
39    let identity = IdentityManager::new(data_dir.join("identity.key"))?;
40    let empty_keys = Arc::new(RwLock::new(HashSet::new()));
41    IrohNode::new(
42        identity,
43        data_dir.join("data"),
44        RelayMode::Default,
45        empty_keys,
46        crate::node::iroh_node::DiscoveryConfig::default(),
47    )
48    .await
49}