logged_stream/record.rs
1use chrono::DateTime;
2use chrono::Utc;
3use std::fmt;
4
5//////////////////////////////////////////////////////////////////////////////////////////////////////////////
6// Record
7//////////////////////////////////////////////////////////////////////////////////////////////////////////////
8
9/// This structure represents a log record and contains message string, creation timestamp ([`DateTime`]<[`Utc`]>)
10/// and record kind ([`RecordKind`]).
11#[derive(Debug, Clone, PartialEq, Eq, Hash)]
12pub struct Record {
13 pub kind: RecordKind,
14 pub message: String,
15 pub time: DateTime<Utc>,
16}
17
18impl Record {
19 /// Construct a new instance of [`Record`] using provided message and kind.
20 pub fn new(kind: RecordKind, message: String) -> Self {
21 Self {
22 kind,
23 message,
24 time: Utc::now(),
25 }
26 }
27}
28
29//////////////////////////////////////////////////////////////////////////////////////////////////////////////
30// RecordKind
31//////////////////////////////////////////////////////////////////////////////////////////////////////////////
32
33/// This enumeration represents log record kind. It is contained inside [`Record`] and helps to determine
34/// how to work with log record message content which is different for each log record kind.
35#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
36pub enum RecordKind {
37 Open,
38 Read,
39 Write,
40 Error,
41 Shutdown,
42 Drop,
43}
44
45impl fmt::Display for RecordKind {
46 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47 write!(f, "{}", char::from(*self))
48 }
49}
50
51impl From<RecordKind> for char {
52 fn from(kind: RecordKind) -> Self {
53 match kind {
54 RecordKind::Open => '+',
55 RecordKind::Read => '<',
56 RecordKind::Write => '>',
57 RecordKind::Error => '!',
58 RecordKind::Shutdown => '-',
59 RecordKind::Drop => 'x',
60 }
61 }
62}