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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
//! Resolve a reference to an op.

use async_hash::Hash;
use std::collections::HashSet;
use std::convert::{TryFrom, TryInto};
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;

use async_trait::async_trait;
use destream::de::{self, Decoder, FromStream};
use destream::en::{EncodeMap, Encoder, IntoStream, ToStream};
use futures::{try_join, TryFutureExt};
use log::debug;
use safecast::{CastFrom, CastInto, Match, TryCastFrom, TryCastInto};
use sha2::digest::{Digest, Output};

use tc_error::*;
use tcgeneric::*;

use crate::route::Public;
use crate::scalar::{Link, Scalar, Scope, Value, SELF};
use crate::state::{State, StateType, ToState};
use crate::txn::Txn;

use super::{IdRef, Refer, TCRef};

const PREFIX: PathLabel = path_label(&["state", "scalar", "ref", "op"]);

/// The [`Class`] of an [`OpRef`].
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum OpRefType {
    Get,
    Put,
    Post,
    Delete,
}

impl Class for OpRefType {}

impl NativeClass for OpRefType {
    fn from_path(path: &[PathSegment]) -> Option<Self> {
        if path.len() == 5 && &path[..4] == &PREFIX[..] {
            match path[4].as_str() {
                "get" => Some(Self::Get),
                "put" => Some(Self::Put),
                "post" => Some(Self::Post),
                "delete" => Some(Self::Delete),
                _ => None,
            }
        } else {
            None
        }
    }

    fn path(&self) -> TCPathBuf {
        let suffix = match self {
            Self::Get => "get",
            Self::Put => "put",
            Self::Post => "post",
            Self::Delete => "delete",
        };

        TCPathBuf::from(PREFIX).append(label(suffix))
    }
}

impl fmt::Display for OpRefType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Get => write!(f, "GET Op ref"),
            Self::Put => write!(f, "PUT Op ref"),
            Self::Post => write!(f, "POST Op ref"),
            Self::Delete => write!(f, "DELETE Op ref"),
        }
    }
}

/// The subject of an op.
#[derive(Clone, Eq, PartialEq)]
pub enum Subject {
    Link(Link),
    Ref(IdRef, TCPathBuf),
}

impl Subject {
    pub fn as_class(&self) -> Option<StateType> {
        match self {
            Self::Link(link) => StateType::from_path(link.path()),
            _ => None,
        }
    }

    fn dereference_self(self, path: &TCPathBuf) -> Self {
        match self {
            Self::Ref(id_ref, suffix) if id_ref.id() == &SELF => {
                let mut path = path.clone();
                path.extend(suffix);
                Self::Link(path.into())
            }
            other => other,
        }
    }

    fn reference_self(self, path: &TCPathBuf) -> Self {
        match self {
            Self::Link(link) if link.path().starts_with(path) => {
                Self::Ref(IdRef::from(SELF), link.path()[path.len()..].to_vec().into())
            }
            other => other,
        }
    }

    fn requires(&self, deps: &mut HashSet<Id>) {
        match self {
            Self::Ref(id_ref, _) if id_ref.id() != &SELF => id_ref.requires(deps),
            _ => {}
        }
    }
}

impl<'a, D: Digest> Hash<D> for &'a Subject {
    fn hash(self) -> Output<D> {
        match self {
            Subject::Link(link) => Hash::<D>::hash(link),
            Subject::Ref(id, path) => Hash::<D>::hash((id, path)),
        }
    }
}

impl From<IdRef> for Subject {
    fn from(id_ref: IdRef) -> Self {
        Self::Ref(id_ref, TCPathBuf::default())
    }
}

impl FromStr for Subject {
    type Err = TCError;

