Repository navigation
Expand file tree
/
Copy pathscalar_input.rs
More file actions
157 lines (151 loc) · 4.91 KB
/
Copy pathscalar_input.rs
File metadata and controls
157 lines (151 loc) · 4.91 KB
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
//! Finite scalar input codecs shared by form and reactive URL boundaries.
use crate::hir::CheckedProgram;
use crate::value::{Type, Value, ValueKind};
use serde::Serialize;
use std::collections::BTreeMap;
#[derive(Clone, Debug, Serialize)]
#[serde(tag = "kind")]
pub(crate) enum Scalar {
Text {
min: usize,
max: usize,
},
Int,
Bool,
Id {
tag: String,
},
Choice {
name: String,
choices: BTreeMap<String, String>,
},
Optional {
inner: Box<Self>,
},
}
#[derive(Clone, Copy, Debug, Serialize)]
pub(crate) enum FieldError {
Required,
TooShort,
TooLong,
InvalidInteger,
InvalidBoolean,
InvalidId,
InvalidChoice,
}
impl Scalar {
pub(crate) fn checked(ty: &Type, program: &CheckedProgram) -> Option<Self> {
Some(match ty {
Type::Text => Self::Text {
min: 0,
max: crate::MAX_SOURCE_BYTES,
},
Type::BoundedText(min, max) => Self::Text {
min: *min,
max: *max,
},
Type::Int => Self::Int,
Type::Bool => Self::Bool,
Type::Id(tag) => Self::Id { tag: tag.clone() },
Type::Enum(name) => {
let definition = program.enums.get(name)?;
if definition
.variants
.values()
.any(|fields| !fields.is_empty())
{
return None;
}
Self::Choice {
name: name.clone(),
choices: definition
.variants
.keys()
.map(|name| {
(
crate::project::resolve::public_name(name).to_owned(),
name.clone(),
)
})
.collect(),
}
}
Type::Option(inner) if !matches!(**inner, Type::Option(_)) => Self::Optional {
inner: Box::new(Self::checked(inner, program)?),
},
_ => return None,
})
}
pub(crate) fn decode(&self, raw: &str) -> std::result::Result<Value, FieldError> {
if raw.len() > 16_384 {
return Err(FieldError::TooLong);
}
if let Self::Optional { inner } = self {
return if raw.is_empty() {
Ok(Value(ValueKind::None))
} else {
inner
.decode(raw)
.map(|value| Value(ValueKind::Some(Box::new(value))))
};
}
if raw.is_empty() && !matches!(self, Self::Text { min: 0, .. }) {
return Err(FieldError::Required);
}
Ok(Value(match self {
Self::Text { min, max } => {
let length = raw.chars().count();
if length < *min {
return Err(FieldError::TooShort);
}
if length > *max {
return Err(FieldError::TooLong);
}
ValueKind::Text(raw.into())
}
Self::Int => {
let value: i64 = raw.parse().map_err(|_| FieldError::InvalidInteger)?;
if value.to_string() != raw {
return Err(FieldError::InvalidInteger);
}
ValueKind::Int(value)
}
Self::Bool => ValueKind::Bool(match raw {
"true" => true,
"false" => false,
_ => return Err(FieldError::InvalidBoolean),
}),
Self::Id { tag } => {
if !crate::auth::valid_id(raw) {
return Err(FieldError::InvalidId);
}
ValueKind::Id(tag.clone(), raw.into())
}
Self::Choice { name, choices } => ValueKind::Variant(
name.clone(),
choices.get(raw).ok_or(FieldError::InvalidChoice)?.clone(),
vec![],
),
Self::Optional { .. } => return Err(FieldError::InvalidChoice),
}))
}
pub(crate) fn format(&self, value: &Value) -> Option<String> {
match (self, &value.0) {
(Self::Optional { .. }, ValueKind::None) => Some(String::new()),
(Self::Optional { inner }, ValueKind::Some(value)) => inner.format(value),
(Self::Choice { choices, .. }, ValueKind::Variant(_, variant, fields))
if fields.is_empty() =>
{
choices
.iter()
.find(|(_, name)| *name == variant)
.map(|(name, _)| name.clone())
}
_ => {
let raw = value.display_text().ok()?;
self.decode(&raw).ok()?;
Some(raw)
}
}
}
}