From f8858501d70104f689559bd059a2e70ca0b49064 Mon Sep 17 00:00:00 2001 From: eater <=@eater.me> Date: Wed, 25 Dec 2019 19:57:43 +0100 Subject: [PATCH] libsysf -> sysf + complete unit type --- Cargo.lock | 4 +- Cargo.toml | 2 +- libsysf/src/unit.rs | 13 --- sysf-init/Cargo.toml | 2 +- sysf-init/src/main.rs | 2 +- {libsysf => sysf}/Cargo.lock | 4 +- {libsysf => sysf}/Cargo.toml | 5 +- {libsysf => sysf}/src/config/mod.rs | 10 +- {libsysf => sysf}/src/config/parser.rs | 19 ++-- {libsysf => sysf}/src/lib.rs | 0 {libsysf => sysf}/src/time.rs | 47 +++++---- sysf/src/unit.rs | 128 +++++++++++++++++++++++++ 12 files changed, 192 insertions(+), 44 deletions(-) delete mode 100644 libsysf/src/unit.rs rename {libsysf => sysf}/Cargo.lock (75%) rename {libsysf => sysf}/Cargo.toml (54%) rename {libsysf => sysf}/src/config/mod.rs (89%) rename {libsysf => sysf}/src/config/parser.rs (86%) rename {libsysf => sysf}/src/lib.rs (100%) rename {libsysf => sysf}/src/time.rs (74%) create mode 100644 sysf/src/unit.rs diff --git a/Cargo.lock b/Cargo.lock index b87cb7b..5d59379 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,13 +1,13 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "libsysf" +name = "sysf" version = "0.1.0" [[package]] name = "sysf-init" version = "0.1.0" dependencies = [ - "libsysf 0.1.0", + "sysf 0.1.0", ] diff --git a/Cargo.toml b/Cargo.toml index 5ff5625..2119464 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] members = [ - "libsysf", + "sysf", "sysf-init" ] diff --git a/libsysf/src/unit.rs b/libsysf/src/unit.rs deleted file mode 100644 index 770f6b6..0000000 --- a/libsysf/src/unit.rs +++ /dev/null @@ -1,13 +0,0 @@ -#[allow(dead_code)] -pub struct Unit { - description: String, - documentation: Vec, - wants: Vec, - requires: Vec, - requisite: Vec, - binds_to: Vec, - part_of: Vec, - conflicts: Vec, - before: Vec, - after: Vec, -} \ No newline at end of file diff --git a/sysf-init/Cargo.toml b/sysf-init/Cargo.toml index 1d34ec2..c16e08d 100644 --- a/sysf-init/Cargo.toml +++ b/sysf-init/Cargo.toml @@ -5,4 +5,4 @@ authors = ["eater <=@eater.me>"] edition = "2018" [dependencies] -libsysf = { path = "../libsysf" } \ No newline at end of file +sysf = { path = "../sysf" } \ No newline at end of file diff --git a/sysf-init/src/main.rs b/sysf-init/src/main.rs index 9b7e29c..d560e21 100644 --- a/sysf-init/src/main.rs +++ b/sysf-init/src/main.rs @@ -1,4 +1,4 @@ -extern crate libsysf; +extern crate sysf; fn main() { diff --git a/libsysf/Cargo.lock b/sysf/Cargo.lock similarity index 75% rename from libsysf/Cargo.lock rename to sysf/Cargo.lock index 9a15ea2..561fbf8 100644 --- a/libsysf/Cargo.lock +++ b/sysf/Cargo.lock @@ -1,6 +1,8 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "systemf" +name = "sysf" version = "0.1.0" +[lib] +crate-type = "dylib" \ No newline at end of file diff --git a/libsysf/Cargo.toml b/sysf/Cargo.toml similarity index 54% rename from libsysf/Cargo.toml rename to sysf/Cargo.toml index 02474fd..d1ea460 100644 --- a/libsysf/Cargo.toml +++ b/sysf/Cargo.toml @@ -1,5 +1,6 @@ [package] -name = "libsysf" +name = "sysf" version = "0.1.0" authors = ["eater <=@eater.me>"] -edition = "2018" \ No newline at end of file +edition = "2018" +crate-type = "dylib" \ No newline at end of file diff --git a/libsysf/src/config/mod.rs b/sysf/src/config/mod.rs similarity index 89% rename from libsysf/src/config/mod.rs rename to sysf/src/config/mod.rs index 19bd018..4916c4e 100644 --- a/libsysf/src/config/mod.rs +++ b/sysf/src/config/mod.rs @@ -1,6 +1,6 @@ use std::collections::HashMap; use crate::config::parser::{ParserError, parse_config}; -use crate::time::{parse_time_span, TimeSpan}; +use crate::time::{parse_finite_time_span, FiniteTimeSpan, TimeSpan, parse_time_span}; pub mod parser; @@ -66,6 +66,10 @@ impl Section { self.entries.get(name).map(|x| x.get_list()) } + pub fn get_finite_time_span(&self, name: &str) -> Option { + self.entries.get(name).and_then(|x| x.get_finite_time_span()) + } + pub fn get_time_span(&self, name: &str) -> Option { self.entries.get(name).and_then(|x| x.get_time_span()) } @@ -130,6 +134,10 @@ impl Entry { pub fn get_time_span(&self) -> Option { self.last_value().and_then(parse_time_span) } + + pub fn get_finite_time_span(&self) -> Option { + self.last_value().and_then(parse_finite_time_span) + } } #[derive(Clone, Debug, Default)] diff --git a/libsysf/src/config/parser.rs b/sysf/src/config/parser.rs similarity index 86% rename from libsysf/src/config/parser.rs rename to sysf/src/config/parser.rs index f6ce312..7d603b3 100644 --- a/libsysf/src/config/parser.rs +++ b/sysf/src/config/parser.rs @@ -88,7 +88,8 @@ pub(crate) fn parse_config(config: &mut Config, filename: String, contents: Stri #[cfg(test)] mod tests { use super::*; - use crate::config::TimeSpan; + use crate::time::FiniteTimeSpan; + use crate::time::TimeSpan::{Finite, Infinite}; #[test] fn test_parse() { @@ -165,6 +166,8 @@ Jump=Oh no\ 4=3m 5=1M1m 6=blaat +7=infinite +8=infinity ".to_string()); assert!(res.is_ok()); @@ -172,11 +175,15 @@ Jump=Oh no\ let unit = config.sections.get("Unit").unwrap(); - assert_eq!(unit.get_time_span("1"), Some(TimeSpan::new().with_hours(3))); - assert_eq!(unit.get_time_span("2"), Some(TimeSpan::new().with_hours(1))); - assert_eq!(unit.get_time_span("3"), Some(TimeSpan::new().with_months(3))); - assert_eq!(unit.get_time_span("4"), Some(TimeSpan::new().with_minutes(3))); - assert_eq!(unit.get_time_span("5"), Some(TimeSpan::new().with_months(1).with_minutes(1))); + assert_eq!(unit.get_time_span("1"), Some(Finite(FiniteTimeSpan::new().with_hours(3)))); + assert_eq!(unit.get_time_span("2"), Some(Finite(FiniteTimeSpan::new().with_hours(1)))); + assert_eq!(unit.get_time_span("3"), Some(Finite(FiniteTimeSpan::new().with_months(3)))); + assert_eq!(unit.get_time_span("4"), Some(Finite(FiniteTimeSpan::new().with_minutes(3)))); + assert_eq!(unit.get_time_span("5"), Some(Finite(FiniteTimeSpan::new().with_months(1).with_minutes(1)))); assert_eq!(unit.get_time_span("6"), None); + assert_eq!(unit.get_finite_time_span("8"), None); + assert_eq!(unit.get_finite_time_span("9"), None); + assert_eq!(unit.get_time_span("8"), Some(Infinite)); + assert_eq!(unit.get_time_span("9"), Some(Infinite)); } } \ No newline at end of file diff --git a/libsysf/src/lib.rs b/sysf/src/lib.rs similarity index 100% rename from libsysf/src/lib.rs rename to sysf/src/lib.rs diff --git a/libsysf/src/time.rs b/sysf/src/time.rs similarity index 74% rename from libsysf/src/time.rs rename to sysf/src/time.rs index aa7c9b5..def5d7b 100644 --- a/libsysf/src/time.rs +++ b/sysf/src/time.rs @@ -1,5 +1,13 @@ +use crate::time::TimeSpan::{Infinite, Finite}; + +#[derive(Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)] +pub enum TimeSpan { + Infinite, + Finite(FiniteTimeSpan) +} + #[derive(Default, Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)] -pub struct TimeSpan { +pub struct FiniteTimeSpan { pub years: usize, pub months: usize, pub weeks: usize, @@ -12,69 +20,69 @@ pub struct TimeSpan { pub nanoseconds: usize, } -impl TimeSpan { - pub fn with_years(&self, years: usize) -> TimeSpan { +impl FiniteTimeSpan { + pub fn with_years(&self, years: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.years = years; new } - pub fn with_months(&self, months: usize) -> TimeSpan { + pub fn with_months(&self, months: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.months = months; new } - pub fn with_weeks(&self, weeks: usize) -> TimeSpan { + pub fn with_weeks(&self, weeks: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.weeks = weeks; new } - pub fn with_days(&self, days: usize) -> TimeSpan { + pub fn with_days(&self, days: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.days = days; new } - pub fn with_hours(&self, hours: usize) -> TimeSpan { + pub fn with_hours(&self, hours: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.hours = hours; new } - pub fn with_minutes(&self, minutes: usize) -> TimeSpan { + pub fn with_minutes(&self, minutes: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.minutes = minutes; new } - pub fn with_seconds(&self, seconds: usize) -> TimeSpan { + pub fn with_seconds(&self, seconds: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.seconds = seconds; new } - pub fn with_milliseconds(&self, milliseconds: usize) -> TimeSpan { + pub fn with_milliseconds(&self, milliseconds: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.milliseconds = milliseconds; new } - pub fn with_microseconds(&self, microseconds: usize) -> TimeSpan { + pub fn with_microseconds(&self, microseconds: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.microseconds = microseconds; new } - pub fn with_nanoseconds(&self, nanoseconds: usize) -> TimeSpan { + pub fn with_nanoseconds(&self, nanoseconds: usize) -> FiniteTimeSpan { let mut new = self.clone(); new.nanoseconds = nanoseconds; new } - pub fn new() -> TimeSpan { - TimeSpan { + pub fn new() -> FiniteTimeSpan { + FiniteTimeSpan { years: 0, months: 0, weeks: 0, @@ -89,10 +97,17 @@ impl TimeSpan { } } - pub fn parse_time_span(val: &str) -> Option { + if val.trim() == "infinite" || val.trim() == "infinity" { + Some(Infinite) + } else { + parse_finite_time_span(val).map(|x| Finite(x)) + } +} + +pub fn parse_finite_time_span(val: &str) -> Option { let parse = val.replace(" ", ""); - let mut time_span = TimeSpan::default(); + let mut time_span = FiniteTimeSpan::default(); let mut current_value = String::new(); let mut current_num = 0usize; diff --git a/sysf/src/unit.rs b/sysf/src/unit.rs new file mode 100644 index 0000000..4fd4f8f --- /dev/null +++ b/sysf/src/unit.rs @@ -0,0 +1,128 @@ +use crate::unit::JobMode::Replace; +use crate::time::{TimeSpan, FiniteTimeSpan}; +use crate::time::TimeSpan::Infinite; + +#[allow(dead_code)] +pub struct Unit { + description: String, + + // Space only + documentation: Vec, + + // Space -and- list + wants: Vec, + requires: Vec, + requisite: Vec, + binds_to: Vec, + part_of: Vec, + + // Space only + conflicts: Vec, + + // Space -and- list + before: Vec, + after: Vec, + + // Space only + on_failure: Vec, + + // Space -and- list + propagates_reload_to: Vec, + propagates_reload_from: Vec, + joins_namespace_of: Vec, + requires_mounts_for: Vec, + + on_failure_job_mode: JobMode, + + ignore_on_isolate: bool, + stop_when_unneeded: bool, + refuse_manual_start: bool, + refuse_manual_stop: bool, + allow_isolate: bool, + default_dependencies: bool, + collect_mode: CollectMode, + failure_action: UnitAction, + success_action: UnitAction, + failure_action_exit_status: u8, + success_action_exit_status: u8, + job_timeout_sec: TimeSpan, + job_running_timeout_sec: TimeSpan, + job_timeout_action: UnitAction, + job_timeout_reboot_argument: Option, + start_limit_interval_sec: FiniteTimeSpan, + start_limit_burst: usize, + start_limit_action: UnitAction, + reboot_argument: Option, + source_path: Option, +} + +impl Default for Unit { + fn default() -> Self { + Unit { + description: "".to_string(), + documentation: vec![], + wants: vec![], + requires: vec![], + requisite: vec![], + binds_to: vec![], + part_of: vec![], + conflicts: vec![], + before: vec![], + after: vec![], + on_failure: vec![], + propagates_reload_to: vec![], + propagates_reload_from: vec![], + joins_namespace_of: vec![], + requires_mounts_for: vec![], + on_failure_job_mode: Replace, + ignore_on_isolate: false, + stop_when_unneeded: false, + refuse_manual_start: false, + refuse_manual_stop: false, + allow_isolate: false, + default_dependencies: true, + collect_mode: CollectMode::Inactive, + failure_action: UnitAction::None, + success_action: UnitAction::None, + failure_action_exit_status: 0, + success_action_exit_status: 0, + job_timeout_sec: Infinite, + job_running_timeout_sec: Infinite, + job_timeout_action: UnitAction::None, + job_timeout_reboot_argument: None, + start_limit_interval_sec: Default::default(), + start_limit_burst: 0, + start_limit_action: UnitAction::None, + reboot_argument: None, + source_path: None, + } + } +} + +enum JobMode { + Fail, + Replace, + ReplaceIrreversibly, + Isolate, + Flush, + IgnoreDependencies, + IgnoreRequirements, +} + +enum CollectMode { + Inactive, + InactiveOrFailed, +} + +enum UnitAction { + None, + Reboot(UnitActionSeverity), + PowerOff(UnitActionSeverity), + Exit(UnitActionSeverity), +} + +enum UnitActionSeverity { + None, + Force, + Immediate, +} \ No newline at end of file