Skip to content

Commit 183af79

Browse files
committed
Added a new usage
1 parent 8de0e1f commit 183af79

4 files changed

Lines changed: 87 additions & 6 deletions

File tree

Lesson_09/src/logging.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
use crate::models::{Level, Log};
2+
use std::io::{Write, stdout};
3+
4+
pub fn log() -> impl FnMut(&Log) {
5+
let mut error_count = 0;
6+
let mut warn_count = 0;
7+
let mut info_count = 0;
8+
move |l| {
9+
stdout()
10+
.write(format!("{}\n", l.to_string()).as_bytes())
11+
.unwrap();
12+
match l.level {
13+
Level::Error => error_count += 1,
14+
Level::Warn => warn_count += 1,
15+
Level::Info => info_count += 1,
16+
_ => {}
17+
}
18+
stdout()
19+
.write(
20+
format!(
21+
"Log Tracker: {} errors, {} warnings, {} infos\n",
22+
error_count, warn_count, info_count
23+
)
24+
.as_bytes(),
25+
)
26+
.unwrap();
27+
}
28+
}

Lesson_09/src/main.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
mod logging;
12
mod models;
23
mod repository;
34
mod systems;
45

6+
use crate::logging::log;
57
use crate::models::*;
68
use crate::systems::*;
79
use repository::*;
@@ -71,4 +73,15 @@ fn main() {
7173
total_team_score += p.score;
7274
});
7375
println!("Total score after update: {}", total_team_score);
76+
77+
// FnMut döndüren log fonksiyonunun kullanımı
78+
let mut logger = log();
79+
80+
logger(&Log::new(Level::Info, "Authentication Success".to_string()));
81+
logger(&Log::new(Level::Error, "File Not Found".to_string()));
82+
logger(&Log::new(Level::Error, "Login failed".to_string()));
83+
logger(&Log::new(
84+
Level::Warn,
85+
"Response Time Decreasing".to_string(),
86+
));
7487
}

Lesson_09/src/models.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
use std::fmt::Display;
2+
3+
#[derive(Debug)]
4+
pub struct Game {
5+
pub title: String,
6+
pub year: u16,
7+
pub popularity: f32,
8+
}
9+
110
#[derive(Debug)]
211
pub struct Player {
312
pub id: u32,
@@ -10,3 +19,38 @@ pub struct Player {
1019
pub struct GameWorld {
1120
pub players: Vec<Player>,
1221
}
22+
23+
#[derive(Debug)]
24+
pub struct Log {
25+
pub level: Level,
26+
pub explanation: String,
27+
}
28+
29+
impl Log {
30+
pub fn new(level: Level, explanation: String) -> Self {
31+
Log { level, explanation }
32+
}
33+
}
34+
35+
impl Display for Log {
36+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37+
write!(f, "{}:{}", self.level, self.explanation)
38+
}
39+
}
40+
41+
#[derive(Debug)]
42+
pub enum Level {
43+
Info,
44+
Warn,
45+
Error,
46+
}
47+
48+
impl Display for Level {
49+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
50+
match self {
51+
Level::Info => write!(f, "INFO"),
52+
Level::Warn => write!(f, "WARN"),
53+
Level::Error => write!(f, "ERROR"),
54+
}
55+
}
56+
}

Lesson_09/src/repository.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
1-
#[derive(Debug)]
2-
pub struct Game {
3-
pub title: String,
4-
pub year: u16,
5-
pub popularity: f32,
6-
}
1+
use crate::models::Game;
2+
73
pub fn print_games(games: &Vec<Game>) {
84
for game in games {
95
println!("{}, {}", game.year, game.title);

0 commit comments

Comments
 (0)