feat: add comprehensive formatting rules and type inference guidelines

This commit is contained in:
overtrue
2025-05-28 16:04:38 +08:00
parent f05a0b8e83
commit 8747a91df8
28 changed files with 792 additions and 383 deletions
+1 -3
View File
@@ -189,9 +189,7 @@ async fn apply_dynamic_config<S: StorageAPI>(cfg: &mut Config, api: Arc<S>) -> R
async fn apply_dynamic_config_for_sub_sys<S: StorageAPI>(cfg: &mut Config, api: Arc<S>, subsys: &str) -> Result<()> {
let set_drive_counts = api.set_drive_counts();
if subsys == STORAGE_CLASS_SUB_SYS {
let kvs = cfg
.get_value(STORAGE_CLASS_SUB_SYS, DEFAULT_KV_KEY)
.unwrap_or_default();
let kvs = cfg.get_value(STORAGE_CLASS_SUB_SYS, DEFAULT_KV_KEY).unwrap_or_default();
for (i, count) in set_drive_counts.iter().enumerate() {
match storageclass::lookup_config(&kvs, *count) {
+1 -1
View File
@@ -97,7 +97,7 @@ impl FileMeta {
if buf.len() < 5 {
return Err(Error::new(io::Error::new(
io::ErrorKind::UnexpectedEof,
format!("Buffer too small: {} bytes, need at least 5", buf.len())
format!("Buffer too small: {} bytes, need at least 5", buf.len()),
)));
}
+24 -58
View File
@@ -208,14 +208,7 @@ mod tests {
#[tokio::test]
async fn test_http_file_writer_creation() {
let writer = HttpFileWriter::new(
"http://localhost:8080",
"test-disk",
"test-volume",
"test-path",
1024,
false
);
let writer = HttpFileWriter::new("http://localhost:8080", "test-disk", "test-volume", "test-path", 1024, false);
assert!(writer.is_ok(), "HttpFileWriter creation should succeed");
}
@@ -228,7 +221,7 @@ mod tests {
"test/volume",
"test file with spaces & symbols.txt",
1024,
false
false,
);
assert!(writer.is_ok(), "HttpFileWriter creation with special characters should succeed");
@@ -242,7 +235,7 @@ mod tests {
"test-volume",
"append-test.txt",
1024,
true // append mode
true, // append mode
);
assert!(writer.is_ok(), "HttpFileWriter creation in append mode should succeed");
@@ -256,7 +249,7 @@ mod tests {
"test-volume",
"empty-file.txt",
0, // zero size
false
false,
);
assert!(writer.is_ok(), "HttpFileWriter creation with zero size should succeed");
@@ -270,7 +263,7 @@ mod tests {
"test-volume",
"large-file.txt",
1024 * 1024 * 100, // 100MB
false
false,
);
assert!(writer.is_ok(), "HttpFileWriter creation with large size should succeed");
@@ -278,14 +271,7 @@ mod tests {
#[tokio::test]
async fn test_http_file_writer_invalid_url() {
let writer = HttpFileWriter::new(
"invalid-url",
"test-disk",
"test-volume",
"test-path",
1024,
false
);
let writer = HttpFileWriter::new("invalid-url", "test-disk", "test-volume", "test-path", 1024, false);
// This should still succeed at creation time, errors occur during actual I/O
assert!(writer.is_ok(), "HttpFileWriter creation should succeed even with invalid URL");
@@ -295,14 +281,8 @@ mod tests {
async fn test_http_file_reader_creation() {
// Test creation without actually making HTTP requests
// We'll test the URL construction logic by checking the error messages
let result = HttpFileReader::new(
"http://invalid-server:9999",
"test-disk",
"test-volume",
"test-file.txt",
0,
1024
).await;
let result =
HttpFileReader::new("http://invalid-server:9999", "test-disk", "test-volume", "test-file.txt", 0, 1024).await;
// May succeed or fail depending on network conditions, but should not panic
// The important thing is that the URL construction logic works
@@ -317,8 +297,9 @@ mod tests {
"test-volume",
"test-file.txt",
100, // offset
500 // length
).await;
500, // length
)
.await;
// May succeed or fail, but this tests parameter handling
assert!(result.is_ok() || result.is_err(), "HttpFileReader creation should not panic");
@@ -332,8 +313,9 @@ mod tests {
"test-volume",
"test-file.txt",
0,
0 // zero length
).await;
0, // zero length
)
.await;
// May succeed or fail, but this tests zero length handling
assert!(result.is_ok() || result.is_err(), "HttpFileReader creation should not panic");
@@ -347,8 +329,9 @@ mod tests {
"test/volume",
"test file with spaces & symbols.txt",
0,
1024
).await;
1024,
)
.await;
// May succeed or fail, but this tests URL encoding
assert!(result.is_ok() || result.is_err(), "HttpFileReader creation should not panic");
@@ -505,11 +488,7 @@ mod tests {
let etag_reader3 = EtagReader::new(cursor3);
// Compute ETags concurrently
let (result1, result2, result3) = tokio::join!(
etag_reader1.etag(),
etag_reader2.etag(),
etag_reader3.etag()
);
let (result1, result2, result3) = tokio::join!(etag_reader1.etag(), etag_reader2.etag(), etag_reader3.etag());
// All ETags should be the same (empty data hash) since no data was read
assert_eq!(result1, result2);
@@ -533,7 +512,7 @@ mod tests {
"", // empty volume
"", // empty path
0, // zero size
false
false,
);
assert!(writer.is_ok(), "HttpFileWriter should handle empty parameters");
@@ -544,8 +523,9 @@ mod tests {
"", // empty volume
"", // empty path
0, // zero offset
0 // zero length
).await;
0, // zero length
)
.await;
// May succeed or fail, but parameters should be handled
assert!(result.is_ok() || result.is_err(), "HttpFileReader creation should not panic");
}
@@ -555,24 +535,10 @@ mod tests {
// Test with characters that need URL encoding
let special_chars = "test file with spaces & symbols + % # ? = @ ! $ ( ) [ ] { } | \\ / : ; , . < > \" '";
let writer = HttpFileWriter::new(
"http://localhost:8080",
special_chars,
special_chars,
special_chars,
1024,
false
);
let writer = HttpFileWriter::new("http://localhost:8080", special_chars, special_chars, special_chars, 1024, false);
assert!(writer.is_ok(), "HttpFileWriter should handle special characters");
let result = HttpFileReader::new(
"http://invalid:9999",
special_chars,
special_chars,
special_chars,
0,
1024
).await;
let result = HttpFileReader::new("http://invalid:9999", special_chars, special_chars, special_chars, 0, 1024).await;
// May succeed or fail, but URL encoding should work
assert!(result.is_ok() || result.is_err(), "HttpFileReader creation should not panic");
}
+18 -24
View File
@@ -17,28 +17,24 @@ pub fn get_info(p: impl AsRef<Path>) -> std::io::Result<Info> {
let reserved = match bfree.checked_sub(bavail) {
Some(reserved) => reserved,
None => {
return Err(Error::other(
format!(
"detected f_bavail space ({}) > f_bfree space ({}), fs corruption at ({}). please run fsck",
bavail,
bfree,
p.as_ref().display()
),
))
return Err(Error::other(format!(
"detected f_bavail space ({}) > f_bfree space ({}), fs corruption at ({}). please run fsck",
bavail,
bfree,
p.as_ref().display()
)))
}
};
let total = match blocks.checked_sub(reserved) {
Some(total) => total * bsize,
None => {
return Err(Error::other(
format!(
"detected reserved space ({}) > blocks space ({}), fs corruption at ({}). please run fsck",
reserved,
blocks,
p.as_ref().display()
),
))
return Err(Error::other(format!(
"detected reserved space ({}) > blocks space ({}), fs corruption at ({}). please run fsck",
reserved,
blocks,
p.as_ref().display()
)))
}
};
@@ -46,14 +42,12 @@ pub fn get_info(p: impl AsRef<Path>) -> std::io::Result<Info> {
let used = match total.checked_sub(free) {
Some(used) => used,
None => {
return Err(Error::other(
format!(
"detected free space ({}) > total drive space ({}), fs corruption at ({}). please run fsck",
free,
total,
p.as_ref().display()
),
))
return Err(Error::other(format!(
"detected free space ({}) > total drive space ({}), fs corruption at ({}). please run fsck",
free,
total,
p.as_ref().display()
)))
}
};