Skip to main content

dataflow_rs/function/
spec.rs

1//! `FunctionSpec`: what a function says about itself, as data.
2
3use crate::expr::FieldSpec;
4use std::borrow::Cow;
5
6/// Is running a function twice the same as running it once?
7#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
8#[non_exhaustive]
9pub enum RetrySafety {
10    /// A repeat has the same effect as one run.
11    Idempotent,
12    /// A repeat has its effect again.
13    NotIdempotent,
14    /// Nothing is claimed.
15    #[default]
16    Unknown,
17}
18
19/// A config field that names another definition, such as a connector.
20#[derive(Clone, Debug, PartialEq, Eq)]
21#[non_exhaustive]
22pub struct Reference {
23    /// The config field holding the name.
24    pub field: Cow<'static, str>,
25    /// What it names, such as `connector`.
26    pub kind: Cow<'static, str>,
27}
28
29impl Reference {
30    /// A reference held in `field`, naming a `kind`.
31    pub fn new(field: impl Into<Cow<'static, str>>, kind: impl Into<Cow<'static, str>>) -> Self {
32        Self {
33            field: field.into(),
34            kind: kind.into(),
35        }
36    }
37}
38
39/// What a function says about itself, for tooling. Data only.
40#[derive(Clone, Debug, Default)]
41#[non_exhaustive]
42pub struct FunctionSpec {
43    /// What the function does.
44    pub description: Cow<'static, str>,
45    /// The group it is listed under.
46    pub category: Cow<'static, str>,
47    /// Whether a retry is safe.
48    pub retry: RetrySafety,
49    /// The config fields that name other definitions.
50    pub references: Vec<Reference>,
51    /// The config's fields, when a plugin declares them at runtime.
52    pub runtime_fields: Option<Vec<FieldSpec>>,
53}
54
55impl FunctionSpec {
56    /// Sets the description.
57    #[must_use]
58    pub fn with_description(mut self, description: impl Into<Cow<'static, str>>) -> Self {
59        self.description = description.into();
60        self
61    }
62
63    /// Sets the category.
64    #[must_use]
65    pub fn with_category(mut self, category: impl Into<Cow<'static, str>>) -> Self {
66        self.category = category.into();
67        self
68    }
69
70    /// Sets the retry safety.
71    #[must_use]
72    pub fn with_retry(mut self, retry: RetrySafety) -> Self {
73        self.retry = retry;
74        self
75    }
76
77    /// Sets the references.
78    #[must_use]
79    pub fn with_references(mut self, references: Vec<Reference>) -> Self {
80        self.references = references;
81        self
82    }
83
84    /// Sets the runtime-declared fields.
85    #[must_use]
86    pub fn with_runtime_fields(mut self, fields: Vec<FieldSpec>) -> Self {
87        self.runtime_fields = Some(fields);
88        self
89    }
90}