mirror of
https://github.com/hl-archive-node/nanoreth.git
synced 2025-12-06 10:59:55 +00:00
feat: add fs-util for reading, writing json files (#9418)
This commit is contained in:
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -7446,6 +7446,7 @@ dependencies = [
|
||||
name = "reth-fs-util"
|
||||
version = "1.0.1"
|
||||
dependencies = [
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
]
|
||||
|
||||
@ -15,4 +15,5 @@ workspace = true
|
||||
|
||||
# misc
|
||||
serde_json.workspace = true
|
||||
serde.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
@ -6,10 +6,10 @@
|
||||
issue_tracker_base_url = "https://github.com/paradigmxyz/reth/issues/"
|
||||
)]
|
||||
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
|
||||
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
use std::{
|
||||
fs::{self, ReadDir},
|
||||
io,
|
||||
fs::{self, File, ReadDir},
|
||||
io::{self, BufWriter, Write},
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
|
||||
@ -223,6 +223,12 @@ pub fn remove_dir_all(path: impl AsRef<Path>) -> Result<()> {
|
||||
fs::remove_dir_all(path).map_err(|err| FsPathError::remove_dir(err, path))
|
||||
}
|
||||
|
||||
/// Wrapper for `File::create`.
|
||||
pub fn create_file(path: impl AsRef<Path>) -> Result<fs::File> {
|
||||
let path = path.as_ref();
|
||||
File::create(path).map_err(|err| FsPathError::create_file(err, path))
|
||||
}
|
||||
|
||||
/// Wrapper for `std::fs::remove_file`
|
||||
pub fn remove_file(path: impl AsRef<Path>) -> Result<()> {
|
||||
let path = path.as_ref();
|
||||
@ -253,3 +259,21 @@ pub fn metadata(path: impl AsRef<Path>) -> Result<fs::Metadata> {
|
||||
let path = path.as_ref();
|
||||
fs::metadata(path).map_err(|err| FsPathError::metadata(err, path))
|
||||
}
|
||||
|
||||
/// Reads the JSON file and deserialize it into the provided type.
|
||||
pub fn read_json_file<T: DeserializeOwned>(path: &Path) -> Result<T> {
|
||||
// read the file into a byte array first
|
||||
// https://github.com/serde-rs/json/issues/160
|
||||
let bytes = read(path)?;
|
||||
serde_json::from_slice(&bytes)
|
||||
.map_err(|source| FsPathError::ReadJson { source, path: path.into() })
|
||||
}
|
||||
|
||||
/// Writes the object as a JSON object.
|
||||
pub fn write_json_file<T: Serialize>(path: &Path, obj: &T) -> Result<()> {
|
||||
let file = create_file(path)?;
|
||||
let mut writer = BufWriter::new(file);
|
||||
serde_json::to_writer_pretty(&mut writer, obj)
|
||||
.map_err(|source| FsPathError::WriteJson { source, path: path.into() })?;
|
||||
writer.flush().map_err(|e| FsPathError::write(e, path))
|
||||
}
|
||||
|
||||
@ -354,11 +354,8 @@ where
|
||||
/// `persistent_peers_file`.
|
||||
pub fn write_peers_to_file(&self, persistent_peers_file: &Path) -> Result<(), FsPathError> {
|
||||
let known_peers = self.all_peers().collect::<Vec<_>>();
|
||||
let known_peers = serde_json::to_string_pretty(&known_peers).map_err(|e| {
|
||||
FsPathError::WriteJson { source: e, path: persistent_peers_file.to_path_buf() }
|
||||
})?;
|
||||
persistent_peers_file.parent().map(fs::create_dir_all).transpose()?;
|
||||
fs::write(persistent_peers_file, known_peers)?;
|
||||
reth_fs_util::write_json_file(persistent_peers_file, &known_peers)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user