lockbook_shared/
lib.rs

1pub mod access_info;
2pub mod account;
3pub mod api;
4pub mod clock;
5pub mod compression_service;
6pub mod core_config;
7pub mod core_ops;
8pub mod core_tree;
9pub mod crypto;
10pub mod document_repo;
11pub mod drawing;
12pub mod file;
13pub mod file_like;
14pub mod file_metadata;
15pub mod filename;
16pub mod lazy;
17pub mod path_ops;
18pub mod pubkey;
19pub mod secret_filename;
20pub mod server_file;
21pub mod server_ops;
22pub mod server_tree;
23pub mod signed_file;
24pub mod staged;
25pub mod symkey;
26pub mod tree_like;
27pub mod usage;
28pub mod validate;
29pub mod work_unit;
30
31pub use lazy::ValidationFailure;
32
33use std::backtrace::Backtrace;
34use std::io;
35
36use db_rs::DbError;
37use hmac::crypto_mac::{InvalidKeyLength, MacError};
38use uuid::Uuid;
39
40pub type SharedResult<T> = Result<T, SharedError>;
41
42#[derive(Debug)]
43pub struct SharedError {
44    pub kind: SharedErrorKind,
45    pub backtrace: Option<Backtrace>,
46}
47
48impl From<SharedErrorKind> for SharedError {
49    fn from(kind: SharedErrorKind) -> Self {
50        Self { kind, backtrace: Some(Backtrace::force_capture()) }
51    }
52}
53
54#[derive(Debug, PartialEq, Eq, Clone)]
55pub enum SharedErrorKind {
56    PathContainsEmptyFileName,
57    PathTaken,
58    RootNonexistent,
59    FileNonexistent,
60    FileNameContainsSlash,
61    RootModificationInvalid,
62    FileNameEmpty,
63    FileParentNonexistent,
64    FileNotFolder,
65    FileNotDocument,
66    SignatureInvalid,
67    WrongPublicKey,
68    SignatureInTheFuture(u64),
69    SignatureExpired(u64),
70    BincodeError(String),
71    Encryption(aead::Error),
72    HmacCreationError(InvalidKeyLength),
73    Decryption(aead::Error),
74    HmacValidationError(MacError),
75    ParseError(libsecp256k1::Error),
76    ShareNonexistent,
77    DuplicateShare,
78    SharedSecretUnexpectedSize,
79    SharedSecretError(libsecp256k1::Error),
80    ValidationFailure(ValidationFailure),
81
82    /// Arises during a call to upsert, when the caller does not have the correct old version of the
83    /// File they're trying to modify
84    OldVersionIncorrect,
85
86    /// Arises during a call to upsert, when the old file is not known to the server
87    OldFileNotFound,
88
89    /// Arises during a call to upsert, when the caller suggests that a file is new, but the id already
90    /// exists
91    OldVersionRequired,
92
93    /// Arises during a call to upsert, when the person making the request is not an owner of the file
94    /// or has not signed the update
95    InsufficientPermission,
96
97    /// Arises during a call to upsert, when a diff's new.id != old.id
98    DiffMalformed,
99
100    /// Metas in upsert cannot contain changes to digest
101    HmacModificationInvalid,
102
103    /// Found update to a deleted file
104    DeletedFileUpdated(Uuid),
105
106    Io(String),
107
108    Db(String),
109
110    Unexpected(&'static str),
111}
112
113impl From<DbError> for SharedError {
114    fn from(value: DbError) -> Self {
115        SharedErrorKind::Db(format!("db error: {:?}", value)).into()
116    }
117}
118
119impl From<bincode::Error> for SharedError {
120    fn from(err: bincode::Error) -> Self {
121        SharedErrorKind::BincodeError(err.to_string()).into()
122    }
123}
124
125impl From<io::Error> for SharedError {
126    fn from(err: io::Error) -> Self {
127        SharedErrorKind::Io(err.to_string()).into()
128    }
129}