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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
use safecast::{CastFrom, TryCastInto};
use tc_error::*;
use tc_value::{Float, Number, NumberClass, NumberInstance, Trigonometry, Value};
use tcgeneric::{label, PathSegment};
use crate::route::{GetHandler, Handler, PostHandler, Route};
use crate::state::State;
struct Dual<F> {
op: F,
}
impl<F> Dual<F> {
fn new(op: F) -> Self {
Self { op }
}
}
impl<'a, F> Handler<'a> for Dual<F>
where
F: Fn(Number) -> TCResult<Number> + Send + 'a,
{
fn get<'b>(self: Box<Self>) -> Option<GetHandler<'a, 'b>>
where
'b: 'a,
{
Some(Box::new(|_txn, value| {
Box::pin(async move {
let value = value.try_cast_into(|v| TCError::bad_request("not a Number", v))?;
(self.op)(value).map(Value::Number).map(State::from)
})
}))
}
fn post<'b>(self: Box<Self>) -> Option<PostHandler<'a, 'b>>
where
'b: 'a,
{
Some(Box::new(|_txn, mut params| {
Box::pin(async move {
let value: Number = params.require(&label("r").into())?;
params.expect_empty()?;
(self.op)(value).map(Value::Number).map(State::from)
})
}))
}
}
struct Log {
n: Number,
}
impl Log {
fn new(n: Number) -> Self {
Self { n }
}
}
impl<'a> Handler<'a> for Log {
fn get<'b>(self: Box<Self>) -> Option<GetHandler<'a, 'b>>
where
'b: 'a,
{
Some(Box::new(|_txn, value| {
Box::pin(async move {
if self.n == Number::from(0) {
return Err(TCError::unsupported("the logarithm of zero is undefined"));
}
let log = if value.is_none() {
Ok(self.n.ln())
} else {
let base: Number =
value.try_cast_into(|v| TCError::bad_request("not a Number", v))?;
if base.class().is_complex() {
Err(TCError::bad_request("invalid base for log", base))
} else {
let base = Float::cast_from(base);
Ok(self.n.log(base))
}
}?;
Ok(Value::Number(log).into())
})
}))
}
fn post<'b>(self: Box<Self>) -> Option<PostHandler<'a, 'b>>
where
'b: 'a,
{
Some(Box::new(|_txn, mut params| {
Box::pin(async move {
let base: Value = params.or_default(&label("r").into())?;
params.expect_empty()?;
let log = if base.is_none() {
self.n.ln()
} else {
let base: Number =
base.try_cast_into(|v| TCError::bad_request("invalid base for log", v))?;
if base.class().is_complex() {
return Err(TCError::bad_request(
"log does not support a complex base",
base,
));
}
let base = Float::cast_from(base);
self.n.log(base)
};
Ok(Value::Number(log).into())
})
}))
}
}
struct Unary<F> {
name: &'static str,
op: F,
}
impl<F> Unary<F> {
fn new(name: &'static str, op: F) -> Self {
Self { name, op }
}
}
impl<'a, F> Handler<'a> for Unary<F>
where
F: Fn() -> Number + Send + 'a,
{
fn get<'b>(self: Box<Self>) -> Option<GetHandler<'a, 'b>>
where
'b: 'a,
{
Some(Box::new(|_txn, value| {
Box::pin(async move {
if value.is_some() {
return Err(TCError::unsupported(format!(
"{} does not have any parameters (found {})",
self.name, value
)));
}
Ok(State::from(Value::from((self.op)())))
})
}))
}
}
impl Route for Number {
fn route<'a>(&'a self, path: &'a [PathSegment]) -> Option<Box<dyn Handler<'a> + 'a>> {
if path.len() != 1 {
return None;
}
let handler: Box<dyn Handler<'a> + 'a> = match path[0].as_str() {
"abs" => Box::new(Unary::new("abs", move || self.abs())),
"add" => Box::new(Dual::new(move |other| Ok(*self + other))),
"and" => Box::new(Dual::new(move |other| Ok(self.and(other)))),
"div" => Box::new(Dual::new(move |other: Number| {
if other == other.class().zero() {
Err(TCError::unsupported("cannot divide by zero"))
} else {
Ok(*self / other)
}
})),
"ln" => Box::new(Unary::new("ln", move || self.ln())),
"log" => Box::new(Log::new(*self)),
"mod" => Box::new(Dual::new(move |other| Ok(*self % other))),
"mul" => Box::new(Dual::new(move |other| Ok(*self * other))),
"round" => Box::new(Unary::new("round", move || self.round())),
"sub" => Box::new(Dual::new(move |other| Ok(*self - other))),
"pow" => Box::new(Dual::new(move |other| Ok(self.pow(other)))),
"gt" => Box::new(Dual::new(move |other| Ok((*self > other).into()))),
"gte" => Box::new(Dual::new(move |other| Ok((*self >= other).into()))),
"lt" => Box::new(Dual::new(move |other| Ok((*self < other).into()))),
"lte" => Box::new(Dual::new(move |other| Ok((*self <= other).into()))),
"not" => Box::new(Unary::new("not", move || self.not())),
"or" => Box::new(Dual::new(move |other| Ok(self.or(other)))),
"xor" => Box::new(Dual::new(move |other| Ok(self.xor(other)))),
"asin" => Box::new(Unary::new("abs", move || self.asin())),
"sin" => Box::new(Unary::new("sin", move || self.sin())),
"asinh" => Box::new(Unary::new("asinh", move || self.asinh())),
"sinh" => Box::new(Unary::new("sinh", move || self.sinh())),
"acos" => Box::new(Unary::new("acos", move || self.acos())),
"cos" => Box::new(Unary::new("cos", move || self.cos())),
"acosh" => Box::new(Unary::new("acosh", move || self.acosh())),
"cosh" => Box::new(Unary::new("cosh", move || self.cosh())),
"atan" => Box::new(Unary::new("atan", move || self.atan())),
"tan" => Box::new(Unary::new("tan", move || self.tan())),
"atanh" => Box::new(Unary::new("atanh", move || self.atanh())),
"tanh" => Box::new(Unary::new("tanh", move || self.tanh())),
_ => return None,
};
Some(handler)
}
}