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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use std::hash::Hash;
use async_trait::async_trait;
pub mod github;
pub mod wechat;
pub mod wecom;
pub mod qq;
#[derive(Debug)]
pub struct Error(String);
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::error::Error for Error {}
impl From<reqwest::Error> for Error {
fn from(e: reqwest::Error) -> Self {
Self(e.to_string())
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub struct Userinfo{
pub unique_id: String,
pub name: String,
pub email: Option<String>,
pub organization: Option<Vec<Organization>>,
}
pub struct Organization {
pub unique_id: String,
pub name: String,
}
pub enum Client {
Github(github::Client),
}
pub struct Service<T>
where
T: Eq + Hash,
{
clients: HashMap<T, Client>,
}
impl<T> Service<T>
where
T: Eq + Hash,
{
pub fn new() -> Self {
Self {
clients: HashMap::new(),
}
}
pub fn register(&mut self, index: T, cli: Client) {
self.clients.insert(index, cli);
}
pub fn deregister(&mut self, index: T) {
self.clients.remove(&index);
}
pub async fn userinfo(&self, index: &T, code: &str) -> Result<Userinfo> {
let cli = self.clients.get(index).unwrap();
match cli {
Client::Github(gh) => gh.userinfo(code).await,
}
}
}
#[async_trait]
pub trait Profile {
async fn userinfo(&self, code: &str) -> Result<Userinfo>;
}
#[cfg(test)]
mod tests {
#[derive(Hash, Eq)]
enum Type {
Github = 0,
}
#[test]
fn test_clients() {
let mut mgr = super::Service::new();
let gh = super::github::Client::new("", "");
mgr.register(Type::Github, super::Client::Github(gh));
let userinfo = mgr.userinfo(Type::Github, "");
}
}