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
//! Input paramters for [Instruction] execution and means to
//! generally [parse] and [resolve] them.
//!
//! [Instruction]: super::Instruction
//! [parse]: Parse::parse
//! [resolve]: Args::resolve

use crate::{
    error::ResolveError,
    pointer::{Await, AwaitResult, ERR_BRANCH, OK_BRANCH, PTR_BRANCH},
    task, Error, Pointer,
};
use async_recursion::async_recursion;
use futures::{future, future::BoxFuture};
use libipld::{serde::from_ipld, Cid, Ipld};
use serde::{Deserialize, Serialize};
use std::{collections::btree_map::BTreeMap, result::Result, sync::Arc};

mod parse;
pub use parse::{Parse, Parsed};

/// A list of ordered [Input] arguments/parameters.
#[derive(Clone, Debug, PartialEq)]
pub struct Args<T>(Vec<Input<T>>);

impl<T> Args<T>
where
    T: std::fmt::Debug,
{
    /// Create an [Args] [Vec]-type.
    pub fn new(args: Vec<Input<T>>) -> Self {
        Self(args)
    }

    /// Return wrapped [Vec] of [inputs].
    ///
    /// [inputs]: Input
    pub fn into_inner(self) -> Vec<Input<T>> {
        self.0
    }

    /// Return refeerence to a wrapped [Vec] of [inputs].
    ///
    /// [inputs]: Input
    pub fn inner(&self) -> &Vec<Input<T>> {
        &self.0
    }

    /// Return *only* deferred/awaited inputs.
    pub fn deferreds(&self) -> impl Iterator<Item = Cid> + '_ {
        self.0.iter().filter_map(|input| {
            if let Input::Deferred(awaited_promise) = input {
                Some(awaited_promise.instruction_cid())
            } else {
                None
            }
        })
    }

    /// Return *only* [Ipld::Link] [Cid]s.
    pub fn links(&self) -> impl Iterator<Item = Cid> + '_ {
        self.0.iter().filter_map(|input| {
            if let Input::Ipld(Ipld::Link(link)) = input {
                Some(link.to_owned())
            } else {
                None
            }
        })
    }

    /// Resolve [awaited promises] of [inputs] into task-specific [Input::Arg]'s,
    /// given a successful lookup function; otherwise, return [Input::Deferred]
    /// for unresolved promises, or just return [Input::Ipld],
    /// [resolving Ipld links] if the lookup function expected [Ipld] input data.
    ///
    /// [awaited promises]: Await
    /// [inputs]: Input
    /// [resolving Ipld links]: resolve_links
    pub async fn resolve<'a, F>(self, lookup_fn: F) -> Result<Self, ResolveError>
    where
        F: Fn(Cid) -> BoxFuture<'a, Result<task::Result<T>, ResolveError>> + Clone + Send + Sync,
        Ipld: From<T>,
    {
        let inputs = resolve_args(self.0, lookup_fn);
        Ok(Args(inputs.await))
    }
}

impl<T> From<Args<T>> for Ipld
where
    Ipld: From<T>,
{
    fn from(args: Args<T>) -> Self {
        let args = args.0.into_iter().map(|v| v.into());
        Ipld::List(args.collect())
    }
}

impl<T> TryFrom<Ipld> for Args<T>
where
    task::Result<T>: TryFrom<Ipld>,
{
    type Error = Error<T>;

    fn try_from(ipld: Ipld) -> Result<Self, Self::Error> {
        if let Ipld::List(vec) = ipld {
            let args = vec
                .into_iter()
                .fold(Vec::<Input<T>>::new(), |mut acc, ipld| {
                    if let Ok(invocation_result) = task::Result::try_from(ipld.to_owned()) {
                        acc.push(Input::Arg(invocation_result));
                    } else if let Ok(await_result) = Await::try_from(ipld.to_owned()) {
                        acc.push(Input::Deferred(await_result));
                    } else {
                        acc.push(Input::Ipld(ipld))
                    }

                    acc
                });
            Ok(Args(args))
        } else {
            Err(Error::not_an_ipld_list())
        }
    }
}

/// Contains parameters expected by the [URI]/[Ability] pair.
///
/// Left to the executor to define the shape of this data, per job.
///
/// [URI]: [url::Url]
/// [Ability]: super::Ability
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub enum Input<T> {
    /// [Ipld] Literals.
    Ipld(Ipld),
    /// Promise-[links] awaiting the output of another [Instruction]'s
    /// invocation, directly.
    ///
    /// [links]: Pointer
    /// [Instruction]: super::Instruction
    Deferred(Await),
    /// General argument, wrapping an [task::Result] over a task-specific
    /// implementation's own input type(s).
    Arg(task::Result<T>),
}

