Quellcode durchsuchen

Simplify error type and create a database connection.

Mathias Gottschlag vor 5 Jahren
Ursprung
Commit
fc400bfc49
5 geänderte Dateien mit 121 neuen und 18 gelöschten Zeilen
  1. 97
    0
      Cargo.lock
  2. 2
    0
      Cargo.toml
  3. 1
    1
      src/client_side_sync.rs
  4. 11
    2
      src/database.rs
  5. 10
    15
      src/lib.rs

+ 97
- 0
Cargo.lock Datei anzeigen

@@ -314,6 +314,16 @@ name = "fake-simd"
314 314
 version = "0.1.2"
315 315
 source = "registry+https://github.com/rust-lang/crates.io-index"
316 316
 
317
+[[package]]
318
+name = "fallible-iterator"
319
+version = "0.2.0"
320
+source = "registry+https://github.com/rust-lang/crates.io-index"
321
+
322
+[[package]]
323
+name = "fallible-streaming-iterator"
324
+version = "0.1.9"
325
+source = "registry+https://github.com/rust-lang/crates.io-index"
326
+
317 327
 [[package]]
318 328
 name = "fnv"
319 329
 version = "1.0.7"
@@ -593,6 +603,20 @@ name = "libc"
593 603
 version = "0.2.71"
594 604
 source = "registry+https://github.com/rust-lang/crates.io-index"
595 605
 
606
+[[package]]
607
+name = "libsqlite3-sys"
608
+version = "0.18.0"
609
+source = "registry+https://github.com/rust-lang/crates.io-index"
610
+dependencies = [
611
+ "pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)",
612
+ "vcpkg 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
613
+]
614
+
615
+[[package]]
616
+name = "linked-hash-map"
617
+version = "0.5.3"
618
+source = "registry+https://github.com/rust-lang/crates.io-index"
619
+
596 620
 [[package]]
597 621
 name = "log"
598 622
 version = "0.4.8"
@@ -601,6 +625,14 @@ dependencies = [
601 625
  "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
602 626
 ]
603 627
 
628
+[[package]]
629
+name = "lru-cache"
630
+version = "0.1.2"
631
+source = "registry+https://github.com/rust-lang/crates.io-index"
632
+dependencies = [
633
+ "linked-hash-map 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)",
634
+]
635
+
604 636
 [[package]]
605 637
 name = "matches"
606 638
 version = "0.1.8"
@@ -738,6 +770,11 @@ name = "pin-utils"
738 770
 version = "0.1.0"
739 771
 source = "registry+https://github.com/rust-lang/crates.io-index"
740 772
 
773
+[[package]]
774
+name = "pkg-config"
775
+version = "0.3.17"
776
+source = "registry+https://github.com/rust-lang/crates.io-index"
777
+
741 778
 [[package]]
742 779
 name = "plotters"
743 780
 version = "0.2.15"
@@ -935,6 +972,21 @@ dependencies = [
935 972
  "serde 1.0.111 (registry+https://github.com/rust-lang/crates.io-index)",
936 973
 ]
937 974
 
975
+[[package]]
976
+name = "rusqlite"
977
+version = "0.23.1"
978
+source = "registry+https://github.com/rust-lang/crates.io-index"
979
+dependencies = [
980
+ "bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
981
+ "fallible-iterator 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
982
+ "fallible-streaming-iterator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)",
983
+ "libsqlite3-sys 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)",
984
+ "lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)",
985
+ "memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)",
986
+ "smallvec 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
987
+ "time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
988
+]
989
+
938 990
 [[package]]
939 991
 name = "rust-argon2"
940 992
 version = "0.7.0"
@@ -1114,6 +1166,24 @@ dependencies = [
1114 1166
  "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)",
1115 1167
 ]
1116 1168
 
