use crate::*;
use std::time::Duration;
pub enum Context {
Header {
time : Time,
source : Source,
location: Location,
},
Footer {
time : Time,
source : Source,
location: Location,
elapsed : Duration,
},
Message {
time : Time,
source : Source,
location: Location,
level : Level,
message : String,
},
}
impl Context {
pub fn time(&self) -> &Time {
match self {
Self::Header { time, .. } => time,
Self::Footer { time, .. } => time,
Self::Message { time, .. } => time,
}
}
pub fn source(&self) -> &Source {
match self {
Self::Header { source, .. } => source,
Self::Footer { source, .. } => source,
Self::Message { source, .. } => source,
}
}
pub fn location(&self) -> &Location {
match self {
Self::Header { location, .. } => location,
Self::Footer { location, .. } => location,
Self::Message { location, .. } => location,
}
}
pub fn level(&self) -> Option<Level> {
match self {
Self::Message { level, .. } => Some(*level),
_ => None,
}
}
pub fn message(&self) -> Option<&str> {
match self {
Self::Message { message, .. } => Some(message),
_ => None,
}
}
pub fn start(&self) -> Option<&Time> {
match self {
Self::Header { time, .. } => Some(time),
_ => None,
}
}
pub fn elapsed(&self) -> Option<Duration> {
match self {
Self::Footer { elapsed, .. } => Some(*elapsed),
_ => None,
}
}
pub fn is_header(&self) -> bool {
matches!(self, Self::Header { .. })
}
pub fn is_footer(&self) -> bool {
matches!(self, Self::Footer { .. })
}
pub fn is_message(&self) -> bool {
matches!(self, Self::Message { .. })
}
#[track_caller]
pub(crate) fn new_header(source: Source) -> Self {
Self::Header {
time : Time::new(),
location: Location::new(),
source,
}
}
#[track_caller]
pub(crate) fn new_footer(source: Source, start: &Time, location: Location) -> Self {
let elapsed = Time::new().raw().signed_duration_since(start.raw()).to_std().unwrap();
Self::Footer {
time: Time::new(),
source,
location,
elapsed,
}
}
#[track_caller]
pub(crate) fn new_message(source: Source, level: Level, message: impl Into<String>) -> Self {
Self::Message {
time : Time::new(),
location: Location::new(),
message : message.into(),
source,
level,
}
}
}