Skip to content

Commit

Permalink
Update dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Jan 9, 2025
1 parent 4820603 commit 9aa48db
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 51 deletions.
21 changes: 12 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ edition = "2021"
[dependencies]
fs2 = "0.4"
log = "0.4"
parking_lot = "0.12.1"
serde = "1"
serde_json = "1"
uuid = { version = "1", features = ["v4"] }
parking_lot = "0.12"
serde = "1.0"
serde_json = "1.0"
uuid = { version = "1.11", features = ["v4"] }

[dev-dependencies]
serde = { version = "1", features = ["derive"] }
serde_derive = { version = "1" }
tempfile = "3.4.0"
serde = { version = "1.0", features = ["derive"] }
serde_derive = { version = "1.0" }
tempfile = "3.15"

[badges]
maintenance = { status = "actively-developed" }
[lints.clippy]
pedantic = { level = "warn", priority = -1 }

# The error types returned should be self-explanatory.
missing_errors_doc = "allow"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ See [docs.rs/jfs](https://docs.rs/jfs/).

## License

Copyright (c) 2016 - 2023 Markus Kohlhase
Copyright (c) 2016 - 2025 Markus Kohlhase

This library is licensed under either of

Expand Down
62 changes: 32 additions & 30 deletions src/file_store.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use crate::json_store::JsonStore;
use fs2::FileExt;
use serde::{Deserialize, Serialize};
use serde_json::{
ser::{PrettyFormatter, Serializer},
value::Map,
Value,
};
use std::{
collections::BTreeMap,
fs::{create_dir_all, metadata, read_dir, remove_file, rename, OpenOptions},
io::{
prelude::*,
{Error, ErrorKind, Result},
Read, Write, {Error, ErrorKind, Result},
},
path::{Path, PathBuf},
};

use fs2::FileExt;
use serde::{Deserialize, Serialize};
use serde_json::{
ser::{PrettyFormatter, Serializer},
value::Map,
Value,
};
use uuid::Uuid;

use crate::json_store::JsonStore;

type Object = Map<String, Value>;

#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -47,7 +48,8 @@ impl JsonStore for FileStore {
where
for<'de> T: Serialize + Deserialize<'de>,
{
self.save_with_id(obj, &Uuid::new_v4().to_string())
let id = Uuid::new_v4().to_string();
self.save_with_id(obj, &id)
}

fn save_with_id<T>(&self, obj: &T, id: &str) -> Result<String>
Expand All @@ -56,10 +58,11 @@ impl JsonStore for FileStore {
{
if self.cfg.single {
let json = get_json_from_file(&self.path)?;
let o = get_object_from_json(&json)?;
let mut x = o.clone();
let j = serde_json::to_value(obj).map_err(|err| Error::new(ErrorKind::Other, err))?;
x.insert(id.to_owned(), j);
let object = get_object_from_json(&json)?;
let mut x = object.clone();
let json =
serde_json::to_value(obj).map_err(|err| Error::new(ErrorKind::Other, err))?;
x.insert(id.to_owned(), json);
self.save_object_to_file(&x, &self.path)?;
} else {
self.save_object_to_file(obj, &self.id_to_path(id))?;
Expand All @@ -72,15 +75,15 @@ impl JsonStore for FileStore {
for<'de> T: Deserialize<'de>,
{
let json = get_json_from_file(&self.id_to_path(id))?;
let o = if self.cfg.single {
let object = if self.cfg.single {
let x = json
.get(id)
.ok_or_else(|| Error::new(ErrorKind::NotFound, "no such object"))?;
x.clone()
} else {
json
};
decode(o)
decode(object)
}

fn all<T>(&self) -> Result<BTreeMap<String, T>>
Expand All @@ -89,12 +92,12 @@ impl JsonStore for FileStore {
{
if self.cfg.single {
let json = get_json_from_file(&self.id_to_path(""))?;
let o = get_object_from_json(&json)?;
let object = get_object_from_json(&json)?;
let mut result = BTreeMap::new();
for x in o.iter() {
let (k, v) = x;
if let Ok(r) = decode(v.clone()) {
result.insert(k.clone(), r);
for x in object {
let (key, value) = x;
if let Ok(r) = decode(value.clone()) {
result.insert(key.clone(), r);
}
}
return Ok(result);
Expand Down Expand Up @@ -129,8 +132,8 @@ impl JsonStore for FileStore {
fn delete(&self, id: &str) -> Result<()> {
if self.cfg.single {
let json = get_json_from_file(&self.path)?;
let o = get_object_from_json(&json)?;
let mut x = o.clone();
let object = get_object_from_json(&json)?;
let mut x = object.clone();
if x.contains_key(id) {
x.remove(id);
} else {
Expand Down Expand Up @@ -180,7 +183,7 @@ impl FileStore {
fn save_object_to_file<T: Serialize>(&self, obj: &T, file_name: &Path) -> Result<()> {
let json_string = self.object_to_string(obj)?;
let mut tmp_filename = file_name.to_path_buf();
tmp_filename.set_file_name(&Uuid::new_v4().to_string());
tmp_filename.set_file_name(Uuid::new_v4().to_string());
tmp_filename.set_extension("tmp");
let file = OpenOptions::new()
.write(true)
Expand All @@ -198,8 +201,8 @@ impl FileStore {
if let Err(err) = Write::write_all(&mut tmp_file, json_string.as_bytes()) {
Err(err)
} else {
tmp_file.unlock()?;
file.unlock()?;
FileExt::unlock(&tmp_file)?;
FileExt::unlock(&file)?;
drop(file);
drop(tmp_file);
rename(tmp_filename, file_name)
Expand Down Expand Up @@ -256,9 +259,9 @@ fn get_string_from_file(file_name: &Path) -> Result<String> {
.create(false)
.open(file_name)?;
let mut buffer = String::new();
f.lock_shared()?;
FileExt::lock_shared(&f)?;
f.read_to_string(&mut buffer)?;
f.unlock()?;
FileExt::unlock(&f)?;
Ok(buffer)
}

Expand Down Expand Up @@ -452,7 +455,6 @@ mod tests {
let dir = tempdir().unwrap().path().to_path_buf();
let db = FileStore::new(&dir).unwrap();

#[cfg(feature = "serde_json")]
#[derive(Deserialize, Serialize)]
struct X {
x: u32,
Expand Down
3 changes: 2 additions & 1 deletion src/json_store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, io::Result};

use serde::{Deserialize, Serialize};

pub trait JsonStore: Send + Sync {
fn save<T>(&self, obj: &T) -> Result<String>
where
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,24 +46,24 @@
//! ```rust,no_run
//! let db = jfs::Store::new(jfs::IN_MEMORY).unwrap();
//! ```
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::{
collections::BTreeMap,
io::Result,
path::{Path, PathBuf},
sync::Arc,
};

use parking_lot::RwLock;
use serde::{Deserialize, Serialize};

mod file_store;
mod json_store;
mod memory_store;

use file_store::FileStore;
use json_store::JsonStore;
use memory_store::MemoryStore;
use self::{file_store::FileStore, json_store::JsonStore, memory_store::MemoryStore};

pub use file_store::Config;
pub use self::file_store::Config;

#[derive(Debug, Clone)]
pub struct Store(StoreType);
Expand Down
9 changes: 5 additions & 4 deletions src/memory_store.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::json_store::JsonStore;
use parking_lot::{Mutex, RwLock};
use serde::{Deserialize, Serialize};
use std::{
collections::{BTreeMap, HashMap},
io::{Error, ErrorKind, Result},
sync::Arc,
};

use uuid::Uuid;
use parking_lot::{Mutex, RwLock};
use serde::{Deserialize, Serialize};

use crate::json_store::JsonStore;

#[derive(Debug, Clone, Default)]
pub struct MemoryStore {
Expand Down Expand Up @@ -187,7 +189,6 @@ mod tests {
fn all() {
let db = MemoryStore::default();

#[cfg(feature = "serde_json")]
#[derive(Deserialize, Serialize)]
struct X {
x: u32,
Expand Down

0 comments on commit 9aa48db

Please sign in to comment.