    fn from_str(s: &str) -> TCResult<Self> {
        if s.starts_with('$') {
            if let Some(i) = s.find('/') {
                let id_ref = IdRef::from_str(&s[..i])?;
                let path = TCPathBuf::from_str(&s[i..])?;
                Ok(Self::Ref(id_ref, path))
            } else {
                let id_ref = IdRef::from_str(s)?;
                Ok(Self::Ref(id_ref, TCPathBuf::default()))
            }
        } else {
            Link::from_str(s).map(Self::Link)
        }
    }
}

impl From<Subject> for Scalar {
    fn from(subject: Subject) -> Self {
        match subject {
            Subject::Ref(id_ref, path) => Scalar::Tuple(Tuple::from((id_ref.into(), path.into()))),
            Subject::Link(link) => Scalar::Value(link.into()),
        }
    }
}

#[async_trait]
impl FromStream for Subject {
    type Context = ();

    async fn from_stream<D: Decoder>(context: (), d: &mut D) -> Result<Self, D::Error> {
        let subject = String::from_stream(context, d).await?;
        Subject::from_str(&subject).map_err(de::Error::custom)
    }
}

impl<'en> ToStream<'en> for Subject {
    fn to_stream<E: Encoder<'en>>(&'en self, e: E) -> Result<E::Ok, E::Error> {
        match self {
            Self::Link(link) => link.to_stream(e),
            Self::Ref(id_ref, path) if path.is_empty() => id_ref.to_stream(e),
            Self::Ref(id_ref, path) => format!("{}{}", id_ref, path).into_stream(e),
        }
    }
}

impl<'en> IntoStream<'en> for Subject {
    fn into_stream<E: Encoder<'en>>(self, e: E) -> Result<E::Ok, E::Error> {
        match self {
            Self::Link(link) => link.into_stream(e),
            Self::Ref(id_ref, path) if path.is_empty() => id_ref.into_stream(e),
            Self::Ref(id_ref, path) => format!("{}{}", id_ref, path).into_stream(e),
        }
    }
}

impl TryCastFrom<Value> for Subject {
    fn can_cast_from(value: &Value) -> bool {
        match value {
            Value::Link(_) => true,
            Value::String(s) => IdRef::from_str(s).is_ok() || Link::from_str(s).is_ok(),
            Value::Tuple(tuple) => tuple.matches::<(IdRef, TCPathBuf)>(),
            _ => false,
        }
    }

    fn opt_cast_from(value: Value) -> Option<Self> {
        match value {
            Value::Link(link) => Some(Self::Link(link)),
            Value::String(s) => {
                if let Ok(id_ref) = IdRef::from_str(&s) {
                    Some(Self::Ref(id_ref, TCPathBuf::default()))
                } else if let Ok(link) = Link::from_str(&s) {
                    Some(Self::Link(link))
                } else {
                    None
                }
            }
            Value::Tuple(tuple) => tuple
                .opt_cast_into()
                .map(|(id, path)| Self::from((id, path))),
            _ => None,
        }
    }
}

impl TryCastFrom<Scalar> for Subject {
    fn can_cast_from(scalar: &Scalar) -> bool {
        match scalar {
            Scalar::Ref(tc_ref) => match &**tc_ref {
                TCRef::Id(_) => true,
                _ => false,
            },
            Scalar::Value(value) => Self::can_cast_from(value),
            Scalar::Tuple(tuple) => tuple.matches::<(IdRef, TCPathBuf)>(),
            _ => false,
        }
    }

    fn opt_cast_from(scalar: Scalar) -> Option<Self> {
        match scalar {
            Scalar::Ref(tc_ref) => match *tc_ref {
                TCRef::Id(id_ref) => Some(Self::Ref(id_ref, TCPathBuf::default())),
                _ => None,
            },
            Scalar::Value(value) => Self::opt_cast_from(value),
            Scalar::Tuple(tuple) => tuple
                .opt_cast_into()
                .map(|(id, path)| Self::from((id, path))),

            _ => None,
        }
    }
}

impl From<TCPathBuf> for Subject {
    fn from(path: TCPathBuf) -> Self {
        Self::Link(path.into())
    }
}

