Signed-off-by: Maciek Borzecki <maciek.borzecki@gmail.com>bboozzoo/maybe-rust
@ -0,0 +1,7 @@ | |||||
[workspace] | |||||
members = [ | |||||
"mconnect", | |||||
"mconnectctl", | |||||
"kdeconnect", | |||||
] |
@ -0,0 +1,9 @@ | |||||
[package] | |||||
name = "kdeconnect" | |||||
version = "0.1.0" | |||||
authors = ["Maciek Borzecki <maciek.borzecki@gmail.com>"] | |||||
edition = "2018" | |||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |||||
[dependencies] |
@ -0,0 +1,11 @@ | |||||
#[cfg(test)] | |||||
mod tests { | |||||
#[test] | |||||
fn it_works() { | |||||
assert_eq!(2 + 2, 4); | |||||
} | |||||
} | |||||
pub fn bang() { | |||||
println!("bang!"); | |||||
} |
@ -0,0 +1,20 @@ | |||||
[package] | |||||
name = "mconnect" | |||||
version = "0.1.0" | |||||
authors = ["Maciek Borzecki <maciek.borzecki@gmail.com>"] | |||||
edition = "2018" | |||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |||||
[dependencies] | |||||
kdeconnect = { path = "../kdeconnect", version = "" } | |||||
log = "0.4" | |||||
tempfile = "3.1.0" | |||||
[dependencies.glib] | |||||
version = "" | |||||
features = ["v2_44"] | |||||
[dependencies.gio] | |||||
version = "" | |||||
features = ["v2_44"] |
@ -0,0 +1,33 @@ | |||||
use std::path::PathBuf; | |||||
use glib; | |||||
use super::config; | |||||
extern crate kdeconnect; | |||||
fn user_config_path() -> Result<PathBuf, String> { | |||||
let user_config_dir = match glib::get_user_config_dir() { | |||||
Some(v) => v, | |||||
None => return Err(String::from("cannot obtain user config directory")) | |||||
}; | |||||
let mut config_path = PathBuf::new(); | |||||
config_path.push(user_config_dir); | |||||
config_path.push(config::NAME); | |||||
println!("user config dir: {}", config_path.as_path().display()); | |||||
Ok(config_path) | |||||
} | |||||
pub fn run() -> Result<(), String> { | |||||
let config_path = match user_config_path() { | |||||
Err(x) => return Err(format!("cannot obtain user config path: {}", x)), | |||||
Ok(p) => p | |||||
}; | |||||
let c = config::load_from_path(config_path); | |||||
kdeconnect::bang(); | |||||
Ok(()) | |||||
} |
@ -0,0 +1,55 @@ | |||||
use std::io; | |||||
use std::path::Path; | |||||
use glib; | |||||
pub const NAME: &'static str = "mconnect.conf"; | |||||
pub struct Config { | |||||
kf: glib::KeyFile, | |||||
} | |||||
pub fn load_from_path<T: AsRef<Path>>(path: T) -> Result<Config, String> { | |||||
let kf = glib::KeyFile::new(); | |||||
let c = Config { kf: kf }; | |||||
if let Err(err) = c.kf.load_from_file(path, glib::KeyFileFlags::KEEP_COMMENTS) { | |||||
match err.kind::<glib::FileError>() { | |||||
Some(glib::FileError::Noent) => { | |||||
println!("user config not found, using defaults"); | |||||
Ok(c) | |||||
} | |||||
_ => Err(err.to_string()), | |||||
} | |||||
} else { | |||||
Ok(c) | |||||
} | |||||
} | |||||
#[cfg(test)] | |||||
mod tests { | |||||
use std::io::Write; | |||||
use tempfile::NamedTempFile; | |||||
#[test] | |||||
fn simple_load() { | |||||
let mut tmpf = NamedTempFile::new().expect("cannot create a temp file"); | |||||
writeln!(tmpf, "").expect("write failed"); | |||||
let res = super::load_from_path(tmpf.path()); | |||||
assert!(res.is_ok()); | |||||
} | |||||
#[test] | |||||
fn not_found_not_error() { | |||||
let res = super::load_from_path("does-not-exist"); | |||||
assert!(res.is_ok()); | |||||
} | |||||
#[test] | |||||
fn bad_format() { | |||||
let mut tmp = NamedTempFile::new().expect("cannot create a temp file"); | |||||
writeln!(tmp, "bad-format").expect("write failed"); | |||||
let res = super::load_from_path(tmp.path()); | |||||
assert!(res.is_err()); | |||||
} | |||||
} |
@ -0,0 +1,12 @@ | |||||
mod app; | |||||
mod config; | |||||
fn main() { | |||||
std::process::exit(match app::run() { | |||||
Ok(_) => 0, | |||||
Err(err) => { | |||||
eprintln!("error: {}", err); | |||||
1 | |||||
} | |||||
}); | |||||
} |
@ -0,0 +1,9 @@ | |||||
[package] | |||||
name = "mconnectctl" | |||||
version = "0.1.0" | |||||
authors = ["Maciek Borzecki <maciek.borzecki@gmail.com>"] | |||||
edition = "2018" | |||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | |||||
[dependencies] |
@ -0,0 +1,3 @@ | |||||
fn main() { | |||||
println!("Hello, world!"); | |||||
} |