alpha2-eval 0.10.0

Pure alpha2 timeline evaluation.
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
//! What an edit operation asks for, what it reports, and why it refuses.

use alpha2_model::{
    ClipId, CompositionId, DataAnchor, DataKind, DataSetId, ModelError, NodeId, TrackId,
};
use alpha2_time::{MediaTime, TimeRange};
use serde::{Deserialize, Serialize};
use std::fmt;

/// One edit operation, applied to a project once.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(tag = "op", rename_all = "snake_case", deny_unknown_fields)]
pub enum EditRequest {
    /// Removes timeline ranges from the target clips, with ripple.
    RemoveRanges {
        /// An `intervals` data set or an anchored list.
        ranges: Ranges,
        /// The clips the ranges come from and apply to.
        target: Target,
        /// What closes the gaps; `timeline` by default.
        #[serde(default)]
        ripple: Ripple,
        /// Keeps only the ranges, removing their complement within the target clips' extent.
        #[serde(default)]
        invert: bool,
        /// Ticks kept at each edge of every removed range; a negative value widens each range,
        /// which never starts before time 0.
        #[serde(default)]
        padding: MediaTime,
        /// Removed ranges shorter than this, after padding, are kept.
        #[serde(default)]
        min_duration: MediaTime,
    },
    /// Splits every target clip spanning a mapped time, except where a cut would change what
    /// the clip shows; those cuts are listed in [`EditReport::skipped`].
    SplitAt {
        /// An `intervals` data set's starts or an anchored list of ticks.
        times: Times,
        /// The clips split.
        target: Target,
    },
    /// Replaces bindings with plain values.
    Bake {
        /// What is baked.
        targets: Vec<BakeTarget>,
    },
}

/// Ranges an operation reads.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Ranges {
    /// An `intervals` data set of the project.
    Data(DataRef),
    /// A list on an anchor's clock.
    List(RangeList),
}

/// A data set an operation reads.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct DataRef {
    /// Its id.
    pub data: DataSetId,
}

/// Ranges given in the request.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RangeList {
    /// The ranges as `{start, end}` ticks, each `start <= end`.
    pub list: Vec<RawRange>,
    /// The clock they are on; required.
    #[serde(default)]
    pub anchor: Option<DataAnchor>,
}

/// A range as written, before its order is checked.
#[derive(Copy, Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RawRange {
    /// Its start, in ticks.
    pub start: MediaTime,
    /// Its end, in ticks.
    pub end: MediaTime,
}

/// Times an operation reads.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Times {
    /// The starts of an `intervals` data set's items.
    Data(DataRef),
    /// A list of ticks on an anchor's clock.
    List(TimeList),
}

/// Times given in the request.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct TimeList {
    /// The times, in ticks.
    pub list: Vec<MediaTime>,
    /// The clock they are on; required.
    #[serde(default)]
    pub anchor: Option<DataAnchor>,
}

/// The clips an operation works on.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case", deny_unknown_fields)]
pub enum Target {
    /// Every clip of a track.
    Track(TrackId),
    /// These clips.
    Clips(Vec<ClipId>),
}

/// What closes the gaps a removal leaves.
#[derive(Copy, Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Ripple {
    /// Nothing closes them; the target clips lose the ranges and gaps stay.
    None,
    /// Every clip on the target clips' tracks loses the ranges and closes up; other tracks stay.
    Track,
    /// The ranges leave every track, so picture, sound and captions stay in sync.
    #[default]
    Timeline,
}

/// One value or caption track to bake.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(untagged)]
pub enum BakeTarget {
    /// A clip's property, such as `visual.position`, `masks[0].feather` or `audio.gain`.
    Clip {
        /// The clip.
        clip: ClipId,
        /// The property path.
        property: String,
    },
    /// A composition node's property, such as `effect.params.radius`.
    Node {
        /// The composition.
        composition: CompositionId,
        /// The node.
        node: NodeId,
        /// The property path.
        property: String,
    },
    /// A track's property: `captions`, `audio.gain` or `audio.pan`.
    Track {
        /// The track.
        track: TrackId,
        /// The property path.
        property: String,
    },
}