impl From<Link> for Subject {
    fn from(link: Link) -> Self {
        Self::Link(link)
    }
}

impl From<(IdRef, TCPathBuf)> for Subject {
    fn from(get: (IdRef, TCPathBuf)) -> Self {
        Self::Ref(get.0, get.1)
    }
}

impl TryFrom<Subject> for Link {
    type Error = TCError;

    fn try_from(subject: Subject) -> TCResult<Self> {
        match subject {
            Subject::Link(link) => Ok(link),
            other => Err(TCError::bad_request("expected a Link but found", other)),
        }
    }
}

impl TryFrom<Subject> for TCPathBuf {
    type Error = TCError;

    fn try_from(subject: Subject) -> TCResult<Self> {
        match subject {
            Subject::Link(link) if link.host().is_none() => Ok(link.into_path()),
            other => Err(TCError::bad_request("expected a Path but found", other)),
        }
    }
}

impl fmt::Debug for Subject {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Link(link) => fmt::Debug::fmt(link, f),
            Self::Ref(id_ref, path) if path.is_empty() => fmt::Debug::fmt(id_ref, f),
            Self::Ref(id_ref, path) => write!(f, "subject: {:?} {:?}", id_ref, path),
        }
    }
}

impl fmt::Display for Subject {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Link(link) => fmt::Display::fmt(link, f),
            Self::Ref(id_ref, path) if path.is_empty() => fmt::Display::fmt(id_ref, f),
            Self::Ref(id_ref, path) => write!(f, "{}{}", id_ref, path),
        }
    }
}

/// The data defining a reference to a GET op.
pub type GetRef = (Subject, Scalar);

/// The data defining a reference to a PUT op.
pub type PutRef = (Subject, Scalar, Scalar);

/// The data defining a reference to a POST op.
pub type PostRef = (Subject, Map<Scalar>);

/// The data defining a reference to a DELETE op.
pub type DeleteRef = (Subject, Scalar);

/// A reference to an op.
#[derive(Clone, Eq, PartialEq)]
pub enum OpRef {
    Get(GetRef),
    Put(PutRef),
    Post(PostRef),
    Delete(DeleteRef),
}

impl Instance for OpRef {
    type Class = OpRefType;

    fn class(&self) -> OpRefType {
        use OpRefType as ORT;
        match self {
            Self::Get(_) => ORT::Get,
            Self::Put(_) => ORT::Put,
            Self::Post(_) => ORT::Post,
            Self::Delete(_) => ORT::Delete,
        }
    }
}

impl OpRef {
    pub(crate) fn dereference_subject(self, path: &TCPathBuf) -> Self {
        match self {
            Self::Get((subject, key)) => Self::Get((subject.dereference_self(path), key)),
            Self::Put((subject, key, value)) => {
                Self::Put((subject.dereference_self(path), key, value))
            }
            Self::Post((subject, params)) => Self::Post((subject.dereference_self(path), params)),
            Self::Delete((subject, key)) => Self::Delete((subject.dereference_self(path), key)),
        }
    }
}

#[async_trait]
impl Refer for OpRef {
    fn dereference_self(self, path: &TCPathBuf) -> Self {
        match self {
            Self::Get((subject, key)) => Self::Get((subject.dereference_self(path), key)),
            Self::Put((subject, key, value)) => Self::Put((
                subject.dereference_self(path),
                key,
                value.dereference_self(path),
            )),
            Self::Post((subject, params)) => {
                if let Scalar::Map(params) = Scalar::Map(params).dereference_self(path) {
                    Self::Post((subject.dereference_self(path), params))
                } else {
                    panic!("Scalar::Map::dereference_self did not return a Scalar::Map")
                }
            }
            Self::Delete((subject, key)) => Self::Delete((subject.dereference_self(path), key)),
        }
    }

    fn is_conditional(&self) -> bool {
        match self {
            Self::Get((_, key)) => key.is_conditional(),
            Self::Put((_, key, value)) => key.is_conditional() || value.is_conditional(),
            Self::Post((_, params)) => params.values().any(|scalar| scalar.is_conditional()),
            Self::Delete((_, key)) => key.is_conditional(),
        }
    }

    fn is_inter_service_write(&self, cluster_path: &[PathSegment]) -> bool {
        let subject = match self {
            Self::Put((Subject::Link(link), _, _)) => Some(link),
            Self::Delete((Subject::Link(link), _)) => Some(link),
            _ => None,
        };

        if let Some(link) = subject {
            !link.path().starts_with(cluster_path)
        } else {
            false
        }
    }

    fn reference_self(self, path: &TCPathBuf) -> Self {
        match self {
            Self::Get((subject, key)) => Self::Get((subject.reference_self(path), key)),
            Self::Put((subject, key, value)) => Self::Put((
                subject.reference_self(path),
                key,
                value.reference_self(path),
            )),
            Self::Post((subject, params)) => {
                let params = if let Scalar::Map(params) = Scalar::Map(params).reference_self(path) {
                    params
                } else {
                    panic!("Scalar::Map::reference_self did not return a Scalar::Map")
                };

                Self::Post((subject.reference_self(path), params))
            }
            Self::Delete((subject, key)) => Self::Delete((subject.reference_self(path), key)),
        }
    }

    fn requires(&self, deps: &mut HashSet<Id>) {
        match self {
            Self::Get((subject, key)) => {
                subject.requires(deps);
                key.requires(deps);
            }
            Self::Put((subject, key, value)) => {
                subject.requires(deps);
                key.requires(deps);
                value.requires(deps);
            }
            Self::Post((subject, params)) => {
                subject.requires(deps);
                for param in params.values() {
                    param.requires(deps);
                }
            }
            Self::Delete((subject, key)) => {
                subject.requires(deps);
                key.requires(deps);
            }
        }
    }

    async fn resolve<'a, T: ToState + Instance + Public>(
        self,
        context: &'a Scope<'a, T>,
        txn: &'a Txn,
    ) -> TCResult<State> {
        debug!("OpRef::resolve {} from context {:?}", self, context);

        #[inline]
        fn invalid_key<'a, T>(subject: &'a T) -> impl FnOnce(&State) -> TCError + 'a
        where
            T: fmt::Display + 'a,
        {
            move |v| TCError::unsupported(format!("{} is not a valid key for {}", v, subject))
        }

        match self {
            Self::Get((subject, key)) => match subject {
                Subject::Link(link) => {
                    let key = resolve(key, context, txn).await?;
                    let key = key.try_cast_into(invalid_key(&link))?;
                    txn.get(link, key).await
                }
                Subject::Ref(id_ref, path) => {
                    let key = resolve(key, context, txn).await?;
                    let key = key.try_cast_into(invalid_key(&id_ref))?;
                    context.resolve_get(txn, id_ref.id(), &path, key).await
                }
            },
            Self::Put((subject, key, value)) => match subject {
                Subject::Link(link) => {
                    let key = resolve(key, context, txn).await?;
                    let key = key.try_cast_into(invalid_key(&link))?;

                    let value = resolve(value, context, txn).await?;

                    txn.put(link, key, value)
                        .map_ok(|()| State::default())
                        .await
                }
                Subject::Ref(id_ref, path) => {
                    let key = resolve(key, context, txn);
                    let value = resolve(value, context, txn);
                    let (key, value) = try_join!(key, value)?;

                    let key = key.try_cast_into(invalid_key(&id_ref))?;

                    context
                        .resolve_put(txn, id_ref.id(), &path, key, value)
                        .map_ok(|()| State::default())
                        .await
                }
            },
            Self::Post((subject, params)) => match subject {
                Subject::Link(link) => {
                    let params = resolve(Scalar::Map(params), context, txn).await?;
                    txn.post(link, params).await
                }
                Subject::Ref(id_ref, path) => {
                    let params = resolve(Scalar::Map(params), context, txn).await?;
                    let params = params.try_into()?;
                    context.resolve_post(txn, id_ref.id(), &path, params).await
                }
            },
            Self::Delete((subject, key)) => match subject {
                Subject::Link(link) => {
                    let key = resolve(key, context, txn).await?;
                    let key = key.try_cast_into(invalid_key(&link))?;
                    txn.delete(link, key).map_ok(|()| State::default()).await
                }
                Subject::Ref(id_ref, path) => {
                    let key = resolve(key, context, txn).await?;
                    let key = key.try_cast_into(invalid_key(&id_ref))?;

                    context
                        .resolve_delete(txn, id_ref.id(), &path, key)
                        .map_ok(|()| State::default())
                        .await
                }
            },
        }
    }
}

