-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instead of hard-coding the sample files, read settings from config.
- Loading branch information
Showing
10 changed files
with
155 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/config.toml | ||
|
||
/target/ | ||
**/*.rs.bk | ||
*.gnucash.*LCK | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,12 @@ version = "0.1.0" | |
authors = ["David Cain <[email protected]>"] | ||
|
||
[dependencies] | ||
quick-xml = "0.13.1" | ||
rust_decimal = ">= 0.11.2" | ||
rusqlite = "0.16.0" | ||
chrono = "0.4.6" | ||
csv = "1" | ||
num = "0.2.0" | ||
quick-xml = "0.13.1" | ||
rusqlite = "0.16.0" | ||
rust_decimal = ">= 0.11.2" | ||
serde = "1" | ||
serde_derive = "1" | ||
num = "0.2.0" | ||
toml = "0.5" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[user] | ||
birthday = '1972-07-12' | ||
|
||
[gnucash] | ||
path_to_book = 'my_books.gnucash' | ||
file_format = 'sqlite3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
extern crate chrono; | ||
extern crate serde_derive; | ||
|
||
use serde_derive::Deserialize; | ||
|
||
use self::chrono::NaiveDate; | ||
use std::fs; | ||
|
||
#[derive(Deserialize)] | ||
struct User { | ||
birthday: String, // YYYY-MM-DD | ||
} | ||
|
||
impl User { | ||
fn birthday(&self) -> NaiveDate { | ||
NaiveDate::parse_from_str(&self.birthday, "%Y-%m-%d").unwrap() | ||
} | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct GnuCash { | ||
pub path_to_book: String, | ||
pub file_format: String, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
pub struct Config { | ||
user: User, | ||
pub gnucash: GnuCash, | ||
} | ||
|
||
impl Config { | ||
/// Return default settings for use with the sample data | ||
pub fn default() -> Config { | ||
Config { | ||
user: User { | ||
birthday: String::from("1985-01-01"), | ||
}, | ||
gnucash: GnuCash { | ||
path_to_book: String::from("example.sqlite3"), | ||
file_format: String::from("sqlite3"), | ||
}, | ||
} | ||
} | ||
|
||
pub fn user_birthday(&self) -> NaiveDate { | ||
self.user.birthday() | ||
} | ||
|
||
/// Return a Config from file, or default settings if not present | ||
/// | ||
/// See `example_config.toml` for a sample configuration: | ||
/// | ||
/// ```toml | ||
/// [user] | ||
/// birthday = '1971-06-14' | ||
/// | ||
/// [gnucash] | ||
/// path_to_book = '/path/to/database.gnucash' | ||
/// file_format = 'sqlite3' | ||
/// ``` | ||
pub fn from_file(path: &str) -> Config { | ||
let config_toml = match fs::read_to_string(path) { | ||
Ok(file) => file, | ||
Err(_) => { | ||
// Silently fall back to the default | ||
return Config::default(); | ||
} | ||
}; | ||
|
||
toml::from_str(&config_toml).unwrap() | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_parses_birthday() { | ||
let user = User { | ||
birthday: String::from("1962-12-31"), | ||
}; | ||
assert_eq!(user.birthday(), NaiveDate::from_ymd(1962, 12, 31)); | ||
} | ||
|
||
#[test] | ||
fn test_parse_from_toml() { | ||
let conf = Config::from_file("example_config.toml"); | ||
assert_eq!(conf.user_birthday(), NaiveDate::from_ymd(1972, 7, 12)); | ||
assert_eq!(&conf.gnucash.path_to_book, "my_books.gnucash"); | ||
assert_eq!(&conf.gnucash.file_format, "sqlite3"); | ||
} | ||
|
||
#[test] | ||
fn test_fallback_to_default_settings() { | ||
let conf = Config::from_file("/tmp/definitely_does_not_exist.toml"); | ||
assert_eq!(&conf.user.birthday, "1985-01-01"); | ||
assert_eq!(&conf.gnucash.path_to_book, "example.sqlite3"); | ||
assert_eq!(&conf.gnucash.file_format, "sqlite3"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters