From 6023fcb296241fe9f99a026379a948a49b1c3eac Mon Sep 17 00:00:00 2001 From: alemidev Date: Mon, 28 Mar 2022 01:51:19 +0200 Subject: [PATCH] use PathBuf instead of String --- src/storage/json.rs | 17 ++++++++++------- src/storage/sqlite.rs | 18 ++++++++++-------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/storage/json.rs b/src/storage/json.rs index 01fd15f..1aefedc 100644 --- a/src/storage/json.rs +++ b/src/storage/json.rs @@ -1,5 +1,6 @@ use std::fs::File; use std::io::BufReader; +use std::path::PathBuf; use chrono::{DateTime, Utc}; use serde::{Serialize, Deserialize}; use std::io::Write; @@ -10,7 +11,7 @@ use crate::storage::{Memo, State, MemoError, AuthStorage, MemoStorage, StateStor use crate::remote::RemoteSync; pub struct JsonStorage { - path: String, + path: PathBuf, data: Store, } @@ -38,17 +39,19 @@ impl Default for Store { } impl JsonStorage { - pub fn new(path: &str) -> Result { + pub fn new(path: PathBuf) -> Result { let mut data = Store::default(); - let file = File::open(path); + let file = File::open(&path); if let Ok(f) = file { let reader = BufReader::new(f); - data = serde_json::from_reader(reader).unwrap(); + if let Ok(d) = serde_json::from_reader(reader) { + data = d; + } } // TODO check if table exist and is valid - return Ok(JsonStorage{ path: path.to_string(), data: data }); + return Ok(JsonStorage{ path, data }); } pub fn save(&self) -> Result<(), std::io::Error> { @@ -125,7 +128,7 @@ impl MemoStorage for JsonStorage { fn set(&mut self, m: &Memo) -> Result { let mut count = 0; - for (i, memo) in self.data.memo.iter().enumerate() { + for (_i, memo) in self.data.memo.iter().enumerate() { if memo.id == m.id { // TODO improve // self.data.memo[i].body = m.body; @@ -173,7 +176,7 @@ impl RemoteSync for JsonStorage { fn deserialize(data: Vec) -> Result { return Ok(JsonStorage{ - path: "".to_string(), + path: PathBuf::new(), data: serde_json::from_slice(data.as_slice()).unwrap(), }); } diff --git a/src/storage/sqlite.rs b/src/storage/sqlite.rs index 9c770eb..6d0365f 100644 --- a/src/storage/sqlite.rs +++ b/src/storage/sqlite.rs @@ -1,10 +1,10 @@ use crate::remote::RemoteSync; use crate::storage::{AuthStorage, Memo, MemoError, MemoStorage, State, StateStorage}; use chrono::{DateTime, Utc}; -use rusqlite::DatabaseName::Main; +// use rusqlite::DatabaseName::Main; use rusqlite::{params, Connection}; -use std::io::Error; use sha2::{Digest, Sha512}; +use std::path::PathBuf; use std::collections::LinkedList; use std::fs; use std::io::Write; @@ -13,13 +13,15 @@ use tempfile::NamedTempFile; pub struct SQLiteStorage { conn: Connection, - path: String, + path: PathBuf, + + #[allow(dead_code)] // I just use this to bind the tempfile life with the SQLiteStorage obj tmpfile: Option, } impl SQLiteStorage { - pub fn new(path: &str, init: bool) -> Result { - let connection = Connection::open(path)?; + pub fn new(path: PathBuf, init: bool) -> Result { + let connection = Connection::open(&path)?; // TODO check if table exist and is valid if init { @@ -48,7 +50,7 @@ impl SQLiteStorage { )?; } - return Ok(SQLiteStorage { conn: connection , path: path.to_string(), tmpfile: None }); + return Ok(SQLiteStorage { conn: connection , path, tmpfile: None }); } } @@ -181,10 +183,10 @@ impl RemoteSync for SQLiteStorage { fn deserialize(data: Vec) -> Result { let mut tmpfile = NamedTempFile::new()?; tmpfile.write(data.as_slice())?; - let path = tmpfile.path().to_str().unwrap(); + let path = tmpfile.path(); return Ok(SQLiteStorage{ conn: Connection::open(path).unwrap(), - path: path.to_string(), + path: path.to_path_buf(), tmpfile: Some(tmpfile), }); }