Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented LogMessageId, LogMessage and LogMessageBuilder Implemented Display for LogMessageId and LogMessage
- Loading branch information
danielsanchezq
committed
Aug 3, 2020
1 parent
a3ab2bf
commit 756743a
Showing
9 changed files
with
104 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
use serde::export::Formatter; | ||
|
||
pub enum LogMessageId { | ||
None, | ||
Other(String), | ||
} | ||
|
||
pub struct LogMessage { | ||
id: LogMessageId, | ||
tags: Vec<String>, | ||
message: String, | ||
timestamp: i64, | ||
} | ||
|
||
pub struct LogMessageBuilder { | ||
id: LogMessageId, | ||
tags: Vec<String>, | ||
message: Option<String>, | ||
} | ||
|
||
impl std::fmt::Display for LogMessageId { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let id = match self { | ||
LogMessageId::None => "None", | ||
LogMessageId::Other(id) => id, | ||
}; | ||
write!(f, "{}", id) | ||
} | ||
} | ||
|
||
impl LogMessageBuilder { | ||
pub fn new() -> Self { | ||
Self { | ||
id: LogMessageId::None, | ||
tags: vec![], | ||
message: None, | ||
} | ||
} | ||
|
||
pub fn with_id(self, id: LogMessageId) -> Self { | ||
Self { | ||
id, | ||
tags: self.tags, | ||
message: self.message, | ||
} | ||
} | ||
|
||
pub fn with_tags(self, tags: Vec<String>) -> Self { | ||
Self { | ||
id: self.id, | ||
tags, | ||
message: self.message, | ||
} | ||
} | ||
|
||
pub fn with_message(self, message: String) -> Self { | ||
Self { | ||
id: self.id, | ||
tags: self.tags, | ||
message: Some(message), | ||
} | ||
} | ||
|
||
pub fn build(self) -> LogMessage { | ||
LogMessage { | ||
id: self.id, | ||
tags: self.tags, | ||
message: self.message.unwrap_or(Default::default()), | ||
timestamp: chrono::Utc::now().timestamp(), | ||
} | ||
} | ||
} | ||
|
||
impl LogMessage { | ||
pub fn new(id: LogMessageId, message: String, tags: Vec<String>) -> Self { | ||
Self { | ||
id, | ||
tags, | ||
message, | ||
timestamp: chrono::Utc::now().timestamp(), | ||
} | ||
} | ||
} | ||
|
||
impl std::fmt::Display for LogMessage { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
let tags = format!("[{}]", self.tags.join(":")); | ||
let id = format!("[{}]", self.id); | ||
write!( | ||
f, | ||
"{} - {} - {} - {}", | ||
id, tags, self.timestamp, self.message | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod config; | ||
pub mod messages; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
pub mod bootstrapping; | ||
pub mod exit_codes; | ||
pub mod log; | ||
pub mod settings; | ||
|
||
pub use bootstrapping::start_server; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters