summaryrefslogtreecommitdiff
path: root/crates/utils/cfg_file
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2025-09-14 13:12:27 +0800
committer魏曹先生 <1992414357@qq.com>2025-09-14 13:12:27 +0800
commit0ad594277e61e9fb41b2e470c34cff7534d6c780 (patch)
tree3d14c3ad518c273105c89e5c3f39e38f4cfa45c2 /crates/utils/cfg_file
parentb8844ab55a6d622370be8ce41387ff9d9897302e (diff)
Fixed codes by Zed
Diffstat (limited to 'crates/utils/cfg_file')
-rw-r--r--crates/utils/cfg_file/cfg_file_derive/src/lib.rs18
-rw-r--r--crates/utils/cfg_file/cfg_file_test/Cargo.toml2
-rw-r--r--crates/utils/cfg_file/cfg_file_test/src/lib.rs23
-rw-r--r--crates/utils/cfg_file/src/config.rs31
-rw-r--r--crates/utils/cfg_file/src/lib.rs2
5 files changed, 41 insertions, 35 deletions
diff --git a/crates/utils/cfg_file/cfg_file_derive/src/lib.rs b/crates/utils/cfg_file/cfg_file_derive/src/lib.rs
index c43fd59..66a6d6f 100644
--- a/crates/utils/cfg_file/cfg_file_derive/src/lib.rs
+++ b/crates/utils/cfg_file/cfg_file_derive/src/lib.rs
@@ -3,11 +3,7 @@ extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::parse::ParseStream;
-use syn::{
- parse_macro_input,
- DeriveInput,
- Attribute
-};
+use syn::{Attribute, DeriveInput, parse_macro_input};
#[proc_macro_derive(ConfigFile, attributes(cfg_file))]
pub fn derive_config_file(input: TokenStream) -> TokenStream {
@@ -55,9 +51,13 @@ fn find_cfg_file_path(attrs: &[Attribute]) -> Option<String> {
let parser = |meta: ParseStream| {
let path_meta: syn::MetaNameValue = meta.parse()?;
if path_meta.path.is_ident("path")
- && let syn::Expr::Lit(syn::ExprLit { lit: syn::Lit::Str(lit), .. }) = path_meta.value {
- return Ok(lit.value());
- }
+ && let syn::Expr::Lit(syn::ExprLit {
+ lit: syn::Lit::Str(lit),
+ ..
+ }) = path_meta.value
+ {
+ return Ok(lit.value());
+ }
Err(meta.error("expected `path = \"...\"`"))
};
@@ -82,4 +82,4 @@ fn to_snake_case(s: &str) -> String {
}
}
snake
-} \ No newline at end of file
+}
diff --git a/crates/utils/cfg_file/cfg_file_test/Cargo.toml b/crates/utils/cfg_file/cfg_file_test/Cargo.toml
index 73e580b..d1b527a 100644
--- a/crates/utils/cfg_file/cfg_file_test/Cargo.toml
+++ b/crates/utils/cfg_file/cfg_file_test/Cargo.toml
@@ -6,4 +6,4 @@ edition = "2024"
[dependencies]
cfg_file = { path = "../../cfg_file", features = ["default"] }
tokio = { version = "1.46.1", features = ["full"] }
-serde = { version = "1.0.219", features = ["derive"] } \ No newline at end of file
+serde = { version = "1.0.219", features = ["derive"] }
diff --git a/crates/utils/cfg_file/cfg_file_test/src/lib.rs b/crates/utils/cfg_file/cfg_file_test/src/lib.rs
index ea490c9..fd805f1 100644
--- a/crates/utils/cfg_file/cfg_file_test/src/lib.rs
+++ b/crates/utils/cfg_file/cfg_file_test/src/lib.rs
@@ -4,10 +4,10 @@ pub fn add(left: u64, right: u64) -> u64 {
#[cfg(test)]
mod test_cfg_file {
- use std::collections::HashMap;
- use serde::{Deserialize, Serialize};
- use cfg_file::config::ConfigFile;
use cfg_file::ConfigFile;
+ use cfg_file::config::ConfigFile;
+ use serde::{Deserialize, Serialize};
+ use std::collections::HashMap;
#[derive(ConfigFile, Deserialize, Serialize, Default)]
#[cfg_file(path = "./.temp/example_cfg.toml")]
@@ -23,19 +23,22 @@ mod test_cfg_file {
let mut example = ExampleConfig {
name: "Weicao".to_string(),
age: 22,
- hobby: vec![ "Programming", "Painting" ]
+ hobby: vec!["Programming", "Painting"]
.iter()
.map(|m| m.to_string())
.collect(),
- secret: HashMap::new()
+ secret: HashMap::new(),
};
- let secret_no_comments = "Actually, I'm really too lazy to write comments, documentation, and unit tests.";
- example.secret
+ let secret_no_comments =
+ "Actually, I'm really too lazy to write comments, documentation, and unit tests.";
+ example
+ .secret
.entry("No comments".to_string())
.insert_entry(secret_no_comments.to_string());
let secret_peek = "Of course, it's peeking at you who's reading the source code.";
- example.secret
+ example
+ .secret
.entry("Peek".to_string())
.insert_entry(secret_peek.to_string());
@@ -45,8 +48,8 @@ mod test_cfg_file {
let read_cfg = ExampleConfig::read().await;
assert_eq!(read_cfg.name, "Weicao");
assert_eq!(read_cfg.age, 22);
- assert_eq!(read_cfg.hobby, vec![ "Programming", "Painting" ]);
+ assert_eq!(read_cfg.hobby, vec!["Programming", "Painting"]);
assert_eq!(read_cfg.secret["No comments"], secret_no_comments);
assert_eq!(read_cfg.secret["Peek"], secret_peek);
}
-} \ No newline at end of file
+}
diff --git a/crates/utils/cfg_file/src/config.rs b/crates/utils/cfg_file/src/config.rs
index 2bc104d..8d97bf0 100644
--- a/crates/utils/cfg_file/src/config.rs
+++ b/crates/utils/cfg_file/src/config.rs
@@ -1,15 +1,12 @@
use async_trait::async_trait;
-use serde::{ Deserialize, Serialize };
+use serde::{Deserialize, Serialize};
use std::{
borrow::Cow,
env::current_dir,
io::Error,
- path:: { PathBuf, Path },
-};
-use tokio::{
- fs,
- io::AsyncReadExt
+ path::{Path, PathBuf},
};
+use tokio::{fs, io::AsyncReadExt};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ConfigFormat {
@@ -46,7 +43,7 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default {
Self: Sized + Send + Sync,
{
let Ok(path) = Self::default_path() else {
- return Self::DataType::default()
+ return Self::DataType::default();
};
Self::read_from(path).await
@@ -86,7 +83,8 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default {
}
// Determine file format
- let format = file_path.file_name()
+ let format = file_path
+ .file_name()
.and_then(|name| name.to_str())
.and_then(ConfigFormat::from_filename)
.unwrap_or(ConfigFormat::Json); // Default to JSON
@@ -136,9 +134,10 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default {
let path = path.as_ref();
if let Some(parent) = path.parent()
- && ! parent.exists() {
- let _ = tokio::fs::create_dir_all(parent).await;
- }
+ && !parent.exists()
+ {
+ let _ = tokio::fs::create_dir_all(parent).await;
+ }
let file_path = match current_dir() {
Ok(cwd) => cwd.join(path),
@@ -149,7 +148,8 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default {
};
// Determine file format
- let format = file_path.file_name()
+ let format = file_path
+ .file_name()
.and_then(|name| name.to_str())
.and_then(ConfigFormat::from_filename)
.unwrap_or(ConfigFormat::Json); // Default to JSON
@@ -181,7 +181,10 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default {
// Don't write if serialization failed
if contents.is_empty() {
- eprintln!("Serialization failed for file {}, not writing", path.display());
+ eprintln!(
+ "Serialization failed for file {}, not writing",
+ path.display()
+ );
return;
}
@@ -190,4 +193,4 @@ pub trait ConfigFile: Serialize + for<'a> Deserialize<'a> + Default {
eprintln!("Failed to write file {}: {}", path.display(), e);
}
}
-} \ No newline at end of file
+}
diff --git a/crates/utils/cfg_file/src/lib.rs b/crates/utils/cfg_file/src/lib.rs
index 945c3fd..72246e7 100644
--- a/crates/utils/cfg_file/src/lib.rs
+++ b/crates/utils/cfg_file/src/lib.rs
@@ -4,4 +4,4 @@ extern crate cfg_file_derive;
#[cfg(feature = "derive")]
pub use cfg_file_derive::*;
-pub mod config; \ No newline at end of file
+pub mod config;