Skip to main content

lb_rs/
lib.rs

1//! The library that underlies most things [lockbook](https://lockbook.net).
2//!
3//! All lockbook clients
4//! (iOS, linux, etc) rely on this library to perform cryptography, offline edits, and
5//! reconciliation of data between our server, other clients, and other devices.
6//!
7//! Our server relies on this library for checking signatures, and validating whether tree
8//! modifications are valid / authorized.
9//!
10//! - Most clients / integrators will be interested in the functions attached to the [Lb] struct.
11//!   See the [service] module for evolving this functionality.
12//! - The [model] module contains the specification of our data structures and contracts between
13//!   components.
14//! - The [blocking] module contains blocking variants of all [Lb] functions for consumers without
15//!   async runtimes.
16//! - The [io] module contains interactions with disk and network.
17
18#[macro_use]
19extern crate tracing;
20
21pub mod blocking;
22pub mod experiments;
23pub mod io;
24pub mod macros;
25pub mod model;
26pub mod search;
27pub mod service;
28pub mod subscribers;
29#[cfg(target_family = "wasm")]
30pub mod wasm;
31
32#[derive(Clone)]
33pub struct Lb {
34    pub config: Config,
35    pub user_last_seen: Arc<RwLock<Instant>>,
36    pub user_wake: Arc<Notify>,
37    pub keychain: Keychain,
38    pub db: LbDb,
39    pub docs: AsyncDocs,
40    pub client: Network,
41    pub events: EventSubs,
42    pub status: StatusUpdater,
43    pub syncer: Syncer,
44}
45
46impl Lb {
47    #[instrument(name = "Lb::init", level = "info", skip_all, err(Debug))]
48    pub async fn init(config: Config) -> LbResult<Self> {
49        logging::init(&config)?;
50
51        let docs = AsyncDocs::from(&config);
52        let db = io::init_with_migration(Path::new(&config.writeable_path))
53            .map_err(|err| LbErrKind::Unexpected(format!("{err:#?}")))?;
54        let keychain = Keychain::from(db.schema.account.as_ref());
55        let db = Arc::new(RwLock::new(db));
56        let client = Network { client_type: config.client_type, ..Network::default() };
57
58        let status = StatusUpdater::default();
59        let syncer = Default::default();
60        let events = EventSubs::default();
61        let user_last_seen = Arc::new(RwLock::new(Instant::now()));
62        let user_wake = Arc::new(Notify::new());
63
64        let result = Self {
65            config,
66            keychain,
67            db,
68            docs,
69            client,
70            syncer,
71            events,
72            status,
73            user_last_seen,
74            user_wake,
75        };
76
77        #[cfg(not(target_family = "wasm"))]
78        {
79            result.setup_syncer();
80            result.setup_status().await?;
81        }
82
83        Ok(result)
84    }
85}
86
87pub fn get_code_version() -> &'static str {
88    env!("CARGO_PKG_VERSION")
89}
90
91pub static DEFAULT_API_LOCATION: &str = "https://app.lockbook.net";
92pub static CORE_CODE_VERSION: &str = env!("CARGO_PKG_VERSION");
93
94use crate::subscribers::syncer::Syncer;
95
96use crate::service::logging;
97use io::LbDb;
98use io::docs::AsyncDocs;
99use io::network::Network;
100use model::core_config::Config;
101pub use model::errors::{LbErrKind, LbResult};
102use service::events::EventSubs;
103use service::keychain::Keychain;
104use std::path::Path;
105use std::sync::Arc;
106use subscribers::status::StatusUpdater;
107use tokio::sync::{Notify, RwLock};
108pub use uuid::Uuid;
109use web_time::Instant;