Add configuration + state factory
Getty Ritter
6 years ago
| 1 |
extern crate clap;
|
| 2 |
|
| 3 |
use self::clap;
|
| 4 |
use constants;
|
| 5 |
|
| 6 |
pub struct Cfg {
|
| 7 |
file_path: Option<&'static str>,
|
| 8 |
}
|
| 9 |
|
| 10 |
fn palladio_args() -> Cfg {
|
| 11 |
let matches =
|
| 12 |
clap::App::new(constants::PROGRAM_NAME)
|
| 13 |
.version(constants::VERSION)
|
| 14 |
.author(constants::AUTHOR)
|
| 15 |
|
| 16 |
.arg(clap::Arg::with_name("i")
|
| 17 |
.short("i")
|
| 18 |
.long("input")
|
| 19 |
.value_name("FILE")
|
| 20 |
.help("The document to load on starting"))
|
| 21 |
.get_matches();
|
| 22 |
|
| 23 |
let file_path = matches.value_of("input");
|
| 24 |
Cfg { file_path }
|
| 25 |
}
|
1 | 1 |
pub mod document;
|
2 | 2 |
pub use self::document::*;
|
| 3 |
use failure::Error;
|
| 4 |
use cfg;
|
3 | 5 |
|
4 | 6 |
pub struct State {
|
5 | 7 |
pub document: Document,
|
6 | 8 |
pub save_state: SaveState,
|
| 9 |
}
|
| 10 |
|
| 11 |
impl State {
|
| 12 |
fn new(conf: cfg::Cfg) -> Result<State, Error> {
|
| 13 |
match conf.file_path {
|
| 14 |
None => State {
|
| 15 |
document: document.default(),
|
| 16 |
save_state = SaveState::Unsaved,
|
| 17 |
}
|
| 18 |
Some(path) => {
|
| 19 |
let mut f = File::open(path)?;
|
| 20 |
State {
|
| 21 |
document: Document::open_from_file(&mut f)?;
|
| 22 |
save_state: SaveState::Saved,
|
| 23 |
}
|
| 24 |
}
|
| 25 |
}
|
| 26 |
}
|
7 | 27 |
}
|
8 | 28 |
|
9 | 29 |
#[derive(Debug)]
|