distributed 4.0.2

CQRS/ES framework for Rust using Plain Old Rust Structs — append-only events, replay, snapshots, outbox, service bus, and pluggable infrastructure
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
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
//! Event-independent mutation expressions over typed inputs.

use serde::Serialize;
use serde_json::Value;

use crate::projection::{
    ProjectionAssignment, ProjectionExpression, ProjectionScalarTransform, ProjectionValue,
    ProjectionValueRef, ProjectionValueType, MAX_PROJECTION_EXPRESSION_DEPTH,
    MAX_PROJECTION_PATH_SEGMENTS,
};

use super::MutationProgramError;

/// Maximum nesting of a mutation expression or literal value.
pub const MAX_MUTATION_EXPRESSION_DEPTH: usize = MAX_PROJECTION_EXPRESSION_DEPTH;

/// Maximum number of segments in a mutation input path.
pub const MAX_MUTATION_PATH_SEGMENTS: usize = MAX_PROJECTION_PATH_SEGMENTS;

/// One ordered object member in a mutation expression.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct MutationExpressionObjectField {
    name: String,
    value: MutationExpression,
}

impl MutationExpressionObjectField {
    /// Return the member name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Return the member expression.
    pub fn value(&self) -> &MutationExpression {
        &self.value
    }
}

/// A bounded, deterministic expression over a bound mutation input object.
///
/// Mutation expressions never read domain events. Portable handlers bind event
/// fields into an input object before evaluation.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct MutationExpression {
    expression: MutationExpressionKind,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
enum MutationExpressionKind {
    InputPath {
        path: Vec<String>,
        value_type: ProjectionValueType,
    },
    Constant {
        value: ProjectionValue,
    },
    Enum {
        enum_type: String,
        variant: String,
    },
    List {
        values: Vec<MutationExpression>,
    },
    Object {
        fields: Vec<MutationExpressionObjectField>,
    },
    Transform {
        transform: ProjectionScalarTransform,
        arguments: Vec<MutationExpression>,
    },
}

/// Field assignment that preserves null versus removal versus unknown.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "kind", content = "expression", rename_all = "snake_case")]
pub enum MutationAssignment {
    /// Evaluate and assign a concrete value (including explicit null).
    Set(MutationExpression),
    /// Explicitly remove the field when the model supports unset.
    Unset,
    /// Field is omitted / unknown in a partial patch input.
    Unknown,
}

/// Result of evaluating a mutation expression against a bound input object.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(tag = "state", content = "value", rename_all = "snake_case")]
pub enum ResolvedMutationValue {
    /// A concrete value; JSON null remains a concrete value.
    Value(ProjectionValue),
    /// The selected optional input did not exist.
    Absent,
    /// The mutation explicitly removes this field.
    Unset,
    /// The field was authored as unknown and must not be written.
    Unknown,
}

impl MutationExpression {
    /// Select a nested property from the bound mutation input.
    ///
    /// # Errors
    ///
    /// Rejects empty path segments and paths beyond the portable limit.
    pub fn input_path<I, S>(
        value_type: ProjectionValueType,
        segments: I,
    ) -> Result<Self, MutationProgramError>
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let path = segments.into_iter().map(Into::into).collect::<Vec<_>>();
        if path.len() > MAX_MUTATION_PATH_SEGMENTS {
            return Err(MutationProgramError::PathTooDeep {
                segments: path.len(),
                max: MAX_MUTATION_PATH_SEGMENTS,
            });
        }
        if path.iter().any(|segment| segment.is_empty()) {
            return Err(MutationProgramError::EmptyName("input path segment"));
        }
        if matches!(&value_type, ProjectionValueType::Enum(name) if name.is_empty()) {
            return Err(MutationProgramError::EmptyName("input enum type"));
        }
        Ok(Self {
            expression: MutationExpressionKind::InputPath { path, value_type },
        })
    }

    /// Embed a validated literal.
    pub fn constant(value: ProjectionValue) -> Self {
        Self {
            expression: MutationExpressionKind::Constant { value },
        }
    }

    /// Embed a typed enum variant.
    ///
    /// # Errors
    ///
    /// Rejects empty enum type or variant names.
    pub fn enum_variant(
        enum_type: impl Into<String>,
        variant: impl Into<String>,
    ) -> Result<Self, MutationProgramError> {
        let enum_type = non_empty(enum_type.into(), "enum type")?;
        let variant = non_empty(variant.into(), "enum variant")?;
        Ok(Self {
            expression: MutationExpressionKind::Enum { enum_type, variant },
        })
    }

    /// Construct a deterministic list from child expressions.
    ///
    /// # Errors
    ///
    /// Rejects expression trees beyond the depth limit.
    pub fn list(values: Vec<Self>) -> Result<Self, MutationProgramError> {
        let expression = Self {
            expression: MutationExpressionKind::List { values },
        };
        expression.validate_depth()?;
        Ok(expression)
    }

    /// Construct an object whose keys are canonicalized lexicographically.
    ///
    /// # Errors
    ///
    /// Rejects empty or duplicate fields and trees beyond the depth limit.
    pub fn object<I, S>(fields: I) -> Result<Self, MutationProgramError>
    where
        I: IntoIterator<Item = (S, Self)>,
        S: Into<String>,
    {
        let mut fields = fields
            .into_iter()
            .map(|(name, value)| {
                Ok(MutationExpressionObjectField {
                    name: non_empty(name.into(), "object field")?,
                    value,
                })
            })
            .collect::<Result<Vec<_>, MutationProgramError>>()?;
        fields.sort_by(|left, right| left.name.cmp(&right.name));
        for pair in fields.windows(2) {
            if pair[0].name == pair[1].name {
                return Err(MutationProgramError::DuplicateName {
                    kind: "object field",
                    name: pair[0].name.clone(),
                });
            }
        }
        let expression = Self {
            expression: MutationExpressionKind::Object { fields },
        };
        expression.validate_depth()?;
        Ok(expression)
    }

    /// Apply one closed, versioned scalar transform.
    ///
    /// # Errors
    ///
    /// Rejects an empty argument list and trees beyond the depth limit.
    pub fn transform(
        transform: ProjectionScalarTransform,
        arguments: Vec<Self>,
    ) -> Result<Self, MutationProgramError> {
        if arguments.is_empty() {
            return Err(MutationProgramError::InvalidOperation {
                operation: "mutation expression".to_owned(),
                reason: "scalar transforms require at least one argument".to_owned(),
            });
        }
        let expression = Self {
            expression: MutationExpressionKind::Transform {
                transform,
                arguments,
            },
        };
        expression.validate_depth()?;
        Ok(expression)
    }

    /// Return whether this expression is a pure constant (no input reads).
    pub fn is_constant(&self) -> bool {
        match &self.expression {
            MutationExpressionKind::Constant { .. } | MutationExpressionKind::Enum { .. } => true,
            MutationExpressionKind::InputPath { .. } => false,
            MutationExpressionKind::List { values } => values.iter().all(Self::is_constant),
            MutationExpressionKind::Object { fields } => {
                fields.iter().all(|field| field.value.is_constant())
            }
            MutationExpressionKind::Transform { arguments, .. } => {
                arguments.iter().all(Self::is_constant)
            }
        }
    }

    /// Borrow the input path segments when this is an input path.
    pub fn as_input_path(&self) -> Option<(&[String], &ProjectionValueType)> {
        match &self.expression {
            MutationExpressionKind::InputPath { path, value_type } => {
                Some((path.as_slice(), value_type))
            }
            _ => None,
        }
    }

    /// Evaluate against a bound JSON input object.
    ///
    /// # Errors
    ///
    /// Returns typed path/type mismatches for unsupported shapes.
    pub fn resolve(&self, input: &Value) -> Result<ResolvedMutationValue, MutationProgramError> {
        match &self.expression {
            MutationExpressionKind::InputPath { path, value_type } => {
                let mut current = input;
                for segment in path {
                    let Value::Object(object) = current else {
                        return Ok(ResolvedMutationValue::Absent);
                    };
                    let Some(next) = object.get(segment) else {
                        return Ok(ResolvedMutationValue::Absent);
                    };
                    current = next;
                }
                let _ = value_type;
                Ok(ResolvedMutationValue::Value(
                    ProjectionValue::try_from_json(current.clone()).map_err(|error| {
                        MutationProgramError::Adapter(format!(
                            "typed input path `{}`: {error}",
                            path.join(".")
                        ))
                    })?,
                ))
            }
            MutationExpressionKind::Constant { value } => {
                Ok(ResolvedMutationValue::Value(value.clone()))
            }
            MutationExpressionKind::Enum { enum_type, variant } => {
                ProjectionExpression::enum_variant(enum_type, variant)
                    .map_err(MutationProgramError::from)?;
                Ok(ResolvedMutationValue::Value(ProjectionValue::string(
                    variant.clone(),
                )))
            }
            MutationExpressionKind::List { values } => {
                let mut items = Vec::with_capacity(values.len());
                for value in values {
                    match value.resolve(input)? {
                        ResolvedMutationValue::Value(resolved) => {
                            items.push(projection_value_to_json(&resolved)?);
                        }
                        ResolvedMutationValue::Absent => {
                            return Ok(ResolvedMutationValue::Absent);
                        }
                        ResolvedMutationValue::Unset | ResolvedMutationValue::Unknown => {
                            return Err(MutationProgramError::InvalidOperation {
                                operation: "list expression".to_owned(),
                                reason: "list members cannot be unset or unknown".to_owned(),
                            });
                        }
                    }
                }
                Ok(ResolvedMutationValue::Value(
                    ProjectionValue::try_from_json(Value::Array(items))
                        .map_err(|error| MutationProgramError::Adapter(error.to_string()))?,
                ))
            }
            MutationExpressionKind::Object { fields } => {
                let mut object = serde_json::Map::new();
                for field in fields {
                    match field.value.resolve(input)? {
                        ResolvedMutationValue::Value(resolved) => {
                            object.insert(field.name.clone(), projection_value_to_json(&resolved)?);
                        }
                        ResolvedMutationValue::Absent => {
                            return Ok(ResolvedMutationValue::Absent);
                        }
                        ResolvedMutationValue::Unset | ResolvedMutationValue::Unknown => {
                            return Err(MutationProgramError::InvalidOperation {
                                operation: "object expression".to_owned(),
                                reason: "object members cannot be unset or unknown".to_owned(),
                            });
                        }
                    }
                }
                Ok(ResolvedMutationValue::Value(
                    ProjectionValue::try_from_json(Value::Object(object))
                        .map_err(|error| MutationProgramError::Adapter(error.to_string()))?,
                ))
            }
            MutationExpressionKind::Transform { .. } => Err(MutationProgramError::Adapter(
                "transform evaluation requires projection rewrite".to_owned(),
            )),
        }
    }

    /// Rewrite this mutation expression into a projection expression using a
    /// binder that maps input roots onto event/envelope/constant expressions.
    ///
    /// # Errors
    ///
    /// Returns adapter failures when a required input binding is missing.
    pub fn rewrite_with(
        &self,
        bind_input_path: &dyn Fn(
            &[String],
            &ProjectionValueType,
        ) -> Result<ProjectionExpression, MutationProgramError>,
    ) -> Result<ProjectionExpression, MutationProgramError> {
        match &self.expression {
            MutationExpressionKind::InputPath { path, value_type } => {
                bind_input_path(path, value_type)
            }
            MutationExpressionKind::Constant { value } => {
                Ok(ProjectionExpression::constant(value.clone()))
            }
            MutationExpressionKind::Enum { enum_type, variant } => {
                ProjectionExpression::enum_variant(enum_type, variant).map_err(Into::into)
            }
            MutationExpressionKind::List { values } => {
                let rewritten = values
                    .iter()
                    .map(|value| value.rewrite_with(bind_input_path))
                    .collect::<Result<Vec<_>, _>>()?;
                ProjectionExpression::list(rewritten).map_err(Into::into)
            }
            MutationExpressionKind::Object { fields } => {
                let rewritten = fields
                    .iter()
                    .map(|field| {
                        Ok((
                            field.name.clone(),
                            field.value.rewrite_with(bind_input_path)?,
                        ))
                    })
                    .collect::<Result<Vec<_>, MutationProgramError>>()?;
                ProjectionExpression::object(rewritten).map_err(Into::into)
            }
            MutationExpressionKind::Transform {
                transform,
                arguments,
            } => {
                let rewritten = arguments
                    .iter()
                    .map(|argument| argument.rewrite_with(bind_input_path))
                    .collect::<Result<Vec<_>, _>>()?;
                ProjectionExpression::transform(*transform, rewritten).map_err(Into::into)
            }
        }
    }

    fn validate_depth(&self) -> Result<(), MutationProgramError> {
        let depth = expression_depth(self, 1);
        if depth > MAX_MUTATION_EXPRESSION_DEPTH {
            return Err(MutationProgramError::ExpressionTooDeep {
                depth,
                max: MAX_MUTATION_EXPRESSION_DEPTH,
            });
        }
        Ok(())
    }
}

impl MutationAssignment {
    /// Construct a set assignment.
    pub fn set(expression: MutationExpression) -> Self {
        Self::Set(expression)
    }

    /// Construct an explicit unset assignment.
    pub fn unset() -> Self {
        Self::Unset
    }

    /// Construct an unknown/omitted assignment.
    pub fn unknown() -> Self {
        Self::Unknown
    }

    /// Rewrite into a projection assignment. Unknown becomes omitted (caller filters).
    ///
    /// # Errors
    ///
    /// Propagates rewrite failures from nested expressions.
    pub fn rewrite_with(
        &self,
        bind_input_path: &dyn Fn(
            &[String],
            &ProjectionValueType,
        ) -> Result<ProjectionExpression, MutationProgramError>,
    ) -> Result<Option<ProjectionAssignment>, MutationProgramError> {
        match self {
            Self::Set(expression) => Ok(Some(ProjectionAssignment::Set(
                expression.rewrite_with(bind_input_path)?,
            ))),
            Self::Unset => Ok(Some(ProjectionAssignment::Unset)),
            Self::Unknown => Ok(None),
        }
    }
}

fn expression_depth(expression: &MutationExpression, level: usize) -> usize {
    match &expression.expression {
        MutationExpressionKind::InputPath { .. }
        | MutationExpressionKind::Constant { .. }
        | MutationExpressionKind::Enum { .. } => level,
        MutationExpressionKind::List { values } => values
            .iter()
            .map(|value| expression_depth(value, level + 1))
            .max()
            .unwrap_or(level),
        MutationExpressionKind::Object { fields } => fields
            .iter()
            .map(|field| expression_depth(&field.value, level + 1))
            .max()
            .unwrap_or(level),
        MutationExpressionKind::Transform { arguments, .. } => arguments
            .iter()
            .map(|argument| expression_depth(argument, level + 1))
            .max()
            .unwrap_or(level),
    }
}

