Add clean-logs-older-than option
This commit is contained in:
parent
4c7d108e09
commit
880c98ecfb
17 changed files with 360 additions and 10 deletions
|
|
@ -9,6 +9,7 @@ use {
|
|||
pub mod action;
|
||||
mod actions;
|
||||
mod capabilities;
|
||||
mod clean_logs_older_than;
|
||||
mod client_match;
|
||||
mod client_rule;
|
||||
mod color;
|
||||
|
|
|
|||
57
toml-config/src/config/parsers/clean_logs_older_than.rs
Normal file
57
toml-config/src/config/parsers/clean_logs_older_than.rs
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
use {
|
||||
crate::{
|
||||
config::{
|
||||
context::Context,
|
||||
extractor::{Extractor, ExtractorError, fltorint, opt},
|
||||
parser::{DataType, ParseResult, Parser, UnexpectedDataType},
|
||||
},
|
||||
toml::{
|
||||
toml_span::{DespanExt, Span, Spanned, SpannedExt},
|
||||
toml_value::Value,
|
||||
},
|
||||
},
|
||||
indexmap::IndexMap,
|
||||
std::time::{Duration, TryFromFloatSecsError},
|
||||
thiserror::Error,
|
||||
};
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum CleanLogsOlderThanParserError {
|
||||
#[error(transparent)]
|
||||
Expected(#[from] UnexpectedDataType),
|
||||
#[error(transparent)]
|
||||
Extract(#[from] ExtractorError),
|
||||
#[error("At least one of the `weeks` or `days` fields must be specified")]
|
||||
WeeksOrDays,
|
||||
#[error("Duration is invalid")]
|
||||
InvalidDuration(#[source] TryFromFloatSecsError),
|
||||
}
|
||||
|
||||
pub struct CleanLogsOlderThanParser<'a>(pub &'a Context<'a>);
|
||||
|
||||
impl Parser for CleanLogsOlderThanParser<'_> {
|
||||
type Value = Duration;
|
||||
type Error = CleanLogsOlderThanParserError;
|
||||
const EXPECTED: &'static [DataType] = &[DataType::Table];
|
||||
|
||||
fn parse_table(
|
||||
&mut self,
|
||||
span: Span,
|
||||
table: &IndexMap<Spanned<String>, Spanned<Value>>,
|
||||
) -> ParseResult<Self> {
|
||||
let mut ext = Extractor::new(self.0, span, table);
|
||||
let (weeks, days) = ext.extract((opt(fltorint("weeks")), opt(fltorint("days"))))?;
|
||||
if weeks.is_none() && days.is_none() {
|
||||
return Err(CleanLogsOlderThanParserError::WeeksOrDays.spanned(span));
|
||||
}
|
||||
const SECONDS_IN_WEEK: f64 = 604800.0;
|
||||
const SECONDS_IN_DAY: f64 = 86400.0;
|
||||
let duration = Duration::try_from_secs_f64(
|
||||
weeks.despan().unwrap_or_default() * SECONDS_IN_WEEK
|
||||
+ days.despan().unwrap_or_default() * SECONDS_IN_DAY,
|
||||
)
|
||||
.map_err(CleanLogsOlderThanParserError::InvalidDuration)
|
||||
.map_err(|e| e.spanned(span))?;
|
||||
Ok(duration)
|
||||
}
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ use {
|
|||
parsers::{
|
||||
action::ActionParser,
|
||||
actions::ActionsParser,
|
||||
clean_logs_older_than::CleanLogsOlderThanParser,
|
||||
client_rule::ClientRulesParser,
|
||||
color_management::ColorManagementParser,
|
||||
connector::ConnectorsParser,
|
||||
|
|
@ -152,6 +153,7 @@ impl Parser for ConfigParser<'_> {
|
|||
show_titles,
|
||||
fallback_output_mode_val,
|
||||
egui_val,
|
||||
clean_logs_older_than_val,
|
||||
),
|
||||
) = ext.extract((
|
||||
(
|
||||
|
|
@ -211,6 +213,7 @@ impl Parser for ConfigParser<'_> {
|
|||
recover(opt(bol("show-titles"))),
|
||||
opt(val("fallback-output-mode")),
|
||||
opt(val("egui")),
|
||||
opt(val("clean-logs-older-than")),
|
||||
),
|
||||
))?;
|
||||
let mut keymap = None;
|
||||
|
|
@ -307,6 +310,17 @@ impl Parser for ConfigParser<'_> {
|
|||
}
|
||||
}
|
||||
}
|
||||
let mut clean_logs_older_than = None;
|
||||
if let Some(value) = clean_logs_older_than_val {
|
||||
match value.parse(&mut CleanLogsOlderThanParser(self.0)) {
|
||||
Ok(v) => {
|
||||
clean_logs_older_than = Some(v);
|
||||
}
|
||||
Err(e) => {
|
||||
log::warn!("Could not parse clean-logs-older-than: {}", self.0.error(e));
|
||||
}
|
||||
}
|
||||
}
|
||||
let mut theme = Theme::default();
|
||||
if let Some(value) = theme_val {
|
||||
match value.parse(&mut ThemeParser(self.0)) {
|
||||
|
|
@ -567,6 +581,7 @@ impl Parser for ConfigParser<'_> {
|
|||
keymaps,
|
||||
auto_reload: auto_reload.despan(),
|
||||
log_level,
|
||||
clean_logs_older_than,
|
||||
theme,
|
||||
egui,
|
||||
gfx_api,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue