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
use std::ops::Bound;

use safecast::TryCastFrom;

use tc_error::*;
use tc_transact::public::generic::COPY;
use tc_transact::public::helpers::{AttributeHandler, EchoHandler};
use tc_transact::public::value;
use tc_transact::public::{
    ClosureInstance, DeleteHandler, GetHandler, Handler, PostHandler, PutHandler, Route,
    StateInstance,
};
use tc_transact::RPCClient;
use tc_value::{Number, TCString, Value};
use tcgeneric::{Id, Map, PathSegment, TCPathBuf, Tuple};

use crate::{ClusterRef, Refer, Scalar, ScalarType};

impl<State> Route<State> for ScalarType
where
    State: StateInstance,
{
    fn route<'a>(&'a self, _path: &'a [PathSegment]) -> Option<Box<dyn Handler<'a, State> + 'a>> {
        None
    }
}

struct ClusterHandler {
    path: TCPathBuf,
}

impl ClusterHandler {
    fn new(cluster: &ClusterRef, path: &[PathSegment]) -> Self {
        let mut cluster_path = cluster.path().clone();
        cluster_path.extend(path.into_iter().cloned());
        Self { path: cluster_path }
    }
}

impl<'a, State> Handler<'a, State> for ClusterHandler
where
    State: StateInstance,
{
    fn get<'b>(self: Box<Self>) -> Option<GetHandler<'a, 'b, State::Txn, State>>
    where
        'b: 'a,
    {
        Some(Box::new(|txn, key| Box::pin(txn.get(self.path, key))))
    }

    fn put<'b>(self: Box<Self>) -> Option<PutHandler<'a, 'b, State::Txn, State>>
    where
        'b: 'a,
    {
        Some(Box::new(|txn, key, value| {
            Box::pin(txn.put(self.path, key, value))
        }))
    }

    fn post<'b>(self: Box<Self>) -> Option<PostHandler<'a, 'b, State::Txn, State>>
    where
        'b: 'a,
    {
        Some(Box::new(|txn, params| {
            Box::pin(txn.post(self.path, params))
        }))
    }

    fn delete<'b>(self: Box<Self>) -> Option<DeleteHandler<'a, 'b, State::Txn>>
    where
        'b: 'a,
    {
        Some(Box::new(|txn, key| Box::pin(txn.delete(self.path, key))))
    }
}

impl<State> Route<State> for ClusterRef
where
    State: StateInstance,
{
    fn route<'a>(&'a self, path: &'a [PathSegment]) -> Option<Box<dyn Handler<'a, State> + 'a>> {
        Some(Box::new(ClusterHandler::new(self, path)))
    }
}

impl<State> Route<State> for Scalar
where
    State: StateInstance
        + Refer<State>
        + From<Scalar>
        + From<Tuple<Scalar>>
        + From<Map<Scalar>>
        + From<Value>
        + From<Tuple<Value>>
        + From<Number>,
    Box<dyn ClosureInstance<State>>: TryCastFrom<State>,
    Id: TryCastFrom<State>,
    Map<State>: TryFrom<State, Error = TCError> + TryCastFrom<State>,
    Number: TryCastFrom<State>,
    TCString: TryCastFrom<State>,
    Tuple<State>: TryCastFrom<State>,
    Value: TryCastFrom<State>,
{
    fn route<'a>(&'a self, path: &'a [PathSegment]) -> Option<Box<dyn Handler<'a, State> + 'a>> {
        if path == &COPY[..] {
            return Some(Box::new(AttributeHandler::from(self.clone())));
        }

        match self {
            Self::Cluster(cluster) => cluster.route(path),
            Self::Map(map) => map.route(path),
            Self::Op(op_def) if path.is_empty() => Some(Box::new(op_def.clone())),
            Self::Range((start, end)) => {
                if path.is_empty() {
                    None
                } else {
                    match path[0].as_str() {
                        "start" => match start {
                            Bound::Included(value) => value.route(&path[1..]),
                            Bound::Excluded(value) => value.route(&path[1..]),
                            Bound::Unbounded => Value::None.route(&path[1..]),
                        },
                        "end" => match end {
                            Bound::Included(value) => value.route(&path[1..]),
                            Bound::Excluded(value) => value.route(&path[1..]),
                            Bound::Unbounded => Value::None.route(&path[1..]),
                        },
                        _ => None,
                    }
                }
            }
            Self::Ref(_) => None,
            Self::Value(value) => value.route(path),
            Self::Tuple(tuple) => tuple.route(path),
            _ => None,
        }
    }
}

pub struct Static;

impl<State> Route<State> for Static
where
    State: StateInstance,
{
    fn route<'a>(&'a self, path: &'a [PathSegment]) -> Option<Box<dyn Handler<'a, State> + 'a>> {
        if path.is_empty() {
            Some(Box::new(EchoHandler))
        } else if path[0] == value::PREFIX {
            value::Static.route(&path[1..])
        } else {
            None
        }
    }
}