Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 58 additions & 60 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ name = "admiral"
version = "1.1.0"

[dependencies]
clap = "2.29.2"
serde = "1.0.27"
serde_derive = "1.0.27"
clap = "2.33.0"
serde = "1.0.101"
serde_derive = "1.0.101"
threadpool = "1.7.1"
toml = "0.4.5"
chan = "0.1.20"
chan-signal = "0.3.1"
toml = "0.5.3"
chan = "0.1.23"
chan-signal = "0.3.3"
104 changes: 44 additions & 60 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,60 +1,41 @@
extern crate serde;
use config::serde::de::{Deserialize, Deserializer};
use config::serde::de::Error;
use config::serde::de::{Deserialize, Deserializer};

use toml::Value;
use toml::value::Table;
use toml::Value;

use std::path::PathBuf;
use std::env;
use std::path::PathBuf;

#[derive(Debug, Serialize)]
pub struct ConfigFile {
pub scripts: Vec<Script>,
}

impl<'de> Deserialize<'de> for ConfigFile {
fn deserialize<D>(d: D) -> Result<ConfigFile, D::Error> where D: Deserializer<'de> {
fn deserialize<D>(d: D) -> Result<ConfigFile, D::Error>
where
D: Deserializer<'de>,
{
let value = Value::deserialize(d)?;
match Value::try_into::<Table>(value) {
Ok(mut table) => {
match table.remove("admiral") {
Some(admiral) => {
match Value::try_into::<ItemList>(admiral) {
Ok(admiral) => {
let mut scripts = Vec::new();
for item in admiral.items {
if let Some(v) = table.get(&item) {
match Value::try_into::<Script>(v.to_owned()) {
Ok(s) => {
scripts.push(s);
},
Err(e) => {
return Err(Error::custom(e.to_string()));
},
}
}
}
Ok(
ConfigFile {
scripts: scripts,
}
)
},
Err(e) => {
Err(Error::custom(e.to_string()))
},
}
},
None => {
Err(Error::custom("missing \"admiral\" section".to_string()))
},
}
},
Err(e) => {
Err(Error::custom(e.to_string()))
},
let mut table =
Value::try_into::<Table>(value).map_err(|e| Error::custom(e.to_string()))?;
let admiral = table
.remove("admiral")
.ok_or(Error::custom("missing \"admiral\" section".to_string()))?;
let admiral =
Value::try_into::<ItemList>(admiral).map_err(|e| Error::custom(e.to_string()))?;

let mut scripts = Vec::new();
for item in admiral.items {
if let Some(v) = table.get(&item) {
let s = Value::try_into::<Script>(v.to_owned())
.map_err(|e| Error::custom(e.to_string()))?;
scripts.push(s);
}
}
Ok(ConfigFile { scripts })
}
}

Expand All @@ -74,34 +55,37 @@ pub struct Script {

impl Script {
pub fn shell(&self) -> PathBuf {
match self.shell {
Some(ref shell) => PathBuf::from(&shell),
None => {
match env::var_os("SHELL") {
Some(sh) => {
PathBuf::from(&sh)
},
None => {
PathBuf::from("/bin/sh")
}
}
}
if let Some(ref shell) = self.shell {
return PathBuf::from(&shell);
}

PathBuf::from(env::var("SHELL").unwrap_or("/bin/sh".into()))
}
}

#[allow(dead_code)]
pub fn get_config_file() -> Option<PathBuf> {
let xdg_path = env::var("XDG_CONFIG_HOME").ok()
let xdg_path = env::var("XDG_CONFIG_HOME")
.ok()
.map(|v| PathBuf::from(v).join("admiral.d").join("admiral.toml"))
.and_then(if_readable);

let dot_home = env::var("HOME").ok()
.map(|v| PathBuf::from(v).join(".config").join("admiral.d").join("admiral.toml"))
let dot_home = env::var("HOME")
.ok()
.map(|v| {
PathBuf::from(v)
.join(".config")
.join("admiral.d")
.join("admiral.toml")
})
.and_then(if_readable);

xdg_path.or(dot_home)
}

#[allow(dead_code)]
fn if_readable(path: PathBuf) -> Option<PathBuf> { if path.exists() { Some(path) } else { None } }
fn if_readable(path: PathBuf) -> Option<PathBuf> {
if path.exists() {
Some(path)
} else {
None
}
}
Loading