/// A kept stretch of timeline that moved.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Moved {
    /// Where it was.
    pub from: TimeRange,
    /// Where it is.
    pub to: TimeRange,
}

/// A clip an operation created by cutting another.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Created {
    /// The new clip.
    pub id: ClipId,
    /// The clip it was cut from, which keeps its id on its first part.
    pub from: ClipId,
}

/// Why `split_at` left a cut out.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum SkipReason {
    /// The cut, or a loop part boundary it makes, falls inside one of the clip's transitions.
    InTransition,
    /// The cut falls inside one of the clip's fades.
    InFade,
    /// The clip is another clip's matte, and that clip is not cut alike.
    MatteSource,
    /// A binding reads through the clip (`via`), and its owner is not cut alike.
    ReadThrough,
}

/// A cut `split_at` left out.
#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct Skipped {
    /// The clip.
    pub clip: ClipId,
    /// The cut, in timeline time.
    pub time: MediaTime,
    /// Why.
    pub reason: SkipReason,
}

/// What an operation changed, so a caller can show and select it.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)]
pub struct EditReport {
    /// Timeline ranges removed, in the old timeline's time.
    pub removed: Vec<TimeRange>,
    /// Kept stretches that moved.
    pub moved: Vec<Moved>,
    /// Clips the operation created, with the clip each was cut from.
    pub created: Vec<Created>,
    /// Cuts `split_at` left out, in clip and time order; omitted when there are none.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub skipped: Vec<Skipped>,
}

/// Why an operation refused.
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum EditError {
    /// It names no data set of the project.
    UnknownData(DataSetId),
    /// The data set's kind does not fit the operation.
    WrongKind {
        /// The data set.
        data: DataSetId,
        /// Its kind.
        kind: DataKind,
    },
    /// It names a clip, track, composition or node the project lacks.
    UnknownTarget(String),
    /// The target names no clip.
    EmptyTarget,
    /// A list without an anchor.
    MissingAnchor,
    /// A range with its start after its end, by position.
    InvalidRange(usize),
    /// A bake target whose value is not bound to data (or a track with no words).
    NothingToBake(String),
    /// A correction has no keyframe equivalent.
    CorrectionNotBakeable(String),
    /// A bake target is a parameter of an effect the model has no range for (a host effect), so
    /// the live value the renderer clamps cannot be written.
    UnboundedParameter(String),
    /// The edit cuts a matte source where a clip it mattes is not cut, or removes it from under
    /// one, so no single part of it can stay that clip's matte.
    MatteSource(ClipId),
    /// The edit cuts a clip where a binding reading through it (`via`) is not cut, or removes it,
    /// so no single part of it can be read through.
    ReadThrough {
        /// The clip read through.
        clip: ClipId,
        /// The binding's owner, such as `clip b`, `track t` or `the master bus`.
        by: String,
    },
    /// A removed range's edge, or a part boundary the removal makes in a loop, falls strictly
    /// inside a transition of the clip it cuts.
    InTransition {
        /// The clip cut.
        clip: ClipId,
        /// Where, in timeline time.
        time: MediaTime,
    },
    /// A removed range's edge falls strictly inside a fade of the clip it cuts.
    InFade {
        /// The clip cut.
        clip: ClipId,
        /// Where, in timeline time.
        time: MediaTime,
    },
    /// The result would hold more clips on a track than a track holds.
    TooManyClips {
        /// The track.
        track: TrackId,
        /// The clips it would hold.
        found: usize,
        /// The most it holds.
        maximum: usize,
    },
    /// A caption bake's cues do not fit a track: more than it holds, or words past the limits of
    /// cues from words ([`alpha2_model::MAX_DERIVED_CUES`], [`alpha2_model::MAX_DATA_WORDS`]).
    TooManyCues {
        /// The track.
        track: TrackId,
        /// The cues it would hold; `None` when the words pass the limits of cues from words.
        found: Option<usize>,
        /// The most it holds.
        maximum: usize,
    },
    /// A bus value whose repetition the edit writes out as keyframes would hold more keyframes
    /// than a value holds.
    TooManyKeyframes {
        /// The value, such as `TrackAudio.gain of track t`.
        of: String,
        /// The most a value holds.
        maximum: usize,
    },
    /// The edited project failed validation, which is a bug.
    InvalidResult(ModelError),
}

impl fmt::Display for EditError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnknownData(data) => write!(formatter, "unknown data set {data}"),
            Self::WrongKind { data, kind } => write!(
                formatter,
                "data set {data} is {kind} data, which the operation does not take"
            ),
            Self::UnknownTarget(what) => write!(formatter, "unknown {what}"),
            Self::EmptyTarget => formatter.write_str("the target names no clip"),
            Self::MissingAnchor => formatter.write_str("a list needs an anchor"),
            Self::InvalidRange(index) => write!(formatter, "range {index} starts after it ends"),
            Self::NothingToBake(at) => write!(formatter, "{at} follows no data"),
            Self::CorrectionNotBakeable(at) => {
                write!(formatter, "{at} is a correction, which has no keyframes")
            }
            Self::UnboundedParameter(at) => write!(
                formatter,
                "{at} is a parameter of an effect whose range only the renderer knows"
            ),
            Self::MatteSource(clip) => write!(
                formatter,
                "clip {clip} is another clip's matte, and the edit cuts the two apart"
            ),
            Self::ReadThrough { clip, by } => write!(
                formatter,
                "{by} reads data through clip {clip}, and the edit cuts the two apart"
            ),
            Self::InTransition { clip, time } => write!(
                formatter,
                "the edit cuts clip {clip} at {} inside one of its transitions",
                time.ticks()
            ),
            Self::InFade { clip, time } => write!(
                formatter,
                "the edit cuts clip {clip} at {} inside one of its fades",
                time.ticks()
            ),
            Self::TooManyClips {
                track,
                found,
                maximum,
            } => write!(
                formatter,
                "the edit leaves {found} clips on track {track}; a track holds {maximum}"
            ),
            Self::TooManyCues {
                track,
                found: Some(found),
                maximum,
            } => write!(
                formatter,
                "the bake writes {found} cues on track {track}; a track holds {maximum}"
            ),
            Self::TooManyCues {
                track, found: None, ..
            } => write!(
                formatter,
                "the words of track {track} derive more than {} cues or map more than {} words",
                alpha2_model::MAX_DERIVED_CUES,
                alpha2_model::MAX_DATA_WORDS
            ),
            Self::TooManyKeyframes { of, maximum } => write!(
                formatter,
                "the edit writes the repetition of {of} out as more than {maximum} keyframes"
            ),
            Self::InvalidResult(error) => {
                write!(formatter, "the edited project is invalid: {error}")
            }
        }
    }
}

impl std::error::Error for EditError {}

impl EditError {
    /// The refusal in snake case, as every binding reports it: `unknown_data`, `wrong_kind`,
    /// `unknown_target`, `empty_target`, `missing_anchor`, `invalid_range`, `nothing_to_bake`,
    /// `correction_not_bakeable`, `unbounded_parameter`, `matte_source`, `read_through`,
    /// `in_transition`, `in_fade`, `too_many_clips`, `too_many_cues`, `result_too_large` or
    /// `invalid_result`.
    #[must_use]
    pub const fn kind(&self) -> &'static str {
        match self {
            Self::UnknownData(_) => "unknown_data",
            Self::WrongKind { .. } => "wrong_kind",
            Self::UnknownTarget(_) => "unknown_target",
            Self::EmptyTarget => "empty_target",
            Self::MissingAnchor => "missing_anchor",
            Self::InvalidRange(_) => "invalid_range",
            Self::NothingToBake(_) => "nothing_to_bake",
            Self::CorrectionNotBakeable(_) => "correction_not_bakeable",
            Self::UnboundedParameter(_) => "unbounded_parameter",
            Self::MatteSource(_) => "matte_source",
            Self::ReadThrough { .. } => "read_through",
            Self::InTransition { .. } => "in_transition",
            Self::InFade { .. } => "in_fade",
            Self::TooManyClips { .. } => "too_many_clips",
            Self::TooManyCues { .. } => "too_many_cues",
            Self::TooManyKeyframes { .. } => "result_too_large",
            Self::InvalidResult(_) => "invalid_result",
        }
    }
}