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
use crate::{core::Library, error::Result, utils::Id};
use serde::{Deserialize, Serialize};
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize)]
pub enum Session {
Global,
Id(Id),
}
impl Session {
pub(crate) fn id(&self) -> Option<Id> {
match self {
Self::Id(id) => Some(*id),
Self::Global => None,
}
}
}
impl From<Id> for Session {
fn from(id: Id) -> Self {
Self::Id(id)
}
}
pub const GLOBAL: Session = Session::Global;
pub struct SessionsApi<'alex> {
pub(crate) inner: &'alex Library,
}
impl<'alex> SessionsApi<'alex> {
pub async fn list(&self) -> Vec<Id> {
vec![]
}
pub async fn open(&self, id: Id, pw: &str) -> Result<Session> {
let ref mut u = self.inner.users.write().await;
u.open(id, pw).map(|_| Session::Id(id))
}
pub async fn close(&self, id: Session) -> Result<()> {
if let Some(id) = id.id() {
let ref mut u = self.inner.users.write().await;
u.close(id)
} else {
Ok(())
}
}
pub async fn create(&self, id: Id, pw: &str) -> Result<Session> {
let ref mut u = self.inner.users.write().await;
u.insert(id, pw).map(|_| Session::Id(id))
}
pub async fn destroy(&self, id: Session) -> Result<()> {
if let Some(id) = id.id() {
let ref mut u = self.inner.users.write().await;
u.delete(id)
} else {
Ok(())
}
}
}