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