mirror of
https://git.alemi.dev/memo-cli.git
synced 2024-11-21 22:24:51 +01:00
use PathBuf instead of String
This commit is contained in:
parent
784ef88e81
commit
6023fcb296
2 changed files with 20 additions and 15 deletions
|
@ -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<JsonStorage, std::io::Error> {
|
||||
pub fn new(path: PathBuf) -> Result<JsonStorage, std::io::Error> {
|
||||
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<usize, MemoError> {
|
||||
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<u8>) -> Result<Self, MemoError> {
|
||||
return Ok(JsonStorage{
|
||||
path: "".to_string(),
|
||||
path: PathBuf::new(),
|
||||
data: serde_json::from_slice(data.as_slice()).unwrap(),
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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<NamedTempFile>,
|
||||
}
|
||||
|
||||
impl SQLiteStorage {
|
||||
pub fn new(path: &str, init: bool) -> Result<SQLiteStorage, MemoError> {
|
||||
let connection = Connection::open(path)?;
|
||||
pub fn new(path: PathBuf, init: bool) -> Result<SQLiteStorage, MemoError> {
|
||||
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<u8>) -> Result<Self, MemoError> {
|
||||
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),
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue