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 io;
23pub mod model;
24pub mod service;
25pub mod subscribers;
26
27#[derive(Clone)]
28pub struct Lb {
29    pub config: Config,
30    pub keychain: Keychain,
31    pub db: LbDb,
32    pub docs: AsyncDocs,
33    pub search: SearchIndex,
34    pub client: Network,
35    pub events: EventSubs,
36    pub syncing: Arc<AtomicBool>,
37    pub status: StatusUpdater,
38}
39
40impl Lb {
41    #[instrument(level = "info", skip_all, err(Debug))]
42    pub async fn init(config: Config) -> LbResult<Self> {
43        logging::init(&config)?;
44
45        let db = CoreDb::init(db_rs::Config::in_folder(&config.writeable_path))
46            .map_err(|err| LbErrKind::Unexpected(format!("{:#?}", err)))?;
47        let keychain = Keychain::from(db.account.get());
48        let db = Arc::new(RwLock::new(db));
49        let docs = AsyncDocs::from(&config);
50        let client = Network::default();
51        let search = SearchIndex::default();
52        let status = StatusUpdater::default();
53        let syncing = Arc::default();
54        let events = EventSubs::default();
55
56        let result = Self { config, keychain, db, docs, client, search, syncing, events, status };
57
58        result.setup_search();
59        result.setup_status().await?;
60
61        Ok(result)
62    }
63}
64
65pub fn get_code_version() -> &'static str {
66    env!("CARGO_PKG_VERSION")
67}
68
69pub static DEFAULT_API_LOCATION: &str = "https://api.prod.lockbook.net";
70pub static CORE_CODE_VERSION: &str = env!("CARGO_PKG_VERSION");
71
72use crate::io::CoreDb;
73use crate::service::logging;
74use db_rs::Db;
75use io::docs::AsyncDocs;
76use io::network::Network;
77use io::LbDb;
78use model::core_config::Config;
79pub use model::errors::{LbErrKind, LbResult};
80use service::events::EventSubs;
81use service::keychain::Keychain;
82use std::sync::atomic::AtomicBool;
83use std::sync::Arc;
84use subscribers::search::SearchIndex;
85use subscribers::status::StatusUpdater;
86use tokio::sync::RwLock;
87pub use uuid::Uuid;