/
common.rs
163 lines (148 loc) · 5.03 KB
/
common.rs
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
use std::str::FromStr;
use juniper::GraphQLObject;
use juniper::ID;
use thiserror::Error;
use ulid::Ulid;
use crate::entities;
use anyhow::anyhow;
#[derive(Debug, Error)]
#[error("{source}")]
pub struct GraphqlUserError {
#[source]
pub source: anyhow::Error,
}
impl From<anyhow::Error> for GraphqlUserError {
fn from(source: anyhow::Error) -> Self {
Self { source }
}
}
impl From<&str> for GraphqlUserError {
fn from(source: &str) -> Self {
Self {
source: anyhow!("{}", source),
}
}
}
#[derive(Debug)]
pub struct ApiError(pub anyhow::Error);
impl<S: juniper::ScalarValue> juniper::IntoFieldError<S> for ApiError {
fn into_field_error(self) -> juniper::FieldError<S> {
match self.0.downcast_ref::<GraphqlUserError>() {
Some(err) => err.source.to_string().into(),
None => {
tracing::error!("{:?}", self.0);
"Internal error".into()
}
}
}
}
impl<T: Into<anyhow::Error>> From<T> for ApiError {
fn from(err: T) -> Self {
Self(err.into())
}
}
pub fn encode_limit(
first: Option<i32>,
last: Option<i32>,
) -> Result<entities::Limit, GraphqlUserError> {
match (first, last) {
(Some(first), None) => {
if first < 0 {
Err(GraphqlUserError::from(
"first must be greater than or equal to 0",
))
} else {
entities::Limit::new(entities::LimitKind::First, first)
.map_err(anyhow::Error::from)
.map_err(GraphqlUserError::from)
}
}
(None, Some(last)) => {
if last < 0 {
Err(GraphqlUserError::from(
"last must be greater than or equal to 0",
))
} else {
entities::Limit::new(entities::LimitKind::Last, last)
.map_err(anyhow::Error::from)
.map_err(GraphqlUserError::from)
}
}
_ => Err(GraphqlUserError::from(
"Must provide either first or last, not both",
)),
}
}
#[derive(Debug, Clone)]
pub enum NodeId {
FigureRecord(entities::FigureRecordId),
CharacterConfig(entities::UserId, entities::Character),
Character(entities::Character),
UserConfig(entities::UserId),
CharacterConfigSeed(entities::Character),
}
impl NodeId {
pub fn to_id(&self) -> ID {
match self {
NodeId::FigureRecord(id) => {
ID::new(base64::encode(format!("FigureRecord:{}", Ulid::from(*id))))
}
NodeId::CharacterConfig(user_id, character) => ID::new(base64::encode(format!(
"CharacterConfig:{}-{}",
base64::encode(String::from(user_id.clone())),
base64::encode(String::from(character.clone()))
))),
NodeId::Character(character) => ID::new(base64::encode(format!(
"Character:{}",
String::from(character.clone())
))),
NodeId::UserConfig(id) => ID::new(base64::encode(format!(
"UserConfig:{}",
String::from(id.clone())
))),
NodeId::CharacterConfigSeed(character) => ID::new(base64::encode(format!(
"CharacterConfigSeed:{}",
String::from(character.clone())
))),
}
}
pub fn from_id(id: &ID) -> Option<NodeId> {
let id = id.to_string();
let buf = base64::decode(id).ok()?;
let s = String::from_utf8(buf).ok()?;
s.split_once(':').and_then(|(kind, id)| match kind {
"FigureRecord" => Ulid::from_str(id)
.ok()
.map(|id| NodeId::FigureRecord(entities::FigureRecordId::from(id))),
"CharacterConfig" => {
let (user_id, character) = id.split_once('-')?;
let user_id = base64::decode(user_id).ok()?;
let user_id = entities::UserId::from(String::from_utf8(user_id).ok()?);
let character = base64::decode(character).ok()?;
let character = String::from_utf8(character).ok()?;
let character = entities::Character::try_from(character.as_str()).ok()?;
Some(NodeId::CharacterConfig(user_id, character))
}
"Character" => entities::Character::try_from(id)
.ok()
.map(NodeId::Character),
"UserConfig" => Some(NodeId::UserConfig(entities::UserId::from(id.to_string()))),
"CharacterConfigSeed" => entities::Character::try_from(id)
.ok()
.map(NodeId::CharacterConfigSeed),
_ => None,
})
}
}
#[derive(GraphQLObject, Clone, Debug)]
pub struct PageInfo {
pub has_next_page: bool,
pub has_previous_page: bool,
pub start_cursor: Option<String>,
pub end_cursor: Option<String>,
}
#[derive(GraphQLObject, Clone, Debug)]
#[graphql(name = "Error")]
pub struct GraphqlErrorType {
pub message: String,
}