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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
use std::{ops::Deref, str::FromStr};
use rusqlite::{
ToSql,
types::{FromSql, FromSqlError, FromSqlResult, ToSqlOutput, ValueRef},
};
use crate::ParseError;
const USER_ID_LENGTH: usize = 6;
const SESSION_ID_LENGTH: usize = 12;
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Base58Id<const N: usize>([u8; N]);
pub type UserId = Base58Id<USER_ID_LENGTH>;
pub type SessionId = Base58Id<SESSION_ID_LENGTH>;
impl<const N: usize> Base58Id<N> {
pub fn new(id: [u8; N]) -> Result<Self, ParseError> {
id.iter().try_for_each(|b| {
if !b.is_ascii_graphic() {
Err(ParseError::IdNotPrintable)
} else {
Ok(())
}
})?;
Ok(Self(id))
}
pub unsafe fn new_unchecked(id: [u8; N]) -> Self {
Self(id)
}
pub fn as_str(&self) -> &str {
// SAFTEY: the UserID is only ever expected to contain base58 characters.
// All constructors are privated to the crate or do not allow setting the
// data of the ID itself without checks
str::from_utf8(&self.0).unwrap()
}
pub fn as_slice(&self) -> &[u8] {
&self.0
}
}
impl<const N: usize> Deref for Base58Id<N> {
type Target = [u8; N];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<const N: usize> ToSql for Base58Id<N> {
fn to_sql(&self) -> rusqlite::Result<ToSqlOutput<'_>> {
self.as_str().to_sql()
}
}
impl<const N: usize> FromSql for Base58Id<N> {
fn column_result(value: ValueRef<'_>) -> FromSqlResult<Self> {
match value {
ValueRef::Text(data) => data.try_into().map_err(|pe| FromSqlError::from(pe)),
_ => Err(FromSqlError::InvalidType),
}
}
}
impl<const N: usize> FromStr for Base58Id<N> {
type Err = ParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.as_bytes().try_into()
}
}
impl<const N: usize> TryFrom<&[u8]> for Base58Id<N> {
type Error = ParseError;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
let tryinto: [u8; N] = bytes.try_into().map_err(|_| ParseError::IdIncorrectSize {
expected: N,
actual: bytes.len(),
})?;
Base58Id::new(tryinto).map_err(|pe| pe.into())
}
}
#[derive(Clone, Debug)]
pub struct HashedPassword(String);
impl HashedPassword {
pub fn new(hash: String) -> Self {
Self(hash)
}
pub fn to_string(&self) -> String {
self.0.clone()
}
}
pub trait AsUserId {
fn userid(&self) -> &UserId;
}
#[derive(Clone, Debug, PartialEq)]
pub struct AnonymousUser {
id: UserId,
}
impl AnonymousUser {
pub fn new(id: UserId) -> Self {
Self { id }
}
}
impl AsUserId for AnonymousUser {
fn userid(&self) -> &UserId {
&self.id
}
}
#[derive(Clone, Debug)]
pub struct User {
id: UserId,
name: String,
}
impl AsUserId for User {
fn userid(&self) -> &UserId {
&self.id
}
}
impl User {
pub fn new(id: UserId, name: String) -> Self {
Self { id, name }
}
pub fn name(&self) -> &str {
&self.name
}
}
#[derive(Clone, Debug)]
pub struct UserAuth {
user: User,
hash: HashedPassword,
}
impl UserAuth {
pub fn new(user: User, hash: HashedPassword) -> Self {
Self { user, hash }
}
pub fn hash(&self) -> &HashedPassword {
&self.hash
}
pub fn user(&self) -> &User {
&self.user
}
}
impl Into<User> for UserAuth {
fn into(self) -> User {
self.user
}
}
#[derive(Clone, Debug)]
pub struct Session {
id: SessionId,
user: User,
}
impl Session {
pub fn new(id: SessionId, user: User) -> Self {
Self { id, user }
}
pub fn sessionid(&self) -> &SessionId {
&self.id
}
pub fn user(&self) -> &User {
&self.user
}
}
impl Into<User> for Session {
fn into(self) -> User {
self.user
}
}
|