1169
+[[package]]
1170
+name = "thiserror"
1171
+version = "1.0.19"
1172
+source = "registry+https://github.com/rust-lang/crates.io-index"
1173
+dependencies = [
1174
+ "thiserror-impl 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
1175
+]
1176
+
1177
+[[package]]
1178
+name = "thiserror-impl"
1179
+version = "1.0.19"
1180
+source = "registry+https://github.com/rust-lang/crates.io-index"
1181
+dependencies = [
1182
+ "proc-macro2 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
1183
+ "quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
1184
+ "syn 1.0.30 (registry+https://github.com/rust-lang/crates.io-index)",
1185
+]
1186
+
1117 1187
 [[package]]
1118 1188
 name = "thread_local"
1119 1189
 version = "1.0.1"
@@ -1122,6 +1192,15 @@ dependencies = [
1122 1192
  "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
1123 1193
 ]
1124 1194
 
1195
+[[package]]
1196
+name = "time"
1197
+version = "0.1.43"
1198
+source = "registry+https://github.com/rust-lang/crates.io-index"
1199
+dependencies = [
1200
+ "libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)",
1201
+ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
1202
+]
1203
+
1125 1204
 [[package]]
1126 1205
 name = "tinytemplate"
1127 1206
 version = "1.1.0"
@@ -1196,9 +1275,11 @@ dependencies = [
1196 1275
  "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
1197 1276
  "range-set 0.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
1198 1277
  "rmp-serde 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)",
1278
+ "rusqlite 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)",
1199 1279
  "serde 1.0.111 (registry+https://github.com/rust-lang/crates.io-index)",
1200 1280
  "serde_json 1.0.53 (registry+https://github.com/rust-lang/crates.io-index)",
1201 1281
  "structopt 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)",
1282
+ "thiserror 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)",
1202 1283
  "tokio 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)",
1203 1284
  "url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
1204 1285
 ]
@@ -1254,6 +1335,11 @@ name = "utf-8"
1254 1335
 version = "0.7.5"
1255 1336
 source = "registry+https://github.com/rust-lang/crates.io-index"
1256 1337
 
1338
+[[package]]
1339
+name = "vcpkg"
1340
+version = "0.2.9"
1341
+source = "registry+https://github.com/rust-lang/crates.io-index"
1342
+
1257 1343
 [[package]]
1258 1344
 name = "vec_map"
1259 1345
 version = "0.8.2"
