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
65
66
67
68
69
70
71
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate diesel;

#[macro_use]
extern crate serde_derive;

extern crate byteorder;

mod db;
mod form;
mod in_;
mod mn;
pub mod point;
pub mod sql_types;
mod user_status;
mod utils;

pub use db::{connection, rollback_if_required, DIESEL_POOL};
pub use form::{Form, FormErrors};
pub use in_::In;
pub use mn::Mn;
pub use sql_types::citext;
pub use user_status::UserStatus;
pub use utils::elapsed;

pub type Request = http::Request<Vec<u8>>;
pub type Result<T> = std::result::Result<T, failure::Error>;
pub type FResult<T> = Result<std::result::Result<T, FormErrors>>;

pub enum Language {
    English,
    Hindi,
}

impl Default for Language {
    fn default() -> Language {
        Language::English
    }
}

pub fn error_stack(err: &failure::Error) -> String {
    let mut b = "".to_string();
    for cause in err.iter_causes() {
        b.push_str(&format!("{}\n", cause));
    }
    b.push_str(&format!("{}\n", err.backtrace()));
    b
}

pub fn is_test() -> bool {
    std::env::args().any(|e| e == "--test")
}

pub fn hash_password(password: &str) -> Result<String> {
    bcrypt::hash(
        password,
        if is_test() {
            /* if we do not do this, tests are too slow */
            4
        } else {
            bcrypt::DEFAULT_COST
        },
    )
    .map_err(|e| e.into())
}

pub fn verify_password(password: &str, hash: &str) -> Result<bool> {
    bcrypt::verify(password, hash).map_err(|e| e.into())
}