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
use self::super::super::Outcome;
use self::super::read_toml_file;
use std::cmp::Ordering;
use toml::encode_str;
use egg_mode::Token;
use std::path::Path;
use std::io::Write;
use std::fs::File;
#[derive(Debug, Clone, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
pub struct User {
pub name: String,
pub id: i64,
pub access_token_key: String,
pub access_token_secret: String,
}
#[derive(Debug, Clone, Hash, PartialEq, Eq, RustcEncodable, RustcDecodable)]
struct Users {
user: Vec<User>,
}
impl User {
pub fn from_raw_access_token<'t>(raw: (Token<'t>, i64, String)) -> User {
let (access_token, user_id, username) = raw;
User {
name: username,
id: user_id,
access_token_key: access_token.key.to_owned().to_string(),
access_token_secret: access_token.secret.to_owned().to_string(),
}
}
pub fn read(p: &Path) -> Result<Vec<User>, Option<Outcome>> {
read_toml_file(p, "users").map(|us: Users| us.user)
}
pub fn write(users: Vec<User>, p: &Path) {
File::create(p).unwrap().write_all(encode_str(&Users { user: users }).as_bytes()).unwrap();
}
}
impl Ord for User {
fn cmp(&self, other: &Self) -> Ordering {
self.name.cmp(&other.name)
}
}
impl PartialOrd for User {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.name.partial_cmp(&other.name)
}
}