Skip to main content

syncweb_core/
init.rs

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