1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//! Main functions doing actual work.
//!
//! Each module contains the functions for their respective subsystems.


use toml::{Parser, Value, decode};
use rustc_serialize::Decodable;
use std::path::{PathBuf, Path};
use self::super::Outcome;
use std::fs::File;
use std::io::Read;

mod user;
mod token;
mod queued_tweet;

pub mod init;
pub mod add_user;
pub mod queue_tweet;
pub mod start_daemon;

pub use self::user::User;
pub use self::token::AppTokens;
pub use self::queued_tweet::QueuedTweet;


fn verify_file(fname: &str, should_exist: bool, config_dir: &(String, PathBuf), force: bool, producing_subsystem: &'static str) -> Result<PathBuf, Outcome> {
    let app_data_file = config_dir.1.join(fname);

    if force || app_data_file.exists() == should_exist {
        Ok(app_data_file)
    } else {
        let filename = PathBuf::from(&config_dir.0).join(fname).to_str().unwrap().replace("\\", "/");

        if should_exist {
            Err(Outcome::RequiredFileFromSubsystemNonexistant {
                subsys: producing_subsystem,
                fname: filename,
            })
        } else {
            Err(Outcome::OverrideNoForce(filename))
        }
    }
}

fn read_toml_file<T: Decodable>(p: &Path, desc: &'static str) -> Result<T, Option<Outcome>> {
    let mut buf = String::new();
    try!(try!(File::open(p).map_err(|_| None)).read_to_string(&mut buf).map_err(|_| None));

    let mut parser = Parser::new(&buf);
    let parsed = parser.parse().and_then(|t| decode(Value::Table(t)));
    parsed.ok_or_else(move || {
        Some(Outcome::FileParsingFailed {
            desc: desc,
            errors: parser.errors
                .iter()
                .map(|e| {
                    let (line, col) = parser.to_linecol(e.lo);
                    format!("error: {}:{}: {}", line, col, e.desc)
                })
                .collect(),
        })
    })
}