impl<'a, D: Digest> Hash<D> for &'a OpRef {
    fn hash(self) -> Output<D> {
        match self {
            OpRef::Get(get) => Hash::<D>::hash(get),
            OpRef::Put(put) => Hash::<D>::hash(put),
            OpRef::Post((subject, params)) => Hash::<D>::hash((subject, params.deref())),
            OpRef::Delete(delete) => Hash::<D>::hash(delete),
        }
    }
}

impl CastFrom<OpRef> for Tuple<Scalar> {
    fn cast_from(value: OpRef) -> Self {
        match value {
            OpRef::Get((subject, key)) => (subject.into(), key).into(),
            OpRef::Put((subject, key, value)) => (subject.into(), key, value).into(),
            OpRef::Post((subject, params)) => (subject.into(), params.into()).cast_into(),
            OpRef::Delete((subject, key)) => (subject.into(), key).cast_into(),
        }
    }
}

pub struct OpRefVisitor;

impl OpRefVisitor {
    pub async fn visit_map_value<A: de::MapAccess>(
        class: OpRefType,
        access: &mut A,
    ) -> Result<OpRef, A::Error> {
        use OpRefType as ORT;

        match class {
            ORT::Get => access.next_value(()).map_ok(OpRef::Get).await,
            ORT::Put => access.next_value(()).map_ok(OpRef::Put).await,
            ORT::Post => access.next_value(()).map_ok(OpRef::Post).await,
            ORT::Delete => access.next_value(()).map_ok(OpRef::Delete).await,
        }
    }

    pub fn visit_ref_value<E: de::Error>(subject: Subject, params: Scalar) -> Result<OpRef, E> {
        debug!("OpRefVisitor::visit_ref_value {} {}", subject, params);

        match params {
            Scalar::Map(params) => Ok(OpRef::Post((subject, params))),
            Scalar::Tuple(mut tuple) if tuple.len() == 1 => {
                let key = tuple.pop().unwrap();
                Ok(OpRef::Get((subject, key)))
            }
            Scalar::Tuple(mut tuple) if tuple.len() == 2 => {
                if let Some(class) = subject.as_class() {
                    const ERR_IMMUTABLE: &str =
                        "a scalar is immutable and cannot contain a mutable type";
                    const HINT: &str = "(consider using a closure)";

                    if let StateType::Chain(ct) = class {
                        return Err(E::custom(format!(
                            "{} {}: {} {}",
                            ERR_IMMUTABLE, ct, tuple, HINT
                        )));
                    } else if let StateType::Collection(ct) = class {
                        return Err(E::custom(format!(
                            "{} {}: {} {}",
                            ERR_IMMUTABLE, ct, tuple, HINT
                        )));
                    }
                }

                let value = tuple.pop().unwrap();
                let key = tuple.pop().unwrap();

                if subject == Subject::Link(OpRefType::Delete.path().into()) {
                    let subject =
                        key.try_cast_into(|k| E::invalid_type(k, "a Link or Id reference"))?;

                    Ok(OpRef::Delete((subject, value)))
                } else {
                    Ok(OpRef::Put((subject, key, value)))
                }
            }
            other => {
                debug!(
                    "invalid parameters for method call on {:?}: {:?}",
                    subject, other
                );
                Err(de::Error::invalid_value(other, "OpRef parameters"))
            }
        }
    }
}

