Restructure project a bit
continuous-integration/drone/push Build is passing Details

master
eater 4 years ago
parent 85643dd9ab
commit 9809d59ab8
Signed by: eater
GPG Key ID: AD2560A0F84F0759

9
Cargo.lock generated

@ -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",
]

@ -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"
]

6
libsysf/Cargo.lock generated

@ -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"

@ -0,0 +1,5 @@
[package]
name = "libsysf"
version = "0.1.0"
authors = ["eater <=@eater.me>"]
edition = "2018"

@ -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<usize, String>,
@ -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<String, Entry>,
}
@ -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<bool> {
pub fn get_boolean(&self, name: &str) -> Option<bool> {
self.entries.get(name).and_then(|x| x.get_boolean())
}
fn get_list(&self, name: &str) -> Option<Vec<&str>> {
pub fn get_list(&self, name: &str) -> Option<Vec<&str>> {
self.entries.get(name).map(|x| x.get_list())
}
fn get_time_span(&self, name: &str) -> Option<TimeSpan> {
pub fn get_time_span(&self, name: &str) -> Option<TimeSpan> {
self.entries.get(name).and_then(|x| x.get_time_span())
}
}
#[derive(Clone, Debug, Default)]
struct Entry {
pub struct Entry {
name: String,
values: Vec<EntryValue>,
}
@ -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,

@ -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<TimeSpan> {
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;

@ -0,0 +1,3 @@
pub mod config;
pub mod unit;
pub mod time;

@ -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<TimeSpan> {
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)
}
}

@ -0,0 +1,13 @@
#[allow(dead_code)]
pub struct Unit {
description: String,
documentation: Vec<String>,
wants: Vec<String>,
requires: Vec<String>,
requisite: Vec<String>,
binds_to: Vec<String>,
part_of: Vec<String>,
conflicts: Vec<String>,
before: Vec<String>,
after: Vec<String>,
}

@ -1,6 +0,0 @@
pub mod config;
fn main() {
println!("Hello, world!");
}

@ -0,0 +1,8 @@
[package]
name = "sysf-init"
version = "0.1.0"
authors = ["eater <=@eater.me>"]
edition = "2018"
[dependencies]
libsysf = { path = "../libsysf" }

@ -0,0 +1,6 @@
extern crate libsysf;
fn main() {
}
Loading…
Cancel
Save