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 io;
23pub mod macros;
24pub mod model;
25pub mod service;
26pub mod subscribers;
27
28#[derive(Clone)]
29pub struct Lb {
30    pub config: Config,
31    pub keychain: Keychain,
32    pub db: LbDb,
33    pub docs: AsyncDocs,
34    #[cfg(not(target_family = "wasm"))]
35    pub search: SearchIndex,
36    pub client: Network,
37    pub events: EventSubs,
38    pub syncing: Arc<AtomicBool>,
39    pub status: StatusUpdater,
40}
41
42impl Lb {
43    /// this is dumb lb that will make the library compile for wasm but doesn't include
44    /// any of the expected functionality. your files wouldn't be saved, sync wouldn't
45    /// work, etc. for now this is useful for unblocking workspace on wasm
46    #[cfg(target_family = "wasm")]
47    pub fn init_dummy(config: Config) -> LbResult<Self> {
48        let db = CoreDb::init(db_rs::Config {
49            path: Default::default(),
50            create_path: false,
51            create_db: false,
52            read_only: false,
53            no_io: true,
54            fs_locks: false,
55            fs_locks_block: false,
56            schema_name: Default::default(),
57        })
58        .map_err(|err| LbErrKind::Unexpected(format!("db rs creation failed: {:#?}", err)))?;
59
60        Ok(Self {
61            config: config.clone(),
62            keychain: Default::default(),
63            db: Arc::new(RwLock::new(db)),
64            docs: AsyncDocs::from(&config),
65            client: Default::default(),
66            syncing: Default::default(),
67            events: Default::default(),
68            status: Default::default(),
69        })
70    }
71}
72
73impl Lb {
74    #[instrument(level = "info", skip_all, err(Debug))]
75    pub async fn init(config: Config) -> LbResult<Self> {
76        logging::init(&config)?;
77
78        let docs = AsyncDocs::from(&config);
79        let db = migrate_and_init(&config, &docs).await?;
80        let keychain = Keychain::from(db.account.get());
81        let db = Arc::new(RwLock::new(db));
82        let client = Network::default();
83        #[cfg(not(target_family = "wasm"))]
84        let search = SearchIndex::default();
85
86        let status = StatusUpdater::default();
87        let syncing = Arc::default();
88        let events = EventSubs::default();
89
90        let result = Self {
91            config,
92            keychain,
93            db,
94            docs,
95            client,
96            #[cfg(not(target_family = "wasm"))]
97            search,
98            syncing,
99            events,
100            status,
101        };
102
103        #[cfg(not(target_family = "wasm"))]
104        {
105            result.setup_search();
106            result.setup_status().await?;
107        }
108
109        Ok(result)
110    }
111}
112
113pub fn get_code_version() -> &'static str {
114    env!("CARGO_PKG_VERSION")
115}
116
117pub static DEFAULT_API_LOCATION: &str = "https://api.prod.lockbook.net";
118pub static CORE_CODE_VERSION: &str = env!("CARGO_PKG_VERSION");
119
120#[cfg(target_family = "wasm")]
121use crate::io::CoreDb;
122#[cfg(target_family = "wasm")]
123use db_rs::Db;
124#[cfg(not(target_family = "wasm"))]
125use subscribers::search::SearchIndex;
126
127use crate::service::logging;
128use io::docs::AsyncDocs;
129use io::network::Network;
130use io::{LbDb, migrate_and_init};
131use model::core_config::Config;
132pub use model::errors::{LbErrKind, LbResult};
133use service::events::EventSubs;
134use service::keychain::Keychain;
135use std::sync::Arc;
136use std::sync::atomic::AtomicBool;
137use subscribers::status::StatusUpdater;
138use tokio::sync::RwLock;
139pub use uuid::Uuid;