Mercurial > lbo > hg > syslog
changeset 15:ad5154cf110c draft
Add more conversion functions for facilities/levels
author | Lewin Bormann <lbo@spheniscida.de> |
---|---|
date | Sat, 03 Dec 2016 14:16:44 +0100 |
parents | e101908dc688 |
children | 4185ac5ed03d |
files | src/priority.rs |
diffstat | 1 files changed, 61 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/src/priority.rs Sat Dec 03 14:16:24 2016 +0100 +++ b/src/priority.rs Sat Dec 03 14:16:44 2016 +0100 @@ -2,8 +2,9 @@ use std::str::FromStr; -#[derive(Debug, PartialEq)] +#[derive(Debug, PartialEq, PartialOrd, Clone, Copy)] pub enum Level { + NONE = -1, EMERG = 0, ALERT = 1, CRIT = 2, @@ -32,7 +33,31 @@ } } -#[derive(Debug, PartialEq)] +pub fn str_to_level(s: &str) -> Option<Level> { + if s == "emerg" { + Some(Level::EMERG) + } else if s == "alert" { + Some(Level::ALERT) + } else if s == "crit" || s == "critical" { + Some(Level::CRIT) + } else if s == "err" { + Some(Level::ERR) + } else if s == "warn" || s == "warning" { + Some(Level::WARNING) + } else if s == "notice" { + Some(Level::NOTICE) + } else if s == "info" { + Some(Level::INFO) + } else if s == "debug" { + Some(Level::DEBUG) + } else if s == "none" { + Some(Level::NONE) + } else { + None + } +} + +#[derive(Debug, PartialEq, Clone, Copy)] pub enum Facility { KERN = 0, USER = 1, @@ -57,6 +82,40 @@ LOCAL7 = 23, } +pub fn str_to_facility(s: &str) -> Option<Facility> { + if s == "kern" { + Some(Facility::KERN) + } else if s == "user" { + Some(Facility::USER) + } else if s == "mail" { + Some(Facility::MAIL) + } else if s == "daemon" { + Some(Facility::DAEMON) + } else if s == "auth" { + Some(Facility::AUTH) + } else if s == "syslog" { + Some(Facility::SYSLOG) + } else if s == "lpr" { + Some(Facility::LPR) + } else if s == "news" { + Some(Facility::NEWS) + } else if s == "uucp" { + Some(Facility::UUCP) + } else if s == "cron" { + Some(Facility::CRON) + } else if s == "authpriv" { + Some(Facility::AUTHPRIV) + } else if s == "ftp" { + Some(Facility::FTP) + } else if s.starts_with("local") { + let suffix = &s[5..]; + let num = i32::from_str(suffix).unwrap_or(0); + to_facility(Facility::LOCAL0 as i32 + num) + } else { + None + } +} + pub fn to_facility(l: i32) -> Option<Facility> { if (l >= Facility::KERN as i32 && l <= Facility::FTP as i32) || (l >= Facility::LOCAL0 as i32 && l <= Facility::LOCAL7 as i32) {