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
use std::collections::HashSet;
use std::fmt;
use async_trait::async_trait;
use destream::{de, en};
use log::debug;
use safecast::{Match, TryCastFrom, TryCastInto};
use tc_error::TCResult;
use tcgeneric::{Id, Instance};
use crate::route::Public;
use crate::scalar::{Scalar, Scope};
use crate::state::State;
use crate::txn::Txn;
use super::{Refer, TCRef};
#[derive(Clone, Eq, PartialEq)]
pub struct After {
when: TCRef,
then: Scalar,
}
#[async_trait]
impl Refer for After {
fn requires(&self, deps: &mut HashSet<Id>) {
self.when.requires(deps);
self.then.requires(deps);
}
async fn resolve<'a, T: Instance + Public>(
self,
context: &'a Scope<'a, T>,
txn: &'a Txn,
) -> TCResult<State> {
debug!("After::resolve {}", self);
self.when.resolve(context, txn).await?;
Ok(self.then.into())
}
}
impl TryCastFrom<Scalar> for After {
fn can_cast_from(scalar: &Scalar) -> bool {
scalar.matches::<(TCRef, Scalar)>()
}
fn opt_cast_from(scalar: Scalar) -> Option<Self> {
scalar
.opt_cast_into()
.map(|(when, then)| Self { when, then })
}
}
#[async_trait]
impl de::FromStream for After {
type Context = ();
async fn from_stream<D: de::Decoder>(context: (), decoder: &mut D) -> Result<Self, D::Error> {
let (when, then) =
<(TCRef, Scalar) as de::FromStream>::from_stream(context, decoder).await?;
Ok(Self { when, then })
}
}
impl<'en> en::IntoStream<'en> for After {
fn into_stream<E: en::Encoder<'en>>(self, encoder: E) -> Result<E::Ok, E::Error> {
(self.when, self.then).into_stream(encoder)
}
}
impl<'en> en::ToStream<'en> for After {
fn to_stream<E: en::Encoder<'en>>(&'en self, encoder: E) -> Result<E::Ok, E::Error> {
en::IntoStream::into_stream((&self.when, &self.then), encoder)
}
}
impl fmt::Display for After {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "after {} then {}", self.when, self.then)
}
}