From edb42b4f227319f712d107a63a75cddd41839429 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 25 Mar 2020 20:20:23 +0530 Subject: ignore nested `target`s --- .gitignore | 2 +- fondant/Cargo.toml | 9 ++++++ fondant/src/lib.rs | 86 +++++++++++++++++++++++++++++++++++++++++++++++++ fondant_deps/Cargo.toml | 19 +++++++++++ fondant_deps/src/lib.rs | 47 +++++++++++++++++++++++++++ 5 files changed, 162 insertions(+), 1 deletion(-) create mode 100644 fondant/Cargo.toml create mode 100644 fondant/src/lib.rs create mode 100644 fondant_deps/Cargo.toml create mode 100644 fondant_deps/src/lib.rs diff --git a/.gitignore b/.gitignore index 088ba6b..83c440c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,6 @@ # Generated by Cargo # will have compiled files and executables -/target/ +**/target/** # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html diff --git a/fondant/Cargo.toml b/fondant/Cargo.toml new file mode 100644 index 0000000..d6886b1 --- /dev/null +++ b/fondant/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "fondant" +version = "0.1.0" +authors = ["Akshay "] +edition = "2018" + +[dependencies] +fondant_derive = {version = "0.1.0", path = "../fondant_derive"} +fondant_deps = {version = "0.1.0", path = "../fondant_deps"} diff --git a/fondant/src/lib.rs b/fondant/src/lib.rs new file mode 100644 index 0000000..d5758dc --- /dev/null +++ b/fondant/src/lib.rs @@ -0,0 +1,86 @@ +//! `fondant` is a macro based library to take the boilerplate out of +//! configuration handling. Most of `fondant` is based off the `confy` crate. +//! +//! `fondant` adds a couple of extra features: +//! +//! - support for json, yaml and toml +//! - support for custom config paths +//! - support for custom config file names +//! +//! ### Sample usage +//! +//! ``` +//! // the struct has to derive Serialize, Deserialize and Default +//! #[derive(Configure, Serialize, Deserialize, Default)] +//! #[config_file = "config.toml"] +//! // | +//! // `-- this attribute sets the file name to "config.toml" +//! // `-- the file format to "toml" +//! // `-- the file path to "default" (read the notes below) +//! struct AppConfig { +//! version: u32, +//! port: u32, +//! username: String, +//! } +//! +//! fn main() { +//! // use `load` (associated method) to load the config file +//! let mut conf = AppConfig::load().unwrap(); +//! +//! // do stuff with conf +//! conf.version = 2; +//! +//! // call `store` to save changes +//! conf.store().unwrap(); +//! } +//! ``` +//! **Notes**: +//! - `load` returns `Default::default` if the config file is not present, and stores +//! a serialized `Default::default` at the specified path +//! - the "default" config path varies by platform: +//! * GNU/Linux: `$XDG_CONFIG_HOME/my_cool_crate/config.toml` (follows xdg spec) +//! * MacOS: `$HOME/Library/Preferences/my_cool_crate/config.toml` +//! * Windows: `{FOLDERID_RoamingAppData}\_project_path_\config` +//! +//! ### Customization +//! +//! Set your own filename, for ex.: `apprc` +//! +//! ``` +//! #[derive(Configure, Serialize, Deserialize, Default)] +//! #[config_file = "apprc.toml"] +//! struct AppConfig { +//! // -- snip -- +//! } +//! // effective path: $XDG_CONFIG_HOME/my_cool_crate/apprc.toml +//! // effective format: toml +//! ``` +//! +//! Change file format to `yaml`, by changing the file extension. +//! Supported extensions are `yaml`, `toml`, `json`: +//! ``` +//! #[derive(Configure, Serialize, Deserialize, Default)] +//! #[config_file = "config.yaml"] +//! struct AppConfig { +//! // -- snip -- +//! } +//! // effective path: $XDG_CONFIG_HOME/my_cool_crate/config.yaml +//! // effective format: yaml +//! ``` +//! +//! Override the default config path, for ex.: the home directory +//! (it is not recommended to override config path): +//! ``` +//! #[derive(Configure, Serialize, Deserialize, Default)] +//! #[config_file = "~/.apprc.json"] +//! struct AppConfig { +//! // -- snip -- +//! } +//! // effective path: $HOME/.apprc.json +//! // effective format: json +//! ``` + +pub use fondant_deps::fondant_exports; +pub use fondant_deps::Configure; +pub use fondant_deps::FondantError; +pub use fondant_derive::Configure; diff --git a/fondant_deps/Cargo.toml b/fondant_deps/Cargo.toml new file mode 100644 index 0000000..2d1a2db --- /dev/null +++ b/fondant_deps/Cargo.toml @@ -0,0 +1,19 @@ +[package] +name = "fondant_deps" +version = "0.1.0" +authors = ["Akshay "] +edition = "2018" + +[dependencies] +toml = "^0.5" +serde_yaml = "0.8" +serde_json = "1.0.48" +directories = "2.0" + +[dependencies.serde] +version = "1.0.103" +features = ["derive"] + +[dependencies.syn] +version = "1.0" +features = ["full"] diff --git a/fondant_deps/src/lib.rs b/fondant_deps/src/lib.rs new file mode 100644 index 0000000..6f62ce8 --- /dev/null +++ b/fondant_deps/src/lib.rs @@ -0,0 +1,47 @@ +pub mod fondant_exports { + pub use directories::{ProjectDirs, UserDirs}; + pub use serde::{de::DeserializeOwned, Serialize}; + pub use serde_json; + pub use serde_yaml; + use std::path::{Path, PathBuf}; + pub use toml; + pub fn expand_tilde>(path: P) -> PathBuf { + let p = path.as_ref(); + if p.starts_with("~") { + if p == Path::new("~") { + return UserDirs::new().unwrap().home_dir().to_path_buf(); + } else { + let mut h = UserDirs::new().unwrap().home_dir().to_path_buf(); + h.push(p.strip_prefix("~/").unwrap()); + return h; + } + } + return p.to_path_buf(); + } +} + +use serde::{de::DeserializeOwned, Serialize}; +#[derive(Debug)] +/// Errors that `load` and `store` can result in +pub enum FondantError { + /// Occurs when the home dir is not accessible. + /// You should probably `panic!` when this is thrown. + InvalidHomeDir, + + /// Invalid toml/yaml/json config. + ConfigParseError, + + /// Invalid permissions to create config dir. + /// Might occur when you set config dir to, say, `/etc/config.toml` and run without superuser. + DirCreateErr(std::io::Error), + LoadError, + FileWriteError, + FileReadError, + FileOpenError, +} + +/// Derive this trait on a struct to mark it as a 'configuration' struct. +pub trait Configure: Serialize + DeserializeOwned + Default { + fn load() -> Result; + fn store(&self) -> Result<(), FondantError>; +} -- cgit v1.2.3