#[async_trait]
impl de::Visitor for OpRefVisitor {
    type Value = OpRef;

    fn expecting() -> &'static str {
        "an OpRef, e.g. {\"$subject\": [\"key\"]}"
    }

    async fn visit_map<A: de::MapAccess>(self, mut access: A) -> Result<Self::Value, A::Error> {
        let subject = access
            .next_key::<Subject>(())
            .await?
            .ok_or_else(|| de::Error::custom("expected OpRef, found empty map"))?;

        if let Subject::Link(link) = &subject {
            if link.host().is_none() {
                if let Some(class) = OpRefType::from_path(link.path()) {
                    return Self::visit_map_value(class, &mut access).await;
                }
            }
        }

        let params = access.next_value(()).await?;
        Self::visit_ref_value(subject, params)
    }
}

#[async_trait]
impl FromStream for OpRef {
    type Context = ();

    async fn from_stream<D: Decoder>(_: (), d: &mut D) -> Result<Self, D::Error> {
        d.decode_map(OpRefVisitor).await
    }
}

impl<'en> ToStream<'en> for OpRef {
    fn to_stream<E: Encoder<'en>>(&'en self, e: E) -> Result<E::Ok, E::Error> {
        let mut map = e.encode_map(Some(1))?;

        match self {
            OpRef::Get((path, key)) => map.encode_entry(path.to_string(), (key,))?,
            OpRef::Put((path, key, value)) => map.encode_entry(path.to_string(), (key, value))?,
            OpRef::Post((path, data)) => map.encode_entry(path.to_string(), data.deref())?,
            OpRef::Delete((path, key)) => {
                map.encode_key(OpRefType::Delete.path().to_string())?;
                map.encode_value((path, key))?
            }
        }

        map.end()
    }
}

impl<'en> IntoStream<'en> for OpRef {
    fn into_stream<E: Encoder<'en>>(self, e: E) -> Result<E::Ok, E::Error> {
        let mut map = e.encode_map(Some(1))?;

        match self {
            OpRef::Get((path, key)) => map.encode_entry(path.to_string(), (key,))?,
            OpRef::Put((path, key, value)) => map.encode_entry(path.to_string(), (key, value))?,
            OpRef::Post((path, data)) => map.encode_entry(path.to_string(), data.into_inner())?,
            OpRef::Delete((path, key)) => {
                map.encode_key(OpRefType::Delete.path().to_string())?;
                map.encode_value((path, key))?
            }
        }

        map.end()
    }
}

impl fmt::Debug for OpRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        fmt::Display::fmt(self, f)
    }
}

impl fmt::Display for OpRef {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let class = self.class();

        match self {
            OpRef::Get((link, key)) => write!(f, "{} {}?key={}", class, link, key),
            OpRef::Put((path, key, value)) => {
                write!(f, "{} {}?key={} <- {}", class, path, key, value)
            }
            OpRef::Post((path, params)) => write!(f, "{} {}({})", class, path, params),
            OpRef::Delete((link, key)) => write!(f, "{} {}?key={}", class, link, key),
        }
    }
}

async fn resolve<'a, T, S>(tc_ref: T, context: &'a Scope<'a, S>, txn: &'a Txn) -> TCResult<State>
where
    T: Refer,
    S: ToState + Public + Instance,
{
    let mut state = tc_ref.resolve(context, txn).await?;
    while state.is_ref() {
        state = state.resolve(context, txn).await?;
    }

    Ok(state)
}