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 docs = AsyncDocs::from(&config);
46        let db = migrate_and_init(&config, &docs).await?;
47        let keychain = Keychain::from(db.account.get());
48        let db = Arc::new(RwLock::new(db));
49        let client = Network::default();
50        let search = SearchIndex::default();
51        let status = StatusUpdater::default();
52        let syncing = Arc::default();
53        let events = EventSubs::default();
54
55        let result = Self { config, keychain, db, docs, client, search, syncing, events, status };
56
57        result.setup_search();
58        result.setup_status().await?;
59
60        Ok(result)
61    }
62}
63
64pub fn get_code_version() -> &'static str {
65    env!("CARGO_PKG_VERSION")
66}
67
68pub static DEFAULT_API_LOCATION: &str = "https://api.prod.lockbook.net";
69pub static CORE_CODE_VERSION: &str = env!("CARGO_PKG_VERSION");
70
71use crate::service::logging;
72use io::docs::AsyncDocs;
73use io::network::Network;
74use io::{LbDb, migrate_and_init};
75use model::core_config::Config;
76pub use model::errors::{LbErrKind, LbResult};
77use service::events::EventSubs;
78use service::keychain::Keychain;
79use std::sync::Arc;
80use std::sync::atomic::AtomicBool;
81use subscribers::search::SearchIndex;
82use subscribers::status::StatusUpdater;
83use tokio::sync::RwLock;
84pub use uuid::Uuid;