Решение на упр.06 задача 1 от Симеон Георгиев
Към профила на Симеон Георгиев
Резултати
- 4 точки от тестове
- 0 бонус точки
- 4 точки общо
- 4 успешни тест(а)
- 0 неуспешни тест(а)
Код
use std::collections::HashMap;
use std::error::Error;
use std::fs;
use std::io;
use std::io::Read;
use std::io::BufRead;
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum LogLevel {
Error,
Warn,
Info,
Debug,
}
struct AggregateInfo {
log_counts: HashMap<LogLevel, usize>,
skipped_files: Vec<String>,
}
fn aggregate_logs(dir: &Path) -> Result<AggregateInfo, Box<dyn Error>> {
let mut log_counts : HashMap<LogLevel, usize> = HashMap::new();
let mut skipped_files : Vec<String> = Vec::new();
for entry in fs::read_dir(dir)? {
let entry = entry?;
let path = entry.path();
if path.extension().map_or(false, |s| s == "log") {
let file = fs::File::open(&path)?;
let _ = parse_log_file(file, &mut log_counts);
} else {
skipped_files.push(String::from(path.to_string_lossy()));
}
}
Ok(AggregateInfo{
log_counts: log_counts,
skipped_files: skipped_files,
})
}
#[derive(Debug)]
enum ParseLogError {
Read(io::Error),
ParseLine,
}
impl From<io::Error> for ParseLogError {
fn from(e: io::Error) -> Self {
ParseLogError::Read(e)
}
}
fn parse_log_file<R>(file: R, map: &mut HashMap<LogLevel, usize>) -> Result<(), ParseLogError>
where
R: Read,
{
let reader = io::BufReader::new(file);
for line_result in reader.lines() {
let line = line_result?;
let log_level : LogLevel = match line.split_once(' ') {
Some((level, _)) => {
match level {
"ERROR" => LogLevel::Error,
"WARN" => LogLevel::Warn,
"INFO" => LogLevel::Info,
"DEBUG" => LogLevel::Debug,
_ => { return Err(ParseLogError::ParseLine) },
}
},
None => { return Err(ParseLogError::ParseLine) },
};
let count = map.entry(log_level).or_insert(0);
*count += 1;
}
Ok(())
}
Лог от изпълнението
Updating crates.io index
Locking 17 packages to latest compatible versions
Compiling proc-macro2 v1.0.103
Compiling quote v1.0.42
Compiling unicode-ident v1.0.22
Compiling futures-sink v0.3.31
Compiling futures-core v0.3.31
Compiling futures-channel v0.3.31
Compiling futures-task v0.3.31
Compiling futures-io v0.3.31
Compiling memchr v2.7.6
Compiling syn v2.0.110
Compiling pin-utils v0.1.0
Compiling slab v0.4.11
Compiling pin-project-lite v0.2.16
Compiling solution v0.1.0 (/tmp/d20251120-1757769-1uzbdc0/solution)
warning: enum `LogLevel` is never used
--> src/lib.rs:10:6
|
10 | enum LogLevel {
| ^^^^^^^^
|
= note: `#[warn(dead_code)]` on by default
warning: struct `AggregateInfo` is never constructed
--> src/lib.rs:17:8
|
17 | struct AggregateInfo {
| ^^^^^^^^^^^^^
warning: function `aggregate_logs` is never used
--> src/lib.rs:22:4
|
22 | fn aggregate_logs(dir: &Path) -> Result<AggregateInfo, Box<dyn Error>> {
| ^^^^^^^^^^^^^^
warning: field `0` is never read
--> src/lib.rs:46:10
|
46 | Read(io::Error),
| ---- ^^^^^^^^^
| |
| field in this variant
|
= note: `ParseLogError` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
|
46 | Read(()),
| ~~
warning: variant `ParseLine` is never constructed
--> src/lib.rs:47:5
|
45 | enum ParseLogError {
| ------------- variant in this enum
46 | Read(io::Error),
47 | ParseLine,
| ^^^^^^^^^
|
= note: `ParseLogError` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
warning: function `parse_log_file` is never used
--> src/lib.rs:56:4
|
56 | fn parse_log_file<R>(file: R, map: &mut HashMap<LogLevel, usize>) -> Result<(), ParseLogError>
| ^^^^^^^^^^^^^^
warning: `solution` (lib) generated 6 warnings
Compiling futures-macro v0.3.31
Compiling futures-util v0.3.31
Compiling futures-executor v0.3.31
Compiling futures v0.3.31
warning: field `0` is never read
--> tests/../src/lib.rs:46:10
|
46 | Read(io::Error),
| ---- ^^^^^^^^^
| |
| field in this variant
|
= note: `ParseLogError` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
= note: `#[warn(dead_code)]` on by default
help: consider changing the field to be of unit type to suppress this warning while preserving the field numbering, or remove the field
|
46 | Read(()),
| ~~
warning: `solution` (test "solution_test") generated 1 warning
Finished `test` profile [unoptimized + debuginfo] target(s) in 8.74s
Running tests/solution_test.rs (target/debug/deps/solution_test-f75e629a1d90e17c)
running 4 tests
test solution_test::test_parse_log_basic ... ok
test solution_test::test_aggregate ... ok
test solution_test::test_parse_log_big_data ... ok
test solution_test::test_parse_log_invalid ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