pub(crate) fn non_empty(value: String, kind: &'static str) -> Result<String, MutationProgramError> {
    if value.is_empty() {
        Err(MutationProgramError::EmptyName(kind))
    } else {
        Ok(value)
    }
}

fn projection_value_to_json(value: &ProjectionValue) -> Result<Value, MutationProgramError> {
    Ok(match value.as_ref() {
        ProjectionValueRef::Null => Value::Null,
        ProjectionValueRef::Boolean(value) => Value::Bool(value),
        ProjectionValueRef::I64(text) => Value::String(text.to_owned()),
        ProjectionValueRef::U64(text) => Value::String(text.to_owned()),
        ProjectionValueRef::F64(text) => Value::String(text.to_owned()),
        ProjectionValueRef::String(text) => Value::String(text.to_owned()),
        ProjectionValueRef::Enum { variant, .. } => Value::String(variant.to_owned()),
        ProjectionValueRef::List(values) => Value::Array(
            values
                .iter()
                .map(projection_value_to_json)
                .collect::<Result<Vec<_>, _>>()?,
        ),
        ProjectionValueRef::Object(fields) => {
            let mut object = serde_json::Map::new();
            for field in fields {
                object.insert(
                    field.name().to_owned(),
                    projection_value_to_json(field.value())?,
                );
            }
            Value::Object(object)
        }
    })
}