config: store and parse 'filter' as regex

This commit is contained in:
Bruno BELANYI 2021-04-10 02:01:19 +02:00 committed by Antoine Martin
parent 54f67887aa
commit ad420dc874
3 changed files with 38 additions and 2 deletions

24
Cargo.lock generated
View file

@ -66,6 +66,15 @@ dependencies = [
"opaque-debug", "opaque-debug",
] ]
[[package]]
name = "aho-corasick"
version = "0.7.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7404febffaa47dac81aa44dba71523c9d069b1bdc50a77db41195149e17f68e5"
dependencies = [
"memchr",
]
[[package]] [[package]]
name = "ansi_term" name = "ansi_term"
version = "0.12.1" version = "0.12.1"
@ -132,7 +141,9 @@ dependencies = [
"clap", "clap",
"futures", "futures",
"matrix-sdk", "matrix-sdk",
"regex",
"serde", "serde",
"serde_regex",
"serde_yaml", "serde_yaml",
"systemd", "systemd",
"thiserror", "thiserror",
@ -1439,7 +1450,10 @@ version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a" checksum = "d9251239e129e16308e70d853559389de218ac275b515068abc96829d05b948a"
dependencies = [ dependencies = [
"aho-corasick",
"memchr",
"regex-syntax", "regex-syntax",
"thread_local",
] ]
[[package]] [[package]]
@ -1774,6 +1788,16 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_regex"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8136f1a4ea815d7eac4101cfd0b16dc0cb5e1fe1b8609dfd728058656b7badf"
dependencies = [
"regex",
"serde",
]
[[package]] [[package]]
name = "serde_urlencoded" name = "serde_urlencoded"
version = "0.7.0" version = "0.7.0"

View file

@ -10,9 +10,11 @@ edition = "2018"
anyhow = "1.0" anyhow = "1.0"
clap = "3.0.0-beta.2" clap = "3.0.0-beta.2"
futures = "0.3" futures = "0.3"
regex = "1"
tokio = { version = "1", features = [ "full" ] } tokio = { version = "1", features = [ "full" ] }
tracing-subscriber = "0.2" tracing-subscriber = "0.2"
url = { version = "2.2", features = [ "serde" ] } url = { version = "2.2", features = [ "serde" ] }
serde_regex = "1"
serde_yaml = "0.8" serde_yaml = "0.8"
serde = "1.0" serde = "1.0"
systemd = "0.8" systemd = "0.8"

View file

@ -1,4 +1,5 @@
use matrix_sdk::identifiers::RoomId; use matrix_sdk::identifiers::RoomId;
use regex::Regex;
use serde::de::{self, MapAccess, Visitor}; use serde::de::{self, MapAccess, Visitor};
use serde::{Deserialize, Deserializer}; use serde::{Deserialize, Deserializer};
use std::fmt; use std::fmt;
@ -27,15 +28,24 @@ pub struct Config {
} }
/// Holds a single unit's configuration. /// Holds a single unit's configuration.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq)] #[derive(Clone, Debug, Deserialize)]
#[serde(from = "SerializedUnit")] #[serde(from = "SerializedUnit")]
pub struct Unit { pub struct Unit {
/// Can be serialized from a string only instead of a map. /// Can be serialized from a string only instead of a map.
pub name: String, pub name: String,
/// Regex to filter each line read from the unit's logs. /// Regex to filter each line read from the unit's logs.
pub filter: Option<String>, // FIXME: regex #[serde(with = "serde_regex")]
pub filter: Option<Regex>,
} }
impl PartialEq for Unit {
fn eq(&self, other: &Self) -> bool {
self.name == other.name
}
}
impl Eq for Unit {}
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
#[serde(transparent)] #[serde(transparent)]
struct SerializedUnit(#[serde(deserialize_with = "unit_name_or_struct")] Unit); struct SerializedUnit(#[serde(deserialize_with = "unit_name_or_struct")] Unit);