flow-lib 0.2.0

Library for implementing Space Operator nodes.
Documentation
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
//! [`CommandTrait`] and command [`builder`].
//!
//! To make a new [`native`][crate::config::CommandType::Native] command:
//! 1. Implement [`CommandTrait`], 2 ways;
//!     - Manually implement it to your types.
//!     - Use [`builder`] helper.
//! 2. Use [`inventory::submit`] with a [`CommandDescription`] to register the command at compile-time.

use crate::{
    CommandType, ValueType,
    config::{
        CmdInputDescription, CmdOutputDescription, Name, ValueSet,
        client::{self, NodeData},
        node::Permissions,
    },
    context::CommandContext,
};
use futures::future::{Either, LocalBoxFuture, OptionFuture};
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, collections::BTreeMap, future::ready};
use uuid::Uuid;
use value::Value;

pub mod builder;

/// Import common types for writing commands.
pub mod prelude {
    pub use crate::{
        CmdInputDescription, CmdInputDescription as Input, CmdOutputDescription,
        CmdOutputDescription as Output, FlowId, Name, ValueSet, ValueType,
        command::{
            CommandDescription, CommandError, CommandTrait, InstructionInfo,
            builder::{BuildResult, BuilderCache, BuilderError, CmdBuilder},
        },
        config::{client::NodeData, node::Permissions},
        context::CommandContext,
        solana::Instructions,
    };
    pub use async_trait::async_trait;
    pub use bytes::Bytes;
    pub use futures::future::Either;
    pub use serde::{Deserialize, Serialize};
    pub use serde_json::Value as JsonValue;
    pub use serde_with::serde_as;
    pub use solana_keypair::Keypair;
    pub use solana_pubkey::Pubkey;
    pub use solana_signature::Signature;
    pub use thiserror::Error as ThisError;
    pub use value::{
        self, Decimal, Value,
        with::{AsDecimal, AsKeypair, AsPubkey, AsSignature},
    };
}

/// Error type of commmands.
pub type CommandError = anyhow::Error;

/// Generic trait for implementing commands.
#[async_trait::async_trait(?Send)]
pub trait CommandTrait: 'static {
    fn r#type(&self) -> CommandType {
        CommandType::Native
    }

    /// Unique name to identify the command.
    fn name(&self) -> Name;

    /// List of inputs that the command can receive.
    fn inputs(&self) -> Vec<CmdInputDescription>;

    /// List of outputs that the command will return.
    fn outputs(&self) -> Vec<CmdOutputDescription>;

    /// Run the command.
    async fn run(&self, ctx: CommandContext, params: ValueSet) -> Result<ValueSet, CommandError>;

    /// Specify if and how would this command output Solana instructions.
    fn instruction_info(&self) -> Option<InstructionInfo> {
        None
    }

    /// Specify requested permissions of this command.
    fn permissions(&self) -> Permissions {
        Permissions::default()
    }

    /// Async `Drop` method.
    async fn destroy(&mut self) {}

    /// Specify how [`form_data`][crate::config::NodeConfig::form_data] are read.
    fn read_form_data(&self, data: serde_json::Value) -> ValueSet {
        let mut result = ValueSet::new();
        for i in self.inputs() {
            if let Some(json) = data.get(&i.name) {
                let value = Value::from(json.clone());
                result.insert(i.name.clone(), value);
            }
        }
        result
    }

    fn node_data(&self) -> NodeData {
        default_node_data(self)
    }
}

/// Specify how to convert inputs into passthrough outputs.
pub fn passthrough_outputs<T: CommandTrait + ?Sized>(cmd: &T, inputs: &ValueSet) -> ValueSet {
    let mut res = ValueSet::new();
    for i in cmd.inputs() {
        if i.passthrough
            && let Some(value) = inputs.get(&i.name)
        {
            if !i.required && matches!(value, Value::Null) {
                continue;
            }

            let value = match i.type_bounds.first() {
                Some(ValueType::Pubkey) => {
                    // keypair could be automatically converted into pubkey
                    // we don't want to passthrough the keypair here, only pubkey
                    value::pubkey::deserialize(value.clone()).map(Into::into)
                }
                _ => Ok(value.clone()),
            }
            .unwrap_or_else(|error| {
                tracing::warn!("error reading passthrough: {}", error);
                value.clone()
            });
            res.insert(i.name, value);
        }
    }
    res
}

/// Specify how [`form_data`][crate::config::NodeConfig::form_data] are read.
pub fn default_read_form_data<T: CommandTrait + ?Sized>(
    cmd: &T,
    data: serde_json::Value,
) -> ValueSet {
    let mut result = ValueSet::new();
    for i in cmd.inputs() {
        if let Some(json) = data.get(&i.name) {
            let value = Value::from(json.clone());
            result.insert(i.name.clone(), value);
        }
    }
    result
}