@@ -1421,6 +1507,8 @@ dependencies = [
1421 1507
 "checksum either 1.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "bb1f6b1ce1c140482ea30ddd3335fc0024ac7ee112895426e0a629a6c20adfe3"
1422 1508
 "checksum env_logger 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
1423 1509
 "checksum fake-simd 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e88a8acf291dafb59c2d96e8f59828f3838bb1a70398823ade51a84de6a6deed"
1510
+"checksum fallible-iterator 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "4443176a9f2c162692bd3d352d745ef9413eec5782a80d8fd6f8a1ac692a07f7"
1511
+"checksum fallible-streaming-iterator 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "7360491ce676a36bf9bb3c56c1aa791658183a54d2744120f27285738d90465a"
1424 1512
 "checksum fnv 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
1425 1513
 "checksum fslock 0.1.6 (registry+https://github.com/rust-lang/crates.io-index)" = "b14c83e47c73f7d62d907ae24a1a98e9132df3c33eb6c54fcf4bce0dbc41d5af"
1426 1514
 "checksum fswatcher 0.1.0 (git+https://github.com/mgottschlag/fswatcher-rs.git)" = "<none>"
@@ -1454,7 +1542,10 @@ dependencies = [
1454 1542
 "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d"
1455 1543
 "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
1456 1544
 "checksum libc 0.2.71 (registry+https://github.com/rust-lang/crates.io-index)" = "9457b06509d27052635f90d6466700c65095fdf75409b3fbdd903e988b886f49"
1545
+"checksum libsqlite3-sys 0.18.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1e704a02bcaecd4a08b93a23f6be59d0bd79cd161e0963e9499165a0a35df7bd"
1546
+"checksum linked-hash-map 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8dd5a6d5999d9907cda8ed67bbd137d3af8085216c2ac62de5be860bd41f304a"
1457 1547
 "checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
1548
+"checksum lru-cache 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "31e24f1ad8321ca0e8a1e0ac13f23cb668e6f5466c2c57319f6a5cf1cc8e3b1c"
1458 1549
 "checksum matches 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)" = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
1459 1550
 "checksum maybe-uninit 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
1460 1551
 "checksum memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
@@ -1473,6 +1564,7 @@ dependencies = [
1473 1564
 "checksum pin-project-internal 0.4.20 (registry+https://github.com/rust-lang/crates.io-index)" = "10b4b44893d3c370407a1d6a5cfde7c41ae0478e31c516c85f67eb3adc51be6d"
1474 1565
 "checksum pin-project-lite 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "282adbf10f2698a7a77f8e983a74b2d18176c19a7fd32a45446139ae7b02b715"
1475 1566
 "checksum pin-utils 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
1567
+"checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
1476 1568
 "checksum plotters 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)" = "0d1685fbe7beba33de0330629da9d955ac75bd54f33d7b79f9a895590124f6bb"
1477 1569
 "checksum ppv-lite86 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)" = "237a5ed80e274dbc66f86bd59c1e25edc039660be53194b5fe0a482e0f2612ea"
1478 1570
 "checksum proc-macro-error 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678"
@@ -1496,6 +1588,7 @@ dependencies = [
1496 1588
 "checksum regex-syntax 0.6.18 (registry+https://github.com/rust-lang/crates.io-index)" = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
1497 1589
 "checksum rmp 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)" = "0f10b46df14cf1ee1ac7baa4d2fbc2c52c0622a4b82fa8740e37bc452ac0184f"
1498 1590
 "checksum rmp-serde 0.14.3 (registry+https://github.com/rust-lang/crates.io-index)" = "4c1ee98f14fe8b8e9c5ea13d25da7b2a1796169202c57a09d7288de90d56222b"
1591
+"checksum rusqlite 0.23.1 (registry+https://github.com/rust-lang/crates.io-index)" = "45d0fd62e1df63d254714e6cb40d0a0e82e7a1623e7a27f679d851af092ae58b"
1499 1592
 "checksum rust-argon2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017"
1500 1593
 "checksum rustc_version 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)" = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
1501 1594
 "checksum ryu 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
@@ -1518,7 +1611,10 @@ dependencies = [
1518 1611
 "checksum syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a"
1519 1612
 "checksum termcolor 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
1520 1613
 "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
1614
+"checksum thiserror 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "b13f926965ad00595dd129fa12823b04bbf866e9085ab0a5f2b05b850fbfc344"
1615
+"checksum thiserror-impl 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "893582086c2f98cde18f906265a65b5030a074b1046c674ae898be6519a7f479"
1521 1616
 "checksum thread_local 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
1617
+"checksum time 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "ca8a50ef2360fbd1eeb0ecd46795a87a19024eb4b53c5dc916ca1fd95fe62438"
1522 1618
 "checksum tinytemplate 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6d3dc76004a03cec1c5932bca4cdc2e39aaa798e3f82363dd94f9adf6098c12f"
1523 1619
 "checksum tokio 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)" = "d099fa27b9702bed751524694adbe393e18b36b204da91eb1cbbbbb4a5ee2d58"
1524 1620
 "checksum tokio-macros 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "f0c3acc6aa564495a0f2e1d59fab677cd7f81a19994cfc7f3ad0e64301560389"
@@ -1531,6 +1627,7 @@ dependencies = [
1531 1627
 "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
1532 1628
 "checksum url 2.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "829d4a8476c35c9bf0bbce5a3b23f4106f79728039b726d292bb93bc106787cb"
1533 1629
 "checksum utf-8 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)" = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7"
1630
+"checksum vcpkg 0.2.9 (registry+https://github.com/rust-lang/crates.io-index)" = "55d1e41d56121e07f1e223db0a4def204e45c85425f6a16d462fd07c8d10d74c"
1534 1631
 "checksum vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
1535 1632
 "checksum version_check 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
1536 1633
 "checksum walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "777182bc735b6424e1a57516d35ed72cb8019d85c8c9bf536dccb3445c1a2f7d"

+ 2
- 0
Cargo.toml Datei anzeigen

@@ -20,6 +20,8 @@ fswatcher = { git = "https://github.com/mgottschlag/fswatcher-rs.git" }
20 20
 url = "*"
21 21
 range-set = "*"
22 22
 fslock = "0.1"
23
+rusqlite = "0.23"
24
+thiserror = "1.0"
23 25
 
24 26
 [dev-dependencies]
25 27
 criterion = "0.3"

+ 1
- 1
src/client_side_sync.rs Datei anzeigen

@@ -3,7 +3,7 @@ use std::sync::Arc;
3 3
 use tokio::sync::{mpsc, oneshot, Mutex};
4 4
 
5 5
 use super::file_tree::{FileTree, NewVersionSource};
6
-use super::{Error, SynchronizationError};
6
+use super::SynchronizationError;
7 7
 
8 8
 pub struct ClientSideSync {
9 9
     state_send: mpsc::Sender<(StateChange, oneshot::Sender<()>)>,

+ 11
- 2
src/database.rs Datei anzeigen

@@ -1,13 +1,22 @@
1 1
 use std::ffi::OsString;
2 2
 
3
+use rusqlite::Connection;
4
+
3 5
 pub struct Database {
6
+    db: Connection,
4 7
     // TODO
5 8
 }
6 9
 
7 10
 impl Database {
8
-    pub fn create_or_open(_path: &str) -> Result<Database, crate::Error> {
11
+    pub fn create_or_open(path: &str) -> Result<Database, crate::Error> {
12
+        let db = Connection::open(&path)?;
13
+
14
+        // We need to create the tables if they do not exist (likely, we just created the
15
+        // database).
16
+        //db.execute("CREATE TABLE IF NOT EXISTS synchronized_directories ( );", &[]);
9 17
         // TODO
10
-        Ok(Database {})
18
+
19
+        Ok(Database { db })
11 20
     }
12 21
 
13 22
     pub fn synchronized_directories(&self) -> Vec<OsString> {

+ 10
- 15
src/lib.rs Datei anzeigen

@@ -4,6 +4,8 @@ extern crate rmp_serde as rmps;
4 4
 use std::ffi::OsString;
5 5
 use std::io;
6 6
 
7
+use thiserror::Error;
8
+
7 9
 mod client_side_sync;
8 10
 mod database;
9 11
 mod file_system_watcher;
@@ -95,25 +97,18 @@ pub struct SynchronizationError {
95 97
     pub error: Error,
96 98
 }
97 99
 
98
-#[derive(Debug)]
100
+#[derive(Error, Debug)]
99 101
 pub enum Error {
100
-    Io(std::io::Error),
101
-    Json(serde_json::error::Error),
102
+    #[error("input/output error")]
103
+    Io(#[from] std::io::Error),
104
+    #[error("JSON serialization error")]
105
+    Json(#[from] serde_json::error::Error),
106
+    #[error("database error")]
107
+    Sqlite(#[from] rusqlite::Error),
108
+    #[error("directory removed")]
102 109
     DirectoryRemoved,
103 110
 }
104 111
 
105
-impl From<serde_json::error::Error> for Error {
106
-    fn from(e: serde_json::error::Error) -> Error {
107
-        Error::Json(e)
108
-    }
109
-}
110
-
111
-impl From<io::Error> for Error {
112
-    fn from(e: io::Error) -> Error {
113
-        Error::Io(e)
114
-    }
115
-}
116
-
117 112
 impl From<fswatcher::Error> for Error {
118 113
     fn from(e: fswatcher::Error) -> Error {
119 114
         match e {

Laden…
Abbrechen
Speichern