Решение на упр.06 задача 1 от Ясин Йосифов

Обратно към всички решения

Към профила на Ясин Йосифов

Резултати

  • 4 точки от тестове
  • 0 бонус точки
  • 4 точки общо
  • 4 успешни тест(а)
  • 0 неуспешни тест(а)

Код

use std::collections::HashMap;
use std::error::Error;
use std::io;
use std::io::Read;
use std::path::Path;
use std::path::PathBuf;
use std::fs::File;
use std::io::BufReader;
use std::io::BufRead;
#[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 info = AggregateInfo{ log_counts: HashMap::new(), skipped_files: Vec::new() };
for file in std::fs::read_dir(dir)? {
let path = file?.path();
if let Some(ext) = path.extension() {
if path.is_file() && ext == "log" {
parse_log_file(File::open(path)?, &mut info.log_counts);
}
else {
info.skipped_files.push(format!("{:?}", path));
}
}
}
Ok(info)
}
#[derive(Debug)]
enum ParseLogError {
Read(io::Error),
ParseLine,
}
impl From<io::Error> for ParseLogError {
fn from(error: io::Error) -> Self {
ParseLogError::Read(error)
}
}
fn parse_log_file<R>(file: R, map: &mut HashMap<LogLevel, usize>) -> Result<(), ParseLogError>
where
R: Read,
{
let reader = BufReader::new(file);
for lines in reader.lines() {
match lines {
Ok(val) => {
let mut split = val.split_whitespace();
if let Some(val) = split.next() {
let s: LogLevel;
if val == "ERROR" {
s = LogLevel::Error;
} else if val == "WARN" {
s = LogLevel::Warn;
} else if val == "INFO" {
s = LogLevel::Info;
} else if val == "DEBUG" {
s = LogLevel::Debug;
} else {
return Err(ParseLogError::ParseLine)
}
match map.get(&s) {
Some(v) => map.insert(s, v+1),
None => map.insert(s, 1),
};
}
},
Err(e) => return Err(e.into()),
}
}
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-io v0.3.31
   Compiling syn v2.0.110
   Compiling slab v0.4.11
   Compiling pin-utils v0.1.0
   Compiling memchr v2.7.6
   Compiling pin-project-lite v0.2.16
   Compiling futures-task v0.3.31
   Compiling solution v0.1.0 (/tmp/d20251120-1757769-ov6qq1/solution)
warning: unused import: `std::path::PathBuf`
 --> src/lib.rs:6:5
  |
6 | use std::path::PathBuf;
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: enum `LogLevel` is never used
  --> src/lib.rs:12:6
   |
12 | enum LogLevel {
   |      ^^^^^^^^
   |
   = note: `#[warn(dead_code)]` on by default

warning: struct `AggregateInfo` is never constructed
  --> src/lib.rs:19:8
   |
19 | struct AggregateInfo {
   |        ^^^^^^^^^^^^^

warning: function `aggregate_logs` is never used
  --> src/lib.rs:24:4
   |
24 | fn aggregate_logs(dir: &Path) -> Result<AggregateInfo, Box<dyn Error>> {
   |    ^^^^^^^^^^^^^^

warning: field `0` is never read
  --> src/lib.rs:42:10
   |
42 |     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
   |
42 |     Read(()),
   |          ~~

warning: variant `ParseLine` is never constructed
  --> src/lib.rs:43:5
   |
41 | enum ParseLogError {
   |      ------------- variant in this enum
42 |     Read(io::Error),
43 |     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:52:4
   |
52 | fn parse_log_file<R>(file: R, map: &mut HashMap<LogLevel, usize>) -> Result<(), ParseLogError>
   |    ^^^^^^^^^^^^^^

warning: unused `Result` that must be used
  --> src/lib.rs:30:17
   |
30 |                 parse_log_file(File::open(path)?, &mut info.log_counts);
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this `Result` may be an `Err` variant, which should be handled
   = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
   |
30 |                 let _ = parse_log_file(File::open(path)?, &mut info.log_counts);
   |                 +++++++

warning: `solution` (lib) generated 8 warnings (run `cargo fix --lib -p solution` to apply 1 suggestion)
   Compiling futures-macro v0.3.31
   Compiling futures-util v0.3.31
   Compiling futures-executor v0.3.31
   Compiling futures v0.3.31
warning: unused import: `std::path::PathBuf`
 --> tests/../src/lib.rs:6:5
  |
6 | use std::path::PathBuf;
  |     ^^^^^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: field `0` is never read
  --> tests/../src/lib.rs:42:10
   |
42 |     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
   |
42 |     Read(()),
   |          ~~

warning: unused `Result` that must be used
  --> tests/../src/lib.rs:30:17
   |
30 |                 parse_log_file(File::open(path)?, &mut info.log_counts);
   |                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = note: this `Result` may be an `Err` variant, which should be handled
   = note: `#[warn(unused_must_use)]` on by default
help: use `let _ = ...` to ignore the resulting value
   |
30 |                 let _ = parse_log_file(File::open(path)?, &mut info.log_counts);
   |                 +++++++

warning: `solution` (test "solution_test") generated 3 warnings (run `cargo fix --test "solution_test"` to apply 1 suggestion)
    Finished `test` profile [unoptimized + debuginfo] target(s) in 8.99s
     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

История (1 версия и 0 коментара)

Ясин качи първо решение на 19.11.2025 23:54 (преди 16 дена)