pub fn default_node_data<T: CommandTrait + ?Sized>(cmd: &T) -> NodeData {
    NodeData {
        r#type: cmd.r#type(),
        node_id: cmd.name(),
        sources: cmd
            .outputs()
            .into_iter()
            .map(|output| client::Source {
                id: Uuid::nil(),
                name: output.name,
                r#type: output.r#type,
                optional: output.optional,
            })
            .collect(),
        targets: cmd
            .inputs()
            .into_iter()
            .map(|input| client::Target {
                id: Uuid::nil(),
                name: input.name,
                type_bounds: input.type_bounds,
                required: input.required,
                passthrough: input.passthrough,
            })
            .collect(),
        targets_form: client::TargetsForm::default(),
        instruction_info: cmd.instruction_info(),
    }
}

pub fn input_is_required<T: CommandTrait + ?Sized>(cmd: &T, name: &str) -> Option<bool> {
    cmd.inputs()
        .into_iter()
        .find_map(|i| (i.name == name).then_some(i.required))
}

pub fn output_is_optional<T: CommandTrait + ?Sized>(cmd: &T, name: &str) -> Option<bool> {
    cmd.outputs()
        .into_iter()
        .find_map(|o| (o.name == name).then_some(o.optional))
        .or_else(|| {
            cmd.inputs()
                .into_iter()
                .find_map(|i| (i.name == name && i.passthrough).then_some(!i.required))
        })
}

/// Specify the order with which a command will return its output:
/// - [`before`][InstructionInfo::before]: list of output names returned before instructions are sent.
/// - [`signature`][InstructionInfo::signature]: name of the signature output port.
/// - [`after`][InstructionInfo::after]: list of output names returned after instructions are sent.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct InstructionInfo {
    pub before: Vec<Name>,
    pub signature: Name,
    pub after: Vec<Name>,
}

impl InstructionInfo {
    /// Simple `InstructionInfo` that can describe most commands:
    /// - [`before`][InstructionInfo::before]: All passthroughs and outputs, except for `signature`.
    /// - [`after`][InstructionInfo::after]: empty.
    pub fn simple<C: CommandTrait>(cmd: &C, signature: &str) -> Self {
        let before = cmd
            .inputs()
            .into_iter()
            .filter(|i| i.passthrough)
            .map(|i| i.name)
            .chain(
                cmd.outputs()
                    .into_iter()
                    .filter(|o| o.name != signature)
                    .map(|o| o.name),
            )
            .collect();
        Self {
            before,
            after: Vec::new(),
            signature: signature.into(),
        }
    }
}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, bincode::Encode)]
pub enum MatchName {
    Exact(Cow<'static, str>),
    Regex(Cow<'static, str>),
}

// generated by bincode macro
impl<__Context> ::bincode::Decode<__Context> for MatchName {
    fn decode<__D: ::bincode::de::Decoder<Context = __Context>>(
        decoder: &mut __D,
    ) -> core::result::Result<Self, ::bincode::error::DecodeError> {
        let variant_index = <u32 as ::bincode::Decode<__D::Context>>::decode(decoder)?;
        match variant_index {
            0u32 => core::result::Result::Ok(Self::Exact(
                ::bincode::Decode::<__D::Context>::decode(decoder)?,
            )),
            1u32 => core::result::Result::Ok(Self::Regex(
                ::bincode::Decode::<__D::Context>::decode(decoder)?,
            )),
            variant => {
                core::result::Result::Err(::bincode::error::DecodeError::UnexpectedVariant {
                    found: variant,
                    type_name: "MatchName",
                    allowed: &::bincode::error::AllowedEnumVariants::Range { min: 0, max: 1 },
                })
            }
        }
    }
}

impl<'de, C> bincode::BorrowDecode<'de, C> for MatchName {
    fn borrow_decode<D: bincode::de::BorrowDecoder<'de, Context = C>>(
        decoder: &mut D,
    ) -> Result<Self, bincode::error::DecodeError> {
        bincode::Decode::decode(decoder)
    }
}

impl std::fmt::Debug for MatchName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Exact(arg0) => arg0.fmt(f),
            Self::Regex(arg0) => {
                f.write_str("/")?;
                f.write_str(arg0)?;
                f.write_str("/")
            }
        }
    }
}

impl std::fmt::Display for MatchName {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MatchName::Exact(cow) => cow.fmt(f),
            MatchName::Regex(cow) => cow.fmt(f),
        }
    }
}

#[derive(Clone, bincode::Encode, bincode::Decode, PartialEq, Eq, PartialOrd, Ord)]
pub struct MatchCommand {
    pub r#type: CommandType,
    pub name: MatchName,
}

impl std::fmt::Debug for MatchCommand {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.r#type.fmt(f)?;
        f.write_str(":")?;
        self.name.fmt(f)
    }
}

impl MatchCommand {
    pub fn is_match(&self, ty: CommandType, name: &str) -> bool {
        self.r#type == ty
            && match &self.name {
                MatchName::Exact(cow) => cow == name,
                MatchName::Regex(cow) => Regex::new(cow) // TODO: slow
                    .map(|re| re.is_match(name))
                    .ok()
                    .unwrap_or(false),
            }
    }
}

pub type FnNewResult = Result<Box<dyn CommandTrait>, CommandError>;

/// Use [`inventory::submit`] to register commands at compile-time.
#[derive(Clone)]
pub struct CommandDescription {
    pub matcher: MatchCommand,
    /// Function to initialize the command from a [`NodeData`].
    pub fn_new:
        Either<fn(&NodeData) -> FnNewResult, fn(&NodeData) -> LocalBoxFuture<'static, FnNewResult>>,
}

impl CommandDescription {
    pub const fn new(name: &'static str, fn_new: fn(&NodeData) -> FnNewResult) -> Self {
        Self {
            matcher: MatchCommand {
                r#type: CommandType::Native,
                name: MatchName::Exact(Cow::Borrowed(name)),
            },
            fn_new: Either::Left(fn_new),
        }
    }
}

inventory::collect!(CommandDescription);

pub fn collect_commands() -> BTreeMap<&'static MatchCommand, &'static CommandDescription> {
    inventory::iter::<CommandDescription>()
        .map(|c| (&c.matcher, c))
        .collect()
}

#[derive(Debug, Clone)]
pub struct CommandIndex<T> {
    pub exact_match: BTreeMap<(CommandType, Cow<'static, str>), T>,
    pub regex: Vec<(CommandType, regex::Regex, T)>,
}

impl<T> Default for CommandIndex<T> {
    fn default() -> Self {
        Self {
            exact_match: <_>::default(),
            regex: <_>::default(),
        }
    }
}

impl<T> FromIterator<(MatchCommand, T)> for CommandIndex<T> {
    fn from_iter<I: IntoIterator<Item = (MatchCommand, T)>>(iter: I) -> Self {
        let mut this = Self::default();
        for (matcher, t) in iter {
            match &matcher.name {
                MatchName::Exact(cow) => {
                    this.exact_match.insert((matcher.r#type, cow.clone()), t);
                }
                MatchName::Regex(cow) => {
                    this.regex
                        .push((matcher.r#type, Regex::new(cow).expect("invalid regex"), t));
                }
            }
        }
        this
    }
}

impl<T> CommandIndex<T> {
    pub fn get(&self, ty: CommandType, name: &str) -> Option<&T> {
        if let Some(d) = self.exact_match.get(&(ty, name.to_owned().into())) {
            Some(d)
        } else {
            let mut matched = None;
            for r in &self.regex {
                if r.0 == ty && r.1.is_match(name) {
                    matched = Some(&r.2);
                }
            }
            matched
        }
    }

    pub fn availables(&self) -> impl Iterator<Item = MatchCommand> {
        self.exact_match
            .keys()
            .cloned()
            .map(|(r#type, name)| MatchCommand {
                r#type,
                name: MatchName::Exact(name),
            })
            .chain(self.regex.iter().map(|(ty, regex, _)| MatchCommand {
                r#type: *ty,
                name: MatchName::Regex(regex.to_string().into()),
            }))
    }
}

#[derive(Clone)]
pub struct CommandFactory {
    index: CommandIndex<&'static CommandDescription>,
}

impl CommandFactory {
    pub fn collect() -> Self {
        Self {
            index: inventory::iter::<CommandDescription>()
                .map(|c| (c.matcher.clone(), c))
                .collect(),
        }
    }

    pub fn init(
        &self,
        nd: &NodeData,
    ) -> impl Future<Output = Result<Option<Box<dyn CommandTrait>>, CommandError>> + 'static {
        let cmd = self.index.get(nd.r#type, &nd.node_id);

        let either = cmd.map(|cmd| match cmd.fn_new {
            Either::Left(fn_new) => Either::Left(ready(fn_new(nd))),
            Either::Right(async_fn_new) => Either::Right(async_fn_new(nd)),
        });
        async move { OptionFuture::from(either).await.transpose() }
    }

    pub fn availables(&self) -> impl Iterator<Item = MatchCommand> {
        self.index.availables()
    }
}