impl<T> Input<T> {
    /// Resolve [awaited promise] of an [Input] into a task-specific
    /// [Input::Arg], given a successful lookup function; otherwise, return
    /// [Input::Deferred] for an unresolved promise, or just return
    /// [Input::Ipld], [resolving Ipld links] if the lookup function expected
    /// [Ipld] input data.
    ///
    /// [awaited promises]: Await
    /// [inputs]: Input
    /// [resolving Ipld links]: resolve_links
    pub async fn resolve<'a, F>(self, lookup_fn: F) -> Input<T>
    where
        F: Fn(Cid) -> BoxFuture<'a, Result<task::Result<T>, ResolveError>> + Clone + Send + Sync,
        Ipld: From<T>,
    {
        match self {
            Input::Ipld(ipld) => {
                if let Ok(await_promise) = Await::try_from(&ipld) {
                    if let Ok(func_ret) = lookup_fn(await_promise.instruction_cid()).await {
                        Input::Arg(func_ret)
                    } else {
                        Input::Deferred(await_promise)
                    }
                } else {
                    Input::Ipld(resolve_links(ipld, lookup_fn.into()).await)
                }
            }
            Input::Arg(ref _arg) => self,
            Input::Deferred(await_promise) => {
                if let Ok(func_ret) = lookup_fn(await_promise.instruction_cid()).await {
                    Input::Arg(func_ret)
                } else {
                    Input::Deferred(await_promise)
                }
            }
        }
    }
}

impl<T> From<Input<T>> for Ipld
where
    Ipld: From<T>,
{
    fn from(input: Input<T>) -> Self {
        match input {
            Input::Ipld(ipld) => ipld,
            Input::Deferred(promise) => Await::into(promise),
            Input::Arg(arg) => arg.into(),
        }
    }
}

impl<T> From<Await> for Input<T> {
    fn from(await_promise: Await) -> Self {
        Input::Deferred(await_promise)
    }
}

impl<T> TryFrom<Ipld> for Input<T>
where
    T: From<Ipld>,
{
    type Error = Error<String>;

    fn try_from(ipld: Ipld) -> Result<Self, Self::Error> {
        let Ok(map) = from_ipld::<BTreeMap<String, Ipld>>(ipld.to_owned()) else {
            if let Ok(invocation_result) = ipld.to_owned().try_into() {
                return Ok(Input::Arg(invocation_result));
            } else {
                return Ok(Input::Ipld(ipld));
            }
        };

        map.get_key_value(OK_BRANCH)
            .or_else(|| map.get_key_value(ERR_BRANCH))
            .or_else(|| map.get_key_value(PTR_BRANCH))
            .map_or(
                if let Ok(invocation_result) = task::Result::try_from(ipld.to_owned()) {
                    Ok(Input::Arg(invocation_result))
                } else {
                    Ok(Input::Ipld(ipld))
                },
                |(branch, ipld)| {
                    let instruction = Pointer::try_from(ipld)?;
                    Ok(Input::Deferred(Await::new(
                        instruction,
                        AwaitResult::result(branch)
                            .ok_or_else(|| Error::InvalidDiscriminant(branch.to_string()))?,
                    )))
                },
            )
    }
}

async fn resolve_args<'a, T, F>(args: Vec<Input<T>>, lookup_fn: F) -> Vec<Input<T>>
where
    F: Fn(Cid) -> BoxFuture<'a, Result<task::Result<T>, ResolveError>> + Clone + Send + Sync,
    Ipld: From<T>,
{
    let args = args.into_iter().map(|v| v.resolve(lookup_fn.clone()));
    future::join_all(args).await.into_iter().collect()
}

/// Resolve [awaited promises] for *only* [Ipld] data, given a lookup function.
///
/// [awaited promises]: Await
#[async_recursion]
pub async fn resolve_links<'a, T, F>(ipld: Ipld, lookup_fn: Arc<F>) -> Ipld
where
    F: Fn(Cid) -> BoxFuture<'a, Result<task::Result<T>, ResolveError>> + Clone + Sync + Send,
    Ipld: From<T>,
{
    match ipld {
        Ipld::Map(m) => {
            let futures = m.into_iter().map(|(k, v)| async {
                match v {
                    Ipld::Link(cid) => {
                        let mut f = Arc::clone(&lookup_fn);
                        if let Ok(func_ret) = Arc::make_mut(&mut f)(cid).await {
                            if k.eq(PTR_BRANCH) {
                                (k, func_ret.into())
                            } else {
                                (k, func_ret.into_inner().into())
                            }
                        } else {
                            (k, v)
                        }
                    }
                    Ipld::Map(ref m) => {
                        let resolved = resolve_links(Ipld::Map(m.clone()), lookup_fn.clone()).await;
                        (k, resolved)
                    }
                    Ipld::List(ref l) => {
                        let resolved =
                            resolve_links(Ipld::List(l.clone()), lookup_fn.clone()).await;
                        (k, resolved)
                    }
                    _ => (k, v),
                }
            });
            let resolved_results = future::join_all(futures).await;
            Ipld::Map(
                resolved_results
                    .into_iter()
                    .collect::<BTreeMap<String, Ipld>>(),
            )
        }
        Ipld::List(l) => {
            let futures = l.into_iter().map(|v| async {
                match v {
                    Ipld::Link(cid) => {
                        let mut f = Arc::clone(&lookup_fn);
                        if let Ok(func_ret) = Arc::make_mut(&mut f)(cid).await {
                            func_ret.into_inner().into()
                        } else {
                            v
                        }
                    }
                    Ipld::Map(ref m) => {
                        resolve_links(Ipld::Map(m.clone()), lookup_fn.clone()).await
                    }
                    Ipld::List(ref l) => {
                        resolve_links(Ipld::List(l.clone()), lookup_fn.clone()).await
                    }
                    _ => v,
                }
            });
            let resolved_results = future::join_all(futures).await;
            Ipld::List(resolved_results)
        }
        Ipld::Link(link) => {
            let mut f = Arc::clone(&lookup_fn);
            if let Ok(func_ret) = Arc::make_mut(&mut f)(link).await {
                func_ret.into_inner().into()
            } else {
                Ipld::Link(link)
            }
        }
        _ => ipld,
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use crate::{test_utils, Unit};

    #[test]
    fn input_ipld_ipld_rountrip() {
        let input: Input<Unit> = Input::Ipld(Ipld::List(vec![Ipld::Bool(true)]));
        let ipld = Ipld::from(input.clone());

        assert_eq!(ipld, Ipld::List(vec![Ipld::Bool(true)]));
        assert_eq!(input, ipld.try_into().unwrap());
    }

    #[test]
    fn input_deferred_ipld_rountrip() {
        let instruction = test_utils::instruction::<Unit>();
        let ptr: Pointer = instruction.try_into().unwrap();
        let input: Input<Unit> = Input::Deferred(Await::new(ptr.clone(), AwaitResult::Ptr));
        let ipld = Ipld::from(input.clone());

        assert_eq!(
            ipld,
            Ipld::Map(BTreeMap::from([(PTR_BRANCH.into(), Ipld::Link(ptr.cid()))]))
        );
        assert_eq!(input, ipld.try_into().unwrap());
    }

    #[test]
    fn input_arg_ipld_rountrip() {
        let input: Input<Ipld> = Input::Arg(task::Result::Just(Ipld::Bool(false)));
        let ipld = Ipld::from(input.clone());

        assert_eq!(
            ipld,
            Ipld::List(vec![Ipld::String("just".into()), Ipld::Bool(false)])
        );
        assert_eq!(input, ipld.try_into().unwrap());
    }

    #[test]
    fn args_ipld_rountrip() {
        let input: Input<Unit> = Input::Ipld(Ipld::Bool(true));
        let args = Args::new(vec![input]);
        let ipld = Ipld::from(args.clone());

        assert_eq!(ipld, Ipld::List(vec![Ipld::Bool(true)]));
        assert_eq!(args, ipld.try_into().unwrap());
    }

    #[test]
    fn ser_de_ipld() {
        let input: Input<Unit> = Input::Ipld(Ipld::Bool(true));
        let ser = serde_json::to_string(&input).unwrap();
        let de = serde_json::from_str(&ser).unwrap();

        assert_eq!(input, de);
    }

    #[test]
    fn ser_de_arg_ipld() {
        let input: Input<Ipld> = Input::Arg(task::Result::Just(Ipld::Bool(false)));
        let ser = serde_json::to_string(&input).unwrap();
        let de = serde_json::from_str(&ser).unwrap();

        assert_eq!(input, de);
    }
}