From 9809d59ab8ccc2fee5b2ca66df5cd0545cd08bd3 Mon Sep 17 00:00:00 2001 From: eater <=@eater.me> Date: Wed, 25 Dec 2019 18:41:18 +0100 Subject: [PATCH] Restructure project a bit --- Cargo.lock | 9 +- Cargo.toml | 14 +- libsysf/Cargo.lock | 6 + libsysf/Cargo.toml | 5 + {src => libsysf/src}/config/mod.rs | 121 ++++------------- {src => libsysf/src}/config/parser.rs | 106 ++------------- libsysf/src/lib.rs | 3 + libsysf/src/time.rs | 180 ++++++++++++++++++++++++++ libsysf/src/unit.rs | 13 ++ src/main.rs | 6 - sysf-init/Cargo.toml | 8 ++ sysf-init/src/main.rs | 6 + 12 files changed, 264 insertions(+), 213 deletions(-) create mode 100644 libsysf/Cargo.lock create mode 100644 libsysf/Cargo.toml rename {src => libsysf/src}/config/mod.rs (50%) rename {src => libsysf/src}/config/parser.rs (66%) create mode 100644 libsysf/src/lib.rs create mode 100644 libsysf/src/time.rs create mode 100644 libsysf/src/unit.rs delete mode 100644 src/main.rs create mode 100644 sysf-init/Cargo.toml create mode 100644 sysf-init/src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 9a15ea2..b87cb7b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,13 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. [[package]] -name = "systemf" +name = "libsysf" version = "0.1.0" +[[package]] +name = "sysf-init" +version = "0.1.0" +dependencies = [ + "libsysf 0.1.0", +] + diff --git a/Cargo.toml b/Cargo.toml index ea14742..5ff5625 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,5 @@ -[package] -name = "systemf" -version = "0.1.0" -authors = ["eater <=@eater.me>"] -edition = "2018" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] +[workspace] +members = [ + "libsysf", + "sysf-init" +] diff --git a/libsysf/Cargo.lock b/libsysf/Cargo.lock new file mode 100644 index 0000000..9a15ea2 --- /dev/null +++ b/libsysf/Cargo.lock @@ -0,0 +1,6 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +[[package]] +name = "systemf" +version = "0.1.0" + diff --git a/libsysf/Cargo.toml b/libsysf/Cargo.toml new file mode 100644 index 0000000..02474fd --- /dev/null +++ b/libsysf/Cargo.toml @@ -0,0 +1,5 @@ +[package] +name = "libsysf" +version = "0.1.0" +authors = ["eater <=@eater.me>"] +edition = "2018" \ No newline at end of file diff --git a/src/config/mod.rs b/libsysf/src/config/mod.rs similarity index 50% rename from src/config/mod.rs rename to libsysf/src/config/mod.rs index 5154829..19bd018 100644 --- a/src/config/mod.rs +++ b/libsysf/src/config/mod.rs @@ -1,10 +1,11 @@ use std::collections::HashMap; -use crate::config::parser::parse_time_span; +use crate::config::parser::{ParserError, parse_config}; +use crate::time::{parse_time_span, TimeSpan}; pub mod parser; #[derive(Clone, Debug, Default)] -struct Config { +pub struct Config { name: String, file_pointer: usize, files: HashMap, @@ -19,11 +20,24 @@ impl Config { id } + + pub fn load_file(&mut self, file_name: String, file_contents: String) -> Result<(), ParserError> { + parse_config(self, file_name, file_contents) + } + + pub fn new(name: &str) -> Config { + Config { + name: name.to_string(), + file_pointer: 0, + files: Default::default(), + sections: Default::default(), + } + } } #[derive(Clone, Debug, Default)] -struct Section { +pub struct Section { name: String, entries: HashMap, } @@ -40,25 +54,25 @@ impl Section { self.entries.get(name) } - fn get_string(&self, name: &str) -> Option<&str> { + pub fn get_string(&self, name: &str) -> Option<&str> { self.entries.get(name).map(|x| x.get_string()) } - fn get_boolean(&self, name: &str) -> Option { + pub fn get_boolean(&self, name: &str) -> Option { self.entries.get(name).and_then(|x| x.get_boolean()) } - fn get_list(&self, name: &str) -> Option> { + pub fn get_list(&self, name: &str) -> Option> { self.entries.get(name).map(|x| x.get_list()) } - fn get_time_span(&self, name: &str) -> Option { + pub fn get_time_span(&self, name: &str) -> Option { self.entries.get(name).and_then(|x| x.get_time_span()) } } #[derive(Clone, Debug, Default)] -struct Entry { +pub struct Entry { name: String, values: Vec, } @@ -118,97 +132,6 @@ impl Entry { } } -#[derive(Default, Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)] -pub struct TimeSpan { - pub years: usize, - pub months: usize, - pub weeks: usize, - pub days: usize, - pub hours: usize, - pub minutes: usize, - pub seconds: usize, - pub milliseconds: usize, - pub microseconds: usize, - pub nanoseconds: usize, -} - -impl TimeSpan { - pub fn with_years(&self, years: usize) -> TimeSpan { - let mut new = self.clone(); - new.years = years; - new - } - - pub fn with_months(&self, months: usize) -> TimeSpan { - let mut new = self.clone(); - new.months = months; - new - } - - pub fn with_weeks(&self, weeks: usize) -> TimeSpan { - let mut new = self.clone(); - new.weeks = weeks; - new - } - - pub fn with_days(&self, days: usize) -> TimeSpan { - let mut new = self.clone(); - new.days = days; - new - } - - pub fn with_hours(&self, hours: usize) -> TimeSpan { - let mut new = self.clone(); - new.hours = hours; - new - } - - pub fn with_minutes(&self, minutes: usize) -> TimeSpan { - let mut new = self.clone(); - new.minutes = minutes; - new - } - - pub fn with_seconds(&self, seconds: usize) -> TimeSpan { - let mut new = self.clone(); - new.seconds = seconds; - new - } - - pub fn with_milliseconds(&self, milliseconds: usize) -> TimeSpan { - let mut new = self.clone(); - new.milliseconds = milliseconds; - new - } - - pub fn with_microseconds(&self, microseconds: usize) -> TimeSpan { - let mut new = self.clone(); - new.microseconds = microseconds; - new - } - - pub fn with_nanoseconds(&self, nanoseconds: usize) -> TimeSpan { - let mut new = self.clone(); - new.nanoseconds = nanoseconds; - new - } - - pub fn new() -> TimeSpan { - TimeSpan { - years: 0, - months: 0, - weeks: 0, - days: 0, - hours: 0, - minutes: 0, - seconds: 0, - milliseconds: 0, - microseconds: 0, - nanoseconds: 0, - } - } -} - #[derive(Clone, Debug, Default)] struct EntryValue { value: String, diff --git a/src/config/parser.rs b/libsysf/src/config/parser.rs similarity index 66% rename from src/config/parser.rs rename to libsysf/src/config/parser.rs index a29218a..f6ce312 100644 --- a/src/config/parser.rs +++ b/libsysf/src/config/parser.rs @@ -1,113 +1,23 @@ -use crate::config::{Config, Section, Entry, TimeSpan}; +use crate::config::{Config, Section, Entry}; use std::error::Error; -use std::fmt::{Display, Formatter, Write}; -use std::borrow::{Borrow, BorrowMut}; +use std::fmt::{Display, Formatter}; #[derive(Debug)] -struct ParserError { - filename: String, - line_no: usize, - description: String, +pub struct ParserError { + pub filename: String, + pub line_no: usize, + pub description: String, } impl Error for ParserError {} impl Display for ParserError { fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> { - f.write_str(&format!("Config didn't start with section header in file {} at line {}", self.filename, self.line_no)) + write!(f, "Failed to parse Unit config: {} in file {} at line {}", self.description, self.filename, self.line_no) } } -pub fn parse_time_span(val: &str) -> Option { - let parse = val.replace(" ", ""); - let mut time_span = TimeSpan::default(); - let mut current_value = String::new(); - let mut current_num = 0usize; - - let finish = &mut |name: &str, amount: usize| { - match name { - "nsec" | "ns" => { - time_span.nanoseconds += amount; - } - - "usec" | "us" | "µs" => { - time_span.microseconds += amount; - } - - "msec" | "ms" => { - time_span.milliseconds += amount; - } - - "" | "s" | "sec" | "second" | "seconds" => { - time_span.seconds += amount; - } - - "minutes" | "minute" | "min" | "m" => { - time_span.minutes += amount; - } - - "hours" | "hour" | "hr" | "h" => { - time_span.hours += amount; - } - - "days" | "day" | "d" => { - time_span.days += amount; - } - - "weeks" | "week" | "w" => { - time_span.weeks += amount; - } - - "M" | "months" | "month" => { - time_span.months += amount; - } - - "years" | "year" | "y" => { - time_span.years += amount; - } - - _ => return false - } - - true - }; - - let mut last_is_nr = false; - for chxr in parse.chars() { - match chxr.to_ascii_lowercase() { - '0'..='9' => { - if !last_is_nr { - if !finish(current_value.as_str(), current_num) { - return None; - } - current_value = String::new(); - current_num = 0; - last_is_nr = true - } - - current_num *= 10; - current_num += chxr.to_digit(10).unwrap_or(0) as usize - } - - 'a'..='z' => { - current_value += &chxr.to_string(); - last_is_nr = false; - } - - _ => { - return None; - } - } - } - - if !finish(current_value.as_str(), current_num) { - None - } else { - Some(time_span) - } -} - -fn parse_config(config: &mut Config, filename: String, contents: String) -> Result<(), ParserError> { +pub(crate) fn parse_config(config: &mut Config, filename: String, contents: String) -> Result<(), ParserError> { let file_id = config.add_file(filename.clone()); let mut current_entry: Option<(&str, String)> = None; let mut current_section: Option<&mut Section> = None; diff --git a/libsysf/src/lib.rs b/libsysf/src/lib.rs new file mode 100644 index 0000000..16465d2 --- /dev/null +++ b/libsysf/src/lib.rs @@ -0,0 +1,3 @@ +pub mod config; +pub mod unit; +pub mod time; diff --git a/libsysf/src/time.rs b/libsysf/src/time.rs new file mode 100644 index 0000000..aa7c9b5 --- /dev/null +++ b/libsysf/src/time.rs @@ -0,0 +1,180 @@ +#[derive(Default, Debug, Copy, Clone, Hash, Ord, PartialOrd, Eq, PartialEq)] +pub struct TimeSpan { + pub years: usize, + pub months: usize, + pub weeks: usize, + pub days: usize, + pub hours: usize, + pub minutes: usize, + pub seconds: usize, + pub milliseconds: usize, + pub microseconds: usize, + pub nanoseconds: usize, +} + +impl TimeSpan { + pub fn with_years(&self, years: usize) -> TimeSpan { + let mut new = self.clone(); + new.years = years; + new + } + + pub fn with_months(&self, months: usize) -> TimeSpan { + let mut new = self.clone(); + new.months = months; + new + } + + pub fn with_weeks(&self, weeks: usize) -> TimeSpan { + let mut new = self.clone(); + new.weeks = weeks; + new + } + + pub fn with_days(&self, days: usize) -> TimeSpan { + let mut new = self.clone(); + new.days = days; + new + } + + pub fn with_hours(&self, hours: usize) -> TimeSpan { + let mut new = self.clone(); + new.hours = hours; + new + } + + pub fn with_minutes(&self, minutes: usize) -> TimeSpan { + let mut new = self.clone(); + new.minutes = minutes; + new + } + + pub fn with_seconds(&self, seconds: usize) -> TimeSpan { + let mut new = self.clone(); + new.seconds = seconds; + new + } + + pub fn with_milliseconds(&self, milliseconds: usize) -> TimeSpan { + let mut new = self.clone(); + new.milliseconds = milliseconds; + new + } + + pub fn with_microseconds(&self, microseconds: usize) -> TimeSpan { + let mut new = self.clone(); + new.microseconds = microseconds; + new + } + + pub fn with_nanoseconds(&self, nanoseconds: usize) -> TimeSpan { + let mut new = self.clone(); + new.nanoseconds = nanoseconds; + new + } + + pub fn new() -> TimeSpan { + TimeSpan { + years: 0, + months: 0, + weeks: 0, + days: 0, + hours: 0, + minutes: 0, + seconds: 0, + milliseconds: 0, + microseconds: 0, + nanoseconds: 0, + } + } +} + + +pub fn parse_time_span(val: &str) -> Option { + let parse = val.replace(" ", ""); + let mut time_span = TimeSpan::default(); + let mut current_value = String::new(); + let mut current_num = 0usize; + + let finish = &mut |name: &str, amount: usize| { + match name { + "nsec" | "ns" => { + time_span.nanoseconds += amount; + } + + "usec" | "us" | "µs" => { + time_span.microseconds += amount; + } + + "msec" | "ms" => { + time_span.milliseconds += amount; + } + + "" | "s" | "sec" | "second" | "seconds" => { + time_span.seconds += amount; + } + + "minutes" | "minute" | "min" | "m" => { + time_span.minutes += amount; + } + + "hours" | "hour" | "hr" | "h" => { + time_span.hours += amount; + } + + "days" | "day" | "d" => { + time_span.days += amount; + } + + "weeks" | "week" | "w" => { + time_span.weeks += amount; + } + + "M" | "months" | "month" => { + time_span.months += amount; + } + + "years" | "year" | "y" => { + time_span.years += amount; + } + + _ => return false + } + + true + }; + + let mut last_is_nr = false; + for chxr in parse.chars() { + match chxr.to_ascii_lowercase() { + '0'..='9' => { + if !last_is_nr { + if !finish(current_value.as_str(), current_num) { + return None; + } + current_value = String::new(); + current_num = 0; + last_is_nr = true + } + + current_num *= 10; + current_num += chxr.to_digit(10).unwrap_or(0) as usize + } + + 'a'..='z' => { + current_value += &chxr.to_string(); + last_is_nr = false; + } + + _ => { + return None; + } + } + } + + if !finish(current_value.as_str(), current_num) { + None + } else { + Some(time_span) + } +} diff --git a/libsysf/src/unit.rs b/libsysf/src/unit.rs new file mode 100644 index 0000000..770f6b6 --- /dev/null +++ b/libsysf/src/unit.rs @@ -0,0 +1,13 @@ +#[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/src/main.rs b/src/main.rs deleted file mode 100644 index b5d1bf8..0000000 --- a/src/main.rs +++ /dev/null @@ -1,6 +0,0 @@ -pub mod config; - - -fn main() { - println!("Hello, world!"); -} diff --git a/sysf-init/Cargo.toml b/sysf-init/Cargo.toml new file mode 100644 index 0000000..1d34ec2 --- /dev/null +++ b/sysf-init/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "sysf-init" +version = "0.1.0" +authors = ["eater <=@eater.me>"] +edition = "2018" + +[dependencies] +libsysf = { path = "../libsysf" } \ No newline at end of file diff --git a/sysf-init/src/main.rs b/sysf-init/src/main.rs new file mode 100644 index 0000000..9b7e29c --- /dev/null +++ b/sysf-init/src/main.rs @@ -0,0 +1,6 @@ +extern crate libsysf; + + +fn main() { + +}