51 lines
1.4 KiB
Rust
51 lines
1.4 KiB
Rust
use std::fmt;
|
|
|
|
/// Priorität eines Jobs (Spezifikation, Abschnitt 6). Die Reihenfolge ist
|
|
/// `Low < Normal < High`.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
pub enum Priority {
|
|
Low,
|
|
Normal,
|
|
High,
|
|
}
|
|
|
|
impl Priority {
|
|
/// Schreibweise in Datenbank und Header: `low`, `normal`, `high`.
|
|
pub fn as_str(self) -> &'static str {
|
|
match self {
|
|
Priority::Low => "low",
|
|
Priority::Normal => "normal",
|
|
Priority::High => "high",
|
|
}
|
|
}
|
|
|
|
/// Liest `low`, `normal`, `high` (Groß- und Kleinschreibung egal).
|
|
pub fn parse(text: &str) -> Option<Self> {
|
|
match text.trim().to_ascii_lowercase().as_str() {
|
|
"low" => Some(Priority::Low),
|
|
"normal" => Some(Priority::Normal),
|
|
"high" => Some(Priority::High),
|
|
_ => None,
|
|
}
|
|
}
|
|
}
|
|
|
|
impl fmt::Display for Priority {
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
f.write_str(self.as_str())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn reihenfolge_und_umwandlung() {
|
|
assert!(Priority::Low < Priority::Normal && Priority::Normal < Priority::High);
|
|
assert_eq!(Priority::parse(" HIGH "), Some(Priority::High));
|
|
assert_eq!(Priority::parse("vip"), None);
|
|
assert_eq!(Priority::Normal.to_string(), "normal");
|
|
}
|
|
}
|