1#[derive(Debug, Clone, Copy)]
12pub struct KernelMethod {
13 pub name: &'static str,
14 pub signature: &'static str,
15}
16
17macro_rules! kernel_ops {
21 ($(
22 $(#[$meta:meta])*
23 $Enum:ident {
24 $( $(#[$vmeta:meta])* $Var:ident = $name:literal, $sig:literal; )*
25 $( @hidden $( $(#[$hmeta:meta])* $HVar:ident = $hname:literal; )* )?
26 }
27 )*) => {
28 $(
29 $(#[$meta])*
30 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
31 pub enum $Enum {
32 $( $(#[$vmeta])* $Var, )*
33 $($( $(#[$hmeta])* $HVar, )*)?
34 }
35
36 impl $Enum {
37 pub const ALL: &'static [$Enum] = &[
39 $( $Enum::$Var, )* $($( $Enum::$HVar, )*)?
40 ];
41 pub const TABLE: &'static [KernelMethod] =
43 &[ $( KernelMethod { name: $name, signature: $sig }, )* ];
44
45 pub fn from_name(name: &str) -> Option<Self> {
47 match name {
48 $( $name => Some($Enum::$Var), )*
49 $($( $hname => Some($Enum::$HVar), )*)?
50 _ => None,
51 }
52 }
53
54 pub const fn name(self) -> &'static str {
56 match self {
57 $( $Enum::$Var => $name, )*
58 $($( $Enum::$HVar => $hname, )*)?
59 }
60 }
61 }
62 )*
63 };
64}
65
66kernel_ops! {
67ListOp {
69 Length = "length", "length() -> Int";
70 Get = "get", "get(index: Int) -> Option[T]";
71 Prepend = "prepend", "prepend(item: T) -> List[T]";
72 Append = "append", "append(item: T) -> List[T]";
74 Concat = "concat", "concat(other: List[T]) -> List[T]";
75 Fold = "fold", "fold(init: U, step: (U, T) -> U) -> U";
76 FoldEff = "foldEff", "foldEff(init: U, step: (U, T) -> Effect[U]) -> Effect[U]";
77 ForEach = "forEach", "forEach(f: T -> Effect[()]) -> Effect[()]";
79 ParForEach = "parForEach", "parForEach(f: T -> Effect[()]) -> Effect[()]";
82 ParTraverse = "parTraverse", "parTraverse(f: T -> Effect[U]) -> Effect[List[U]]";
83 TraverseAll = "traverseAll", "traverseAll(f: T -> Effect[Result[U, E]]) -> Effect[List[Result[U, E]]]";
86 ParTraverseAll = "parTraverseAll", "parTraverseAll(f: T -> Effect[Result[U, E]]) -> Effect[List[Result[U, E]]]";
87 TraverseTry = "traverseTry", "traverseTry(f: T -> Effect[Result[U, E]]) -> Effect[Result[List[U], E]]";
89 ParTraverseTry = "parTraverseTry", "parTraverseTry(f: T -> Effect[Result[U, E]]) -> Effect[Result[List[U], E]]";
90 SequenceAll = "sequenceAll", "sequenceAll() -> Result[List[T], List[E]]";
92 SequenceTry = "sequenceTry", "sequenceTry() -> Result[List[T], E]";
93 Map = "map", "map(f: T -> U) -> List[U]";
95 Filter = "filter", "filter(p: T -> Bool) -> List[T]";
96 FlatMap = "flatMap", "flatMap(f: T -> List[U]) -> List[U]";
97 SortBy = "sortBy", "sortBy(key: T -> K) -> List[T]";
98 SortByDescending = "sortByDescending", "sortByDescending(key: T -> K) -> List[T]";
99 Take = "take", "take(n: Int) -> List[T]";
100 Skip = "skip", "skip(n: Int) -> List[T]";
101 Distinct = "distinct", "distinct() -> List[T]";
102 DistinctBy = "distinctBy", "distinctBy(key: T -> K) -> List[T]";
103 Count = "count", "count() -> Int";
104 Any = "any", "any(p: T -> Bool) -> Bool";
105 All = "all", "all(p: T -> Bool) -> Bool";
106 First = "first", "first() -> Option[T]";
107 FirstOrElse = "firstOrElse", "firstOrElse(default: T) -> T";
108 Reverse = "reverse", "reverse() -> List[T]";
111 Find = "find", "find(p: T -> Bool) -> Option[T]";
112 Contains = "contains", "contains(item: T) -> Bool";
113 Last = "last", "last() -> Option[T]";
114 LastOrElse = "lastOrElse", "lastOrElse(default: T) -> T";
115 IsEmpty = "isEmpty", "isEmpty() -> Bool";
116 Sort = "sort", "sort() -> List[T]";
117 Traverse = "traverse", "traverse(f: T -> Effect[U]) -> Effect[List[U]]";
118 Sum = "sum", "sum(key: T -> K) -> K";
119 Min = "min", "min(key: T -> K) -> Option[K], or min() -> Option[T] for an Ord element";
120 Max = "max", "max(key: T -> K) -> Option[K], or max() -> Option[T] for an Ord element";
121 Average = "average", "average(key: T -> K) -> Option[Float]";
122 JoinOn = "joinOn", "joinOn(other: List[U], left: T -> K, right: U -> K, into: (T, U) -> V) -> List[V]";
125 LeftJoin = "leftJoin", "leftJoin(other: List[U], left: T -> K, right: U -> K, into: (T, Option[U]) -> V) -> List[V]";
126 Join = "join", "join(other: List[U], on: (T, U) -> Bool, into: (T, U) -> V) -> List[V]";
127 GroupBy = "groupBy", "groupBy(key: T -> K, into: (K, List[T]) -> V) -> List[V]";
128 @hidden
129 UpTo = "upTo";
132 }
133SetOp {
136 Length = "length", "length() -> Int";
137 Contains = "contains", "contains(item: T) -> Bool";
138 Insert = "insert", "insert(item: T) -> Set[T]";
139 Remove = "remove", "remove(item: T) -> Set[T]";
140 ToList = "toList", "toList() -> List[T]";
141 Union = "union", "union(other: Set[T]) -> Set[T]";
143 Intersect = "intersect", "intersect(other: Set[T]) -> Set[T]";
144 Difference = "difference", "difference(other: Set[T]) -> Set[T]";
145 IsSubsetOf = "isSubsetOf", "isSubsetOf(other: Set[T]) -> Bool";
146 IsEmpty = "isEmpty", "isEmpty() -> Bool";
147 Filter = "filter", "filter(p: T -> Bool) -> Set[T]";
148 Map = "map", "map(f: T -> U) -> Set[U]";
149 Fold = "fold", "fold(init: U, step: (U, T) -> U) -> U";
150 }
151MapOp {
153 Length = "length", "length() -> Int";
154 Keys = "keys", "keys() -> List[K]";
155 Values = "values", "values() -> List[V]";
156 Get = "get", "get(key: K) -> Option[V]";
157 Insert = "insert", "insert(key: K, value: V) -> Map[K, V]";
158 Remove = "remove", "remove(key: K) -> Map[K, V]";
160 Contains = "contains", "contains(key: K) -> Bool";
161 GetOrElse = "getOrElse", "getOrElse(key: K, default: V) -> V";
162 IsEmpty = "isEmpty", "isEmpty() -> Bool";
163 Entries = "entries", "entries() -> List[MapEntry[K, V]]";
164 Filter = "filter", "filter(p: (K, V) -> Bool) -> Map[K, V]";
165 MapValues = "mapValues", "mapValues(f: V -> U) -> Map[K, U]";
166 Fold = "fold", "fold(init: U, step: (U, K, V) -> U) -> U";
167 }
168QueryOp {
177 Map = "map", "map(f: T -> U) -> Query[U]";
178 Filter = "filter", "filter(p: T -> Bool) -> Query[T]";
179 FlatMap = "flatMap", "flatMap(f: T -> Query[U]) -> Query[U]";
180 SortBy = "sortBy", "sortBy(key: T -> K) -> Query[T]";
181 SortByDescending = "sortByDescending", "sortByDescending(key: T -> K) -> Query[T]";
182 Take = "take", "take(n: Int) -> Query[T]";
183 Skip = "skip", "skip(n: Int) -> Query[T]";
184 Distinct = "distinct", "distinct() -> Query[T]";
185 DistinctBy = "distinctBy", "distinctBy(key: T -> K) -> Query[T]";
186 JoinOn = "joinOn", "joinOn(other: Query[U], left: T -> K, right: U -> K, into: (T, U) -> V) -> Query[V]";
187 LeftJoin = "leftJoin", "leftJoin(other: Query[U], left: T -> K, right: U -> K, into: (T, Option[U]) -> V) -> Query[V]";
188 Join = "join", "join(other: Query[U], on: (T, U) -> Bool, into: (T, U) -> V) -> Query[V]";
189 GroupBy = "groupBy", "groupBy(key: T -> K, into: (K, Query[T]) -> V) -> Query[V]";
190 Collect = "collect", "collect() -> Effect[List[T]]";
191 First = "first", "first() -> Effect[Option[T]]";
192 FirstOrElse = "firstOrElse", "firstOrElse(default: T) -> Effect[T]";
193 Count = "count", "count() -> Effect[Int]";
194 Fold = "fold", "fold(init: U, step: (U, T) -> U) -> Effect[U]";
195 Any = "any", "any(p: T -> Bool) -> Effect[Bool]";
196 All = "all", "all(p: T -> Bool) -> Effect[Bool]";
197 Sum = "sum", "sum(key: T -> K) -> Effect[K]";
198 Min = "min", "min(key: T -> K) -> Effect[Option[K]]";
199 Max = "max", "max(key: T -> K) -> Effect[Option[K]]";
200 Average = "average", "average(key: T -> K) -> Effect[Option[Float]]";
201 ForEach = "forEach", "forEach(f: T -> Effect[()]) -> Effect[()]";
202 ParTraverse = "parTraverse", "parTraverse(f: T -> Effect[()]) -> Effect[()]";
203 TraverseAll = "traverseAll", "traverseAll(f: T -> Effect[Result[U, E]]) -> Effect[List[Result[U, E]]]";
204 ParTraverseAll = "parTraverseAll", "parTraverseAll(f: T -> Effect[Result[U, E]]) -> Effect[List[Result[U, E]]]";
205 TraverseTry = "traverseTry", "traverseTry(f: T -> Effect[Result[U, E]]) -> Effect[Result[List[U], E]]";
206 ParTraverseTry = "parTraverseTry", "parTraverseTry(f: T -> Effect[Result[U, E]]) -> Effect[Result[List[U], E]]";
207 }
208OptionOp {
210 Map = "map", "map(f: T -> U) -> Option[U]";
211 AndThen = "andThen", "andThen(f: T -> Option[U]) -> Option[U]";
212 GetOrElse = "getOrElse", "getOrElse(default: T) -> T";
213 IsSome = "isSome", "isSome() -> Bool";
214 OkOr = "okOr", "okOr(err: E) -> Result[T, E]";
215 IsNone = "isNone", "isNone() -> Bool";
216 OrElse = "orElse", "orElse(f: () -> Option[T]) -> Option[T]";
217 Filter = "filter", "filter(p: T -> Bool) -> Option[T]";
218 }
219ResultOp {
221 Map = "map", "map(f: T -> U) -> Result[U, E]";
222 AndThen = "andThen", "andThen(f: T -> Result[U, E]) -> Result[U, E]";
223 MapErr = "mapErr", "mapErr(f: E -> F) -> Result[T, F]";
224 GetOrElse = "getOrElse", "getOrElse(default: T) -> T";
225 IsOk = "isOk", "isOk() -> Bool";
226 IsErr = "isErr", "isErr() -> Bool";
227 Ok = "ok", "ok() -> Option[T]";
228 }
229StringOp {
231 Length = "length", "length() -> Int";
232 Split = "split", "split(sep: String) -> List[String]";
233 Trim = "trim", "trim() -> String";
234 ToUpper = "toUpper", "toUpper() -> String";
235 ToLower = "toLower", "toLower() -> String";
236 Contains = "contains", "contains(s: String) -> Bool";
237 StartsWith = "startsWith", "startsWith(s: String) -> Bool";
238 EndsWith = "endsWith", "endsWith(s: String) -> Bool";
239 Replace = "replace", "replace(from: String, to: String) -> String";
240 Slice = "slice", "slice(start: Int, end: Int) -> String";
241 IndexOf = "indexOf", "indexOf(s: String) -> Option[Int]";
242 Chars = "chars", "chars() -> List[String]";
243 Concat = "concat", "concat(s: String) -> String";
244 PercentEncode = "percentEncode", "percentEncode() -> String";
246 PercentDecode = "percentDecode", "percentDecode() -> Option[String]";
247 MatchesRegex = "matchesRegex", "matchesRegex(pattern: String) -> Bool";
249 ContainsRegex = "containsRegex", "containsRegex(pattern: String) -> Bool";
250 FindRegex = "findRegex", "findRegex(pattern: String) -> Option[List[Option[String]]]";
251 FindAllRegex = "findAllRegex", "findAllRegex(pattern: String) -> List[List[Option[String]]]";
252 ReplaceRegex = "replaceRegex", "replaceRegex(pattern: String, with: String) -> String";
253 SplitRegex = "splitRegex", "splitRegex(pattern: String) -> List[String]";
254 }
255IntOp {
257 ToFloat = "toFloat", "toFloat() -> Float";
258 ToString = "toString", "toString() -> String";
259 Abs = "abs", "abs() -> Int";
260 Min = "min", "min(other: Int) -> Int";
261 Max = "max", "max(other: Int) -> Int";
262 Clamp = "clamp", "clamp(lo: Int, hi: Int) -> Int";
263 Div = "div", "div(other: Int) -> Int";
265 Rem = "rem", "rem(other: Int) -> Int";
266 Mod = "mod", "mod(other: Int) -> Int";
267 Pow = "pow", "pow(exp: Int) -> Int";
268 And = "and", "and(other: Int) -> Int";
271 Or = "or", "or(other: Int) -> Int";
272 Xor = "xor", "xor(other: Int) -> Int";
273 AndNot = "andNot", "andNot(mask: Int) -> Int";
274 Shl = "shl", "shl(count: Int) -> Int";
275 Shr = "shr", "shr(count: Int) -> Int";
276 }
277FloatOp {
279 Round = "round", "round() -> Int";
280 Floor = "floor", "floor() -> Int";
281 Ceil = "ceil", "ceil() -> Int";
282 Truncate = "truncate", "truncate() -> Int";
283 ToString = "toString", "toString() -> String";
284 Abs = "abs", "abs() -> Float";
285 Min = "min", "min(other: Float) -> Float";
286 Max = "max", "max(other: Float) -> Float";
287 Clamp = "clamp", "clamp(lo: Float, hi: Float) -> Float";
288 IsNaN = "isNaN", "isNaN() -> Bool";
289 IsFinite = "isFinite", "isFinite() -> Bool";
290 Sqrt = "sqrt", "sqrt() -> Float";
292 Pow = "pow", "pow(exp: Float) -> Float";
293 Exp = "exp", "exp() -> Float";
294 Ln = "ln", "ln() -> Float";
295 Log10 = "log10", "log10() -> Float";
296 Log2 = "log2", "log2() -> Float";
297 Sin = "sin", "sin() -> Float";
298 Cos = "cos", "cos() -> Float";
299 Tan = "tan", "tan() -> Float";
300 Asin = "asin", "asin() -> Float";
301 Acos = "acos", "acos() -> Float";
302 Atan = "atan", "atan() -> Float";
303 Atan2 = "atan2", "atan2(x: Float) -> Float";
304 }
305DurationOp {
308 ToMillis = "toMillis", "toMillis() -> Int";
309 ToString = "toString", "toString() -> String";
310 }
311InstantOp {
314 ToEpochMillis = "toEpochMillis", "toEpochMillis() -> Int";
315 ToString = "toString", "toString() -> String";
316 ToIso = "toIso", "toIso() -> String";
318 }
319BytesOp {
324 Length = "length", "length() -> Int";
325 Concat = "concat", "concat(other: Bytes) -> Bytes";
326 Slice = "slice", "slice(start: Int, end: Int) -> Bytes";
327 Compare = "compare", "compare(other: Bytes) -> Int";
328 ToHex = "toHex", "toHex() -> String";
329 ToBase64 = "toBase64", "toBase64() -> String";
330 ToBase64Url = "toBase64Url", "toBase64Url() -> String";
331 DecodeUtf8 = "decodeUtf8", "decodeUtf8() -> Option[String]";
332 }
333DecimalOp {
336 Scale = "scale", "scale() -> Int";
337 ToString = "toString", "toString() -> String";
338 Round = "round", "round(scale: Int, mode: RoundingMode) -> Decimal";
339 Div = "div", "div(other: Decimal, scale: Int, mode: RoundingMode) -> Decimal";
340 Abs = "abs", "abs() -> Decimal";
341 Min = "min", "min(other: Decimal) -> Decimal";
342 Max = "max", "max(other: Decimal) -> Decimal";
343 ToInt = "toInt", "toInt() -> Option[Int]";
344 ToFloat = "toFloat", "toFloat() -> Float";
345 }
346RequestHeadersOp {
349 Get = "get", "get(name: String) -> Option[String]";
350 Bearer = "bearer", "bearer() -> Option[String]";
351 ClientIp = "clientIp", "clientIp() -> Option[String]";
352 }
353HttpResultOp {
356 WithCookie = "withCookie", "withCookie(c: Cookie) -> HttpResult[T]";
357 ClearCookie = "clearCookie", "clearCookie(c: Cookie) -> HttpResult[T]";
358 Cookies = "cookies", "cookies() -> List[Cookie]";
359 }
360RequestQueryOp {
363 Get = "get", "get(name: String) -> Option[String]";
364 All = "all", "all(name: String) -> List[String]";
365 Params = "params", "params() -> Map[String, List[String]]";
366 }
367RequestCookiesOp {
370 Get = "get", "get(name: String) -> Option[String]";
371 All = "all", "all(name: String) -> List[String]";
372 Names = "names", "names() -> List[String]";
373 }
374TokenClaimsOp {
376 Get = "get", "get(name: String) -> Option[String]";
377 Has = "has", "has(name: String) -> Bool";
378 }
379TestConnectionOp {
382 Sent = "sent", "sent() -> List[F]";
383 Closed = "closed", "closed() -> Bool";
384 Connection = "connection", "connection() -> Connection[F]";
385 }
386
387StreamOp {
390 Map = "map", "map(f: T -> U) -> Stream[U]";
391 Take = "take", "take(n: Int) -> Stream[T]";
392 Collect = "collect", "collect() -> Effect[List[T]]";
393 }
394ConnectionOp {
397 Send = "send", "send(frame: F) -> Effect[()]";
398 Close = "close", "close() -> Effect[()]";
399 }
400EffectOp {
402 Within = "within", "within(d: Duration) -> Effect[Result[T, TimedOut]]";
406 Race = "race", "race(other: Effect[T]) -> Effect[T]";
410 Detach = "detach", "detach() -> Effect[()]";
414 }
415EffectResultOp {
419 MapOk = "mapOk", "mapOk(f: T -> U) -> Effect[Result[U, E]]";
420 MapErr = "mapErr", "mapErr(f: E -> F) -> Effect[Result[T, F]]";
421 FlatMapOk = "flatMapOk", "flatMapOk(f: T -> Effect[Result[U, E]]) -> Effect[Result[U, E]]";
422 FlatMapErr = "flatMapErr", "flatMapErr(f: E -> Effect[Result[T, F]]) -> Effect[Result[T, F]]";
423 }
424}
425
426#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
428pub enum KernelOp {
429 List(ListOp),
430 Query(QueryOp),
431 Stream(StreamOp),
432 Connection(ConnectionOp),
433 TestConnection(TestConnectionOp),
434 Map(MapOp),
435 Set(SetOp),
436 Int(IntOp),
437 Float(FloatOp),
438 Duration(DurationOp),
439 Instant(InstantOp),
440 RequestHeaders(RequestHeadersOp),
441 HttpResult(HttpResultOp),
442 RequestQuery(RequestQueryOp),
443 RequestCookies(RequestCookiesOp),
444 TokenClaims(TokenClaimsOp),
445 Bytes(BytesOp),
446 Decimal(DecimalOp),
447 String(StringOp),
448 Option(OptionOp),
449 Result(ResultOp),
450 Effect(EffectOp),
452 EffectResult(EffectResultOp),
454}
455
456impl KernelOp {
457 pub const fn name(self) -> &'static str {
459 match self {
460 KernelOp::List(o) => o.name(),
461 KernelOp::Query(o) => o.name(),
462 KernelOp::Stream(o) => o.name(),
463 KernelOp::Connection(o) => o.name(),
464 KernelOp::TestConnection(o) => o.name(),
465 KernelOp::Map(o) => o.name(),
466 KernelOp::Set(o) => o.name(),
467 KernelOp::Int(o) => o.name(),
468 KernelOp::Float(o) => o.name(),
469 KernelOp::Duration(o) => o.name(),
470 KernelOp::Instant(o) => o.name(),
471 KernelOp::RequestHeaders(o) => o.name(),
472 KernelOp::HttpResult(o) => o.name(),
473 KernelOp::RequestQuery(o) => o.name(),
474 KernelOp::RequestCookies(o) => o.name(),
475 KernelOp::TokenClaims(o) => o.name(),
476 KernelOp::Bytes(o) => o.name(),
477 KernelOp::Decimal(o) => o.name(),
478 KernelOp::String(o) => o.name(),
479 KernelOp::Option(o) => o.name(),
480 KernelOp::Result(o) => o.name(),
481 KernelOp::Effect(o) => o.name(),
482 KernelOp::EffectResult(o) => o.name(),
483 }
484 }
485}
486
487impl KernelOp {
488 pub fn for_base(base: bynk_syntax::ast::BaseType, name: &str) -> Option<KernelOp> {
492 use bynk_syntax::ast::BaseType;
493 match base {
494 BaseType::Int => IntOp::from_name(name).map(KernelOp::Int),
495 BaseType::Float => FloatOp::from_name(name).map(KernelOp::Float),
496 BaseType::String => StringOp::from_name(name).map(KernelOp::String),
497 BaseType::Duration => DurationOp::from_name(name).map(KernelOp::Duration),
498 BaseType::Instant => InstantOp::from_name(name).map(KernelOp::Instant),
499 BaseType::Bytes => BytesOp::from_name(name).map(KernelOp::Bytes),
500 BaseType::Decimal => DecimalOp::from_name(name).map(KernelOp::Decimal),
501 BaseType::Bool => None,
502 }
503 }
504}
505
506kernel_ops! {
511ListStatic {
513 Empty = "empty", "List.empty() -> List[T]";
514 }
515MapStatic {
517 Empty = "empty", "Map.empty() -> Map[K, V]";
518 Of = "of", "Map.of(entries: List[MapEntry[K, V]]) -> Map[K, V]";
519 }
520SetStatic {
522 Empty = "empty", "Set.empty() -> Set[T]";
523 Of = "of", "Set.of(items: List[T]) -> Set[T]";
524 }
525IntStatic {
527 Parse = "parse", "Int.parse(s: String) -> Option[Int]";
528 }
529FloatStatic {
531 Parse = "parse", "Float.parse(s: String) -> Option[Float]";
532 Pi = "pi", "Float.pi() -> Float";
533 E = "e", "Float.e() -> Float";
534 }
535DecimalStatic {
537 Parse = "parse", "Decimal.parse(s: String) -> Option[Decimal]";
538 FromInt = "fromInt", "Decimal.fromInt(n: Int) -> Decimal";
539 }
540DurationStatic {
542 Millis = "millis", "Duration.millis(n: Int) -> Duration";
543 }
544InstantStatic {
546 FromEpochMillis = "fromEpochMillis", "Instant.fromEpochMillis(n: Int) -> Instant";
547 FromIso = "fromIso", "Instant.fromIso(s: String) -> Option[Instant]";
548 }
549BytesStatic {
551 FromUtf8 = "fromUtf8", "Bytes.fromUtf8(s: String) -> Bytes";
552 FromBase64 = "fromBase64", "Bytes.fromBase64(s: String) -> Option[Bytes]";
553 FromHex = "fromHex", "Bytes.fromHex(s: String) -> Option[Bytes]";
554 FromBase64Url = "fromBase64Url", "Bytes.fromBase64Url(s: String) -> Option[Bytes]";
555 Empty = "empty", "Bytes.empty() -> Bytes";
556 }
557TokenClaimsStatic {
559 Of = "of", "TokenClaims.of(lines: List[String]) -> TokenClaims";
560 }
561RequestHeadersStatic {
563 Of = "of", "RequestHeaders.of(lines: List[String]) -> RequestHeaders";
564 Empty = "empty", "RequestHeaders.empty() -> RequestHeaders";
565 }
566RequestQueryStatic {
568 Of = "of", "RequestQuery.of(pairs: List[String]) -> RequestQuery";
569 Empty = "empty", "RequestQuery.empty() -> RequestQuery";
570 }
571RequestCookiesStatic {
573 Of = "of", "RequestCookies.of(pairs: List[String]) -> RequestCookies";
574 Empty = "empty", "RequestCookies.empty() -> RequestCookies";
575 }
576StreamStatic {
578 Of = "of", "Stream.of(items: List[T]) -> Stream[T]";
579 }
580JsonStatic {
582 Encode = "encode", "Json.encode(value: T) -> String";
583 EncodePlain = "encodePlain", "Json.encodePlain(value: T) -> String";
584 Decode = "decode", "Json.decode[T](s: String) -> Result[T, DecodeError]";
585 DecodeSql = "decodeSql", "Json.decodeSql[T](row: String) -> Result[T, DecodeError]";
586 }
587YamlStatic {
589 Decode = "decode", "Yaml.decode[T](s: String) -> Result[T, DecodeError]";
590 }
591FormStatic {
593 Encode = "encode", "Form.encode(value: T) -> String";
594 Decode = "decode", "Form.decode[T](s: String) -> Result[T, DecodeError]";
595 }
596TestConnectionStatic {
598 New = "new", "TestConnection[F]() -> TestConnection[F]";
599 }
600TestRunStatic {
602 Id = "id", "run.id -> String";
603 }
604}
605
606#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
611pub enum IntrinsicNs {
612 Json,
613 Yaml,
614 Form,
615 List,
616 Map,
617 Set,
618 Int,
619 Float,
620 Decimal,
621 Duration,
622 Instant,
623 Bytes,
624 TokenClaims,
625 RequestHeaders,
626 RequestQuery,
627 RequestCookies,
628 Stream,
629 HttpResult,
631 QueueResult,
633 TestConnection,
634 TestRun,
636}
637
638impl IntrinsicNs {
639 pub const fn name(self) -> &'static str {
641 use crate::builtin_names::types as t;
642 match self {
643 IntrinsicNs::Json => t::JSON,
644 IntrinsicNs::Yaml => t::YAML,
645 IntrinsicNs::Form => t::FORM,
646 IntrinsicNs::List => t::LIST,
647 IntrinsicNs::Map => t::MAP,
648 IntrinsicNs::Set => t::SET,
649 IntrinsicNs::Int => t::INT,
650 IntrinsicNs::Float => t::FLOAT,
651 IntrinsicNs::Decimal => t::DECIMAL,
652 IntrinsicNs::Duration => t::DURATION,
653 IntrinsicNs::Instant => t::INSTANT,
654 IntrinsicNs::Bytes => t::BYTES,
655 IntrinsicNs::TokenClaims => t::TOKEN_CLAIMS,
656 IntrinsicNs::RequestHeaders => t::REQUEST_HEADERS,
657 IntrinsicNs::RequestQuery => t::REQUEST_QUERY,
658 IntrinsicNs::RequestCookies => t::REQUEST_COOKIES,
659 IntrinsicNs::Stream => t::STREAM,
660 IntrinsicNs::HttpResult => t::HTTP_RESULT,
661 IntrinsicNs::QueueResult => t::QUEUE_RESULT,
662 IntrinsicNs::TestConnection => "TestConnection",
663 IntrinsicNs::TestRun => crate::checker::RUN_ID_NS,
664 }
665 }
666}
667
668#[derive(Debug, Clone, Copy, PartialEq, Eq)]
677pub enum StaticOp {
678 List(ListStatic),
679 Map(MapStatic),
680 Set(SetStatic),
681 Int(IntStatic),
682 Float(FloatStatic),
683 Decimal(DecimalStatic),
684 Duration(DurationStatic),
685 Instant(InstantStatic),
686 Bytes(BytesStatic),
687 TokenClaims(TokenClaimsStatic),
688 RequestHeaders(RequestHeadersStatic),
689 RequestQuery(RequestQueryStatic),
690 RequestCookies(RequestCookiesStatic),
691 Stream(StreamStatic),
692 Json(JsonStatic),
693 Yaml(YamlStatic),
694 Form(FormStatic),
695 HttpResult(bynk_syntax::ast::HttpVariant),
696 QueueResult(bynk_syntax::ast::QueueVariant),
697 TestConnection(TestConnectionStatic),
698 TestRun(TestRunStatic),
699}
700
701impl StaticOp {
702 pub const fn ns(self) -> IntrinsicNs {
704 match self {
705 StaticOp::List(_) => IntrinsicNs::List,
706 StaticOp::Map(_) => IntrinsicNs::Map,
707 StaticOp::Set(_) => IntrinsicNs::Set,
708 StaticOp::Int(_) => IntrinsicNs::Int,
709 StaticOp::Float(_) => IntrinsicNs::Float,
710 StaticOp::Decimal(_) => IntrinsicNs::Decimal,
711 StaticOp::Duration(_) => IntrinsicNs::Duration,
712 StaticOp::Instant(_) => IntrinsicNs::Instant,
713 StaticOp::Bytes(_) => IntrinsicNs::Bytes,
714 StaticOp::TokenClaims(_) => IntrinsicNs::TokenClaims,
715 StaticOp::RequestHeaders(_) => IntrinsicNs::RequestHeaders,
716 StaticOp::RequestQuery(_) => IntrinsicNs::RequestQuery,
717 StaticOp::RequestCookies(_) => IntrinsicNs::RequestCookies,
718 StaticOp::Stream(_) => IntrinsicNs::Stream,
719 StaticOp::Json(_) => IntrinsicNs::Json,
720 StaticOp::Yaml(_) => IntrinsicNs::Yaml,
721 StaticOp::Form(_) => IntrinsicNs::Form,
722 StaticOp::HttpResult(_) => IntrinsicNs::HttpResult,
723 StaticOp::QueueResult(_) => IntrinsicNs::QueueResult,
724 StaticOp::TestConnection(_) => IntrinsicNs::TestConnection,
725 StaticOp::TestRun(_) => IntrinsicNs::TestRun,
726 }
727 }
728
729 pub const fn name(self) -> &'static str {
731 match self {
732 StaticOp::List(o) => o.name(),
733 StaticOp::Map(o) => o.name(),
734 StaticOp::Set(o) => o.name(),
735 StaticOp::Int(o) => o.name(),
736 StaticOp::Float(o) => o.name(),
737 StaticOp::Decimal(o) => o.name(),
738 StaticOp::Duration(o) => o.name(),
739 StaticOp::Instant(o) => o.name(),
740 StaticOp::Bytes(o) => o.name(),
741 StaticOp::TokenClaims(o) => o.name(),
742 StaticOp::RequestHeaders(o) => o.name(),
743 StaticOp::RequestQuery(o) => o.name(),
744 StaticOp::RequestCookies(o) => o.name(),
745 StaticOp::Stream(o) => o.name(),
746 StaticOp::Json(o) => o.name(),
747 StaticOp::Yaml(o) => o.name(),
748 StaticOp::Form(o) => o.name(),
749 StaticOp::HttpResult(v) => v.name,
750 StaticOp::QueueResult(v) => v.name,
751 StaticOp::TestConnection(o) => o.name(),
752 StaticOp::TestRun(o) => o.name(),
753 }
754 }
755
756 pub fn resolve(ns: IntrinsicNs, name: &str) -> Option<StaticOp> {
758 Some(match ns {
759 IntrinsicNs::List => StaticOp::List(ListStatic::from_name(name)?),
760 IntrinsicNs::Map => StaticOp::Map(MapStatic::from_name(name)?),
761 IntrinsicNs::Set => StaticOp::Set(SetStatic::from_name(name)?),
762 IntrinsicNs::Int => StaticOp::Int(IntStatic::from_name(name)?),
763 IntrinsicNs::Float => StaticOp::Float(FloatStatic::from_name(name)?),
764 IntrinsicNs::Decimal => StaticOp::Decimal(DecimalStatic::from_name(name)?),
765 IntrinsicNs::Duration => StaticOp::Duration(DurationStatic::from_name(name)?),
766 IntrinsicNs::Instant => StaticOp::Instant(InstantStatic::from_name(name)?),
767 IntrinsicNs::Bytes => StaticOp::Bytes(BytesStatic::from_name(name)?),
768 IntrinsicNs::TokenClaims => StaticOp::TokenClaims(TokenClaimsStatic::from_name(name)?),
769 IntrinsicNs::RequestHeaders => {
770 StaticOp::RequestHeaders(RequestHeadersStatic::from_name(name)?)
771 }
772 IntrinsicNs::RequestQuery => {
773 StaticOp::RequestQuery(RequestQueryStatic::from_name(name)?)
774 }
775 IntrinsicNs::RequestCookies => {
776 StaticOp::RequestCookies(RequestCookiesStatic::from_name(name)?)
777 }
778 IntrinsicNs::Stream => StaticOp::Stream(StreamStatic::from_name(name)?),
779 IntrinsicNs::Json => StaticOp::Json(JsonStatic::from_name(name)?),
780 IntrinsicNs::Yaml => StaticOp::Yaml(YamlStatic::from_name(name)?),
781 IntrinsicNs::Form => StaticOp::Form(FormStatic::from_name(name)?),
782 IntrinsicNs::HttpResult => StaticOp::HttpResult(bynk_syntax::ast::http_variant(name)?),
783 IntrinsicNs::QueueResult => {
784 StaticOp::QueueResult(bynk_syntax::ast::queue_variant(name)?)
785 }
786 IntrinsicNs::TestConnection => {
787 StaticOp::TestConnection(TestConnectionStatic::from_name(name)?)
788 }
789 IntrinsicNs::TestRun => StaticOp::TestRun(TestRunStatic::from_name(name)?),
790 })
791 }
792}
793
794kernel_ops! {
804MapStoreOp {
807 Put = "put", "put(key: K, value: V) -> Effect[()]";
808 Get = "get", "get(key: K) -> Effect[Option[V]]";
809 Remove = "remove", "remove(key: K) -> Effect[()]";
810 Contains = "contains", "contains(key: K) -> Effect[Bool]";
811 Size = "size", "size() -> Effect[Int]";
812 Update = "update", "update(key: K, f: (V) -> V) -> Effect[()]";
813 Upsert = "upsert", "upsert(key: K, initial: V, f: (V) -> V) -> Effect[()]";
814 }
815SetStoreOp {
817 Add = "add", "add(item: T) -> Effect[()]";
818 Remove = "remove", "remove(item: T) -> Effect[()]";
819 Contains = "contains", "contains(item: T) -> Effect[Bool]";
820 Size = "size", "size() -> Effect[Int]";
821 }
822CellStoreOp {
824 Update = "update", "update(f: (T) -> T) -> Effect[()]";
825 }
826LogStoreOp {
828 Append = "append", "append(entry: T) -> Effect[()]";
829 }
830LogWindowOp {
832 Since = "since", "since(start: Instant) -> Query[T]";
833 Before = "before", "before(end: Instant) -> Query[T]";
834 Between = "between", "between(start: Instant, end: Instant) -> Query[T]";
835 Recent = "recent", "recent(count: Int) -> Query[T]";
836 Reversed = "reversed", "reversed() -> Query[T]";
837 }
838}
839
840#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
843pub enum StoreEntryOp {
844 Map(MapStoreOp),
845 Cache(MapStoreOp),
846 Set(SetStoreOp),
847 Cell(CellStoreOp),
848 Log(LogStoreOp),
849}
850
851impl StoreEntryOp {
852 pub const fn name(self) -> &'static str {
854 match self {
855 StoreEntryOp::Map(o) | StoreEntryOp::Cache(o) => o.name(),
856 StoreEntryOp::Set(o) => o.name(),
857 StoreEntryOp::Cell(o) => o.name(),
858 StoreEntryOp::Log(o) => o.name(),
859 }
860 }
861
862 pub fn mutates(self) -> bool {
865 crate::store_ops::is_mutating(self.kind_head(), self.name())
866 }
867
868 const fn kind_head(self) -> &'static str {
870 match self {
871 StoreEntryOp::Map(_) => "Map",
872 StoreEntryOp::Cache(_) => "Cache",
873 StoreEntryOp::Set(_) => "Set",
874 StoreEntryOp::Cell(_) => "Cell",
875 StoreEntryOp::Log(_) => "Log",
876 }
877 }
878}
879
880#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
884pub enum StoreQueryOp {
885 Query(QueryOp),
886 LogWindow(LogWindowOp),
887}
888
889impl StoreQueryOp {
890 pub const fn name(self) -> &'static str {
892 match self {
893 StoreQueryOp::Query(o) => o.name(),
894 StoreQueryOp::LogWindow(o) => o.name(),
895 }
896 }
897}
898
899#[cfg(test)]
900mod tests {
901 use super::*;
902
903 macro_rules! round_trips {
904 ($($Enum:ident),* $(,)?) => {$(
905 for op in $Enum::ALL {
906 assert_eq!($Enum::from_name(op.name()), Some(*op), "{op:?}");
908 }
909 assert!($Enum::ALL.len() >= $Enum::TABLE.len());
910 )*};
911 }
912
913 #[test]
914 fn every_operation_round_trips_through_its_spelling() {
915 round_trips!(
916 ListOp,
917 QueryOp,
918 StreamOp,
919 ConnectionOp,
920 TestConnectionOp,
921 MapOp,
922 SetOp,
923 IntOp,
924 FloatOp,
925 DurationOp,
926 InstantOp,
927 RequestHeadersOp,
928 HttpResultOp,
929 RequestQueryOp,
930 RequestCookiesOp,
931 TokenClaimsOp,
932 BytesOp,
933 DecimalOp,
934 StringOp,
935 OptionOp,
936 ResultOp,
937 EffectOp,
938 EffectResultOp,
939 ListStatic,
940 MapStatic,
941 SetStatic,
942 IntStatic,
943 FloatStatic,
944 DecimalStatic,
945 DurationStatic,
946 InstantStatic,
947 BytesStatic,
948 TokenClaimsStatic,
949 RequestHeadersStatic,
950 RequestQueryStatic,
951 RequestCookiesStatic,
952 StreamStatic,
953 JsonStatic,
954 YamlStatic,
955 FormStatic,
956 TestConnectionStatic,
957 TestRunStatic,
958 MapStoreOp,
959 SetStoreOp,
960 CellStoreOp,
961 LogStoreOp,
962 LogWindowOp,
963 );
964 }
965
966 fn signature(table: &[KernelMethod], name: &str) -> &'static str {
968 table
969 .iter()
970 .find(|m| m.name == name)
971 .unwrap_or_else(|| panic!("no `{name}` in the table"))
972 .signature
973 }
974
975 #[test]
976 fn equi_joins_display_their_two_key_functions() {
977 for (table, kind) in [(ListOp::TABLE, "List"), (QueryOp::TABLE, "Query")] {
981 assert_eq!(
982 signature(table, "joinOn"),
983 format!(
984 "joinOn(other: {kind}[U], left: T -> K, right: U -> K, into: (T, U) -> V) -> {kind}[V]"
985 ),
986 );
987 assert_eq!(
988 signature(table, "leftJoin"),
989 format!(
990 "leftJoin(other: {kind}[U], left: T -> K, right: U -> K, into: (T, Option[U]) -> V) -> {kind}[V]"
991 ),
992 );
993 assert!(signature(table, "join").contains("on: (T, U) -> Bool"));
994 }
995 }
996
997 #[test]
998 fn an_unknown_spelling_resolves_to_nothing() {
999 assert_eq!(ListOp::from_name("nope"), None);
1000 assert_eq!(
1001 KernelOp::for_base(bynk_syntax::ast::BaseType::Bool, "length"),
1002 None
1003 );
1004 }
1005}