summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndinus <andinus@nand.sh>2021-09-29 20:17:40 +0530
committerAndinus <andinus@nand.sh>2021-09-29 20:17:40 +0530
commitb7f76db2a02aba793a68ba35c2b4b51d5e069bdc (patch)
treeeb03e291aebb4e91e2a5118b9f738cd2ff97002e
parent70cadb4dffe89acb5fcb6265bfcf9e978121e83e (diff)
downloadexercism-b7f76db2a02aba793a68ba35c2b4b51d5e069bdc.tar.gz
Rust: Semi Structured Logs: Implement Display trait to format log
-rw-r--r--README.org4
-rw-r--r--rust/semi-structured-logs/src/lib.rs21
2 files changed, 15 insertions, 10 deletions
diff --git a/README.org b/README.org
index 470a2ee..a457ed8 100644
--- a/README.org
+++ b/README.org
@@ -91,10 +91,10 @@ My solutions for [[https://exercism.io][Exercism]] exercises.
- [ ] Acronym
- [ ] Allergies
-* Rust [4/5]
+* Rust [5/5]
- [X] Hello World
- [X] Lucian's Luscious Lasagna
- [X] Armstrong Numbers
- [X] Assembly Line
-- [ ] Semi Structured Logs
+- [X] Semi Structured Logs
diff --git a/rust/semi-structured-logs/src/lib.rs b/rust/semi-structured-logs/src/lib.rs
index 4f06874..0160b1d 100644
--- a/rust/semi-structured-logs/src/lib.rs
+++ b/rust/semi-structured-logs/src/lib.rs
@@ -1,25 +1,30 @@
+use std::fmt::{Display, Formatter, Result};
+
+#[derive(Debug)]
pub enum LogLevel {
Info,
Warning,
Error,
}
-pub fn log(level: LogLevel, message: &str) -> String {
- match level {
- LogLevel::Info => info(message),
- LogLevel::Warning => warn(message),
- LogLevel::Error => error(message),
+impl Display for LogLevel {
+ fn fmt(&self, f: &mut Formatter) -> Result {
+ write!(f, "{:?}", self)
}
}
+pub fn log(level: LogLevel, message: &str) -> String {
+ format!("[{}]: {}", level.to_string().to_uppercase(), message)
+}
+
pub fn info(message: &str) -> String {
- format!("[INFO]: {}", message)
+ log(LogLevel::Info, message)
}
pub fn warn(message: &str) -> String {
- format!("[WARNING]: {}", message)
+ log(LogLevel::Warning, message)
}
pub fn error(message: &str) -> String {
- format!("[ERROR]: {}", message)
+ log(LogLevel::Error, message)
}