Skip to main content

alpha2_model/
error.rs

1use crate::{
2    AudioNodeRef, BusId, ClipId, CompositionId, DataSetId, EffectProblem, EffectResourceId, NodeId,
3    TrackId,
4};
5use alpha2_media::SourceId;
6use std::fmt;
7
8/// What holds the shape or layout a [`ModelError::InvalidShape`] or
9/// [`ModelError::InvalidLayout`] names.
10#[derive(Clone, Debug, Eq, PartialEq)]
11pub enum ShapeOwner {
12    /// A generated source's shapes or layout generator.
13    Source(SourceId),
14    /// A clip's masks.
15    Clip(ClipId),
16    /// A composition node's shapes or layout generator, or its masks.
17    Node {
18        /// The composition.
19        composition: CompositionId,
20        /// The node.
21        node: NodeId,
22    },
23}
24
25impl fmt::Display for ShapeOwner {
26    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
27        match self {
28            Self::Source(source) => write!(formatter, "source {source}"),
29            Self::Clip(clip) => write!(formatter, "clip {clip}"),
30            Self::Node { composition, node } => {
31                write!(formatter, "composition {composition} node {node}")
32            }
33        }
34    }
35}
36
37/// What holds the value a [`ModelError::InvalidDataBinding`] names.
38#[derive(Clone, Debug, Eq, PartialEq)]
39pub enum DataOwner {
40    /// A clip's visual, masks or audio.
41    Clip(ClipId),
42    /// A track's bus or caption track.
43    Track(TrackId),
44    /// The master bus.
45    Master,
46    /// A bus.
47    Bus(BusId),
48    /// A timeline transition's effect.
49    Transition {
50        /// Its track.
51        track: TrackId,
52        /// Its first clip.
53        from: ClipId,
54        /// Its second clip.
55        to: ClipId,
56    },
57    /// A composition node's settings.
58    Node {
59        /// The composition.
60        composition: CompositionId,
61        /// The node.
62        node: NodeId,
63    },
64    /// A generated source's tree.
65    Source(SourceId),
66}
67
68impl fmt::Display for DataOwner {
69    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
70        match self {
71            Self::Clip(clip) => write!(formatter, "clip {clip}"),
72            Self::Track(track) => write!(formatter, "track {track}"),
73            Self::Master => formatter.write_str("the master bus"),
74            Self::Bus(bus) => write!(formatter, "bus {bus}"),
75            Self::Transition { track, from, to } => {
76                write!(formatter, "track {track} transition {from} to {to}")
77            }
78            Self::Node { composition, node } => {
79                write!(formatter, "composition {composition} node {node}")
80            }
81            Self::Source(source) => write!(formatter, "source {source}"),
82        }
83    }
84}
85
86/// What holds the effect a [`ModelError::InvalidEffect`] names.
87#[derive(Clone, Debug, Eq, PartialEq)]
88pub enum EffectOwner {
89    /// A composition's effect, transition, generator or group node.
90    Node {
91        /// The composition.
92        composition: CompositionId,
93        /// The node.
94        node: NodeId,
95    },
96    /// A timeline transition.
97    Transition {
98        /// Its track.
99        track: TrackId,
100        /// The clip at progress zero.
101        from: ClipId,
102        /// The clip at progress one.
103        to: ClipId,
104    },
105    /// A generated source's effect generator.
106    Source(SourceId),
107}
108
109impl fmt::Display for EffectOwner {
110    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
111        match self {
112            Self::Node { composition, node } => {
113                write!(formatter, "composition {composition} node {node}")
114            }
115            Self::Transition { track, from, to } => {
116                write!(
117                    formatter,
118                    "the transition from {from} to {to} on track {track}"
119                )
120            }
121            Self::Source(source) => write!(formatter, "source {source}"),
122        }
123    }
124}
125
126impl fmt::Display for EffectProblem {
127    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
128        match self {
129            Self::UnknownBuiltin => formatter.write_str("is not a built-in"),
130            Self::Version { expected } => write!(formatter, "needs version {expected}"),
131            Self::Kind => formatter.write_str("is used as the wrong kind"),
132            Self::UnknownParam(name) => write!(formatter, "has no parameter {name}"),
133            Self::InvalidParam(name) => {
134                write!(
135                    formatter,
136                    "has a parameter {name} of the wrong type or out of range"
137                )
138            }
139            Self::Inputs => formatter.write_str("does not get exactly its declared inputs"),
140            Self::Resources => formatter.write_str("does not get exactly its declared resources"),
141        }
142    }
143}
144
145/// A document invariant violation.
146#[derive(Clone, Debug, Eq, PartialEq)]
147#[non_exhaustive]
148pub enum ModelError {
149    /// This build cannot interpret the document version.
150    UnsupportedSchemaVersion {
151        /// Version found in the decoded document.
152        found: u32,
153    },
154    /// The project has more than [`crate::MAX_DATA_SETS`] data sets.
155    TooManyDataSets {
156        /// Data sets in the document.
157        found: usize,
158        /// Largest accepted count.
159        maximum: usize,
160    },
161    /// Two data sets share one ID.
162    DuplicateDataSetId(DataSetId),
163    /// A data set breaks a rule of its kind.
164    InvalidDataSet {
165        /// The data set.
166        data: DataSetId,
167        /// What is wrong.
168        problem: crate::DataProblem,
169    },
170    /// A data binding, correction or caption words reference is unusable where it sits.
171    InvalidDataBinding {
172        /// What holds it.
173        owner: DataOwner,
174        /// The property, like `visual.position`, `masks[0].feather`, `effect.params.radius`,
175        /// `visual.correction` or `captions.from_words`.
176        at: String,
177        /// What is wrong.
178        problem: crate::BindingProblem,
179    },
180    /// At least one ID was empty.
181    EmptyIdentifier,
182    /// Two sources share one ID.
183    DuplicateSourceId(SourceId),
184    /// Two effect resources share one ID.
185    DuplicateEffectResourceId(EffectResourceId),
186    /// Two tracks share one ID.
187    DuplicateTrackId(TrackId),
188    /// Two clips share one ID.
189    DuplicateClipId(ClipId),
190    /// A source locator was empty.
191    EmptyLocator {
192        /// Source whose locator was empty.
193        source: SourceId,
194    },
195    /// A still image source says it has audio, which no image holds.
196    AudioOnImage {
197        /// The image source.
198        source: SourceId,
199    },
200    /// A source's provider does not fit it (wrong source kind, audio, or incoherent settings),
201    /// or a generated source has one.
202    InvalidProvider {
203        /// The source.
204        source: SourceId,
205    },
206    /// A source's fingerprint is malformed, or a generated source has one.
207    InvalidFingerprint {
208        /// The source.
209        source: SourceId,
210    },
211    /// A source declares a color whose dynamic range contradicts its transfer (HDR is exactly a
212    /// PQ or HLG transfer).
213    ContradictoryColor {
214        /// The source.
215        source: SourceId,
216    },
217    /// A generated source has no valid generator, or has a locator, color metadata or audio; or
218    /// another source has a generator.
219    InvalidGenerator {
220        /// The source.
221        source: SourceId,
222    },
223    /// A shapes content tree or mask outline breaks a limit, has a value out of range, or has
224    /// keyframes that cannot interpolate.
225    InvalidShape {
226        /// What holds the shape.
227        owner: ShapeOwner,
228        /// Where the problem lies, like `items[2].items[0].path` or `masks[1].shape.size`.
229        at: String,
230        /// What is wrong there.
231        problem: crate::ShapeProblem,
232    },
233    /// A layout tree breaks a limit or structure rule, has a value out of range, or has
234    /// keyframes that cannot interpolate.
235    InvalidLayout {
236        /// What holds the layout.
237        owner: ShapeOwner,
238        /// Where in it the problem lies, like `root.children[2].padding`.
239        at: String,
240        /// What is wrong there.
241        problem: crate::LayoutProblem,
242    },
243    /// A text generator's run has a style out of range once applied, or the runs draw in more
244    /// than [`crate::MAX_TEXT_FONT_SELECTIONS`] font selections from this run on.
245    InvalidTextRunStyle {
246        /// What holds the text generator.
247        owner: ShapeOwner,
248        /// The run's position among the text's runs.
249        run: usize,
250    },
251    /// An effect resource locator was empty.
252    EmptyEffectResourceLocator {
253        /// Resource whose locator was empty.
254        resource: EffectResourceId,
255    },
256    /// A clip effect references an absent effect resource.
257    UnknownEffectResource {
258        /// Clip containing the reference.
259        clip: ClipId,
260        /// Missing resource identity.
261        resource: EffectResourceId,
262    },
263    /// A clip has an empty source or timeline range.
264    EmptyClipRange {
265        /// Clip with an empty range.
266        clip: ClipId,
267    },
268    /// A clip starts before the project or source timeline origin.
269    NegativeClipRange {
270        /// Clip with a negative range start.
271        clip: ClipId,
272    },
273    /// Two clips overlap without one transition covering their intersection.
274    OverlappingClips {
275        /// Track containing the overlap.
276        track: TrackId,
277        /// First overlapping clip.
278        first: ClipId,
279        /// Second overlapping clip.
280        second: ClipId,
281    },
282    /// A clip refers to an absent source.
283    UnknownClipSource {
284        /// Clip containing the absent source reference.
285        clip: ClipId,
286        /// Referenced source that was not in the project.
287        source: SourceId,
288    },
289    /// A source cannot be placed on the selected track kind.
290    IncompatibleClipSource {
291        /// Clip containing the incompatible reference.
292        clip: ClipId,
293        /// Track that cannot accept the source kind.
294        track: TrackId,
295        /// Source with the incompatible kind.
296        source: SourceId,
297    },
298    /// A visual clip has non-finite values, invalid opacity, or unordered keyframes.
299    InvalidVisualProperties {
300        /// Clip containing the invalid presentation data.
301        clip: ClipId,
302    },
303    /// A clip has an invalid gain, pan, fade, or audio effect parameter.
304    InvalidAudioProperties {
305        /// Clip containing the invalid audio data.
306        clip: ClipId,
307    },
308    /// A track bus has an invalid gain, pan or effect parameter, or too many sends.
309    InvalidTrackAudio {
310        /// Track containing the invalid bus data.
311        track: TrackId,
312    },
313    /// The project master bus has an invalid gain, effect, or limiter parameter.
314    InvalidMasterAudio,
315    /// A bus has an invalid gain, pan or effect parameter, or too many sends.
316    InvalidAudioBus {
317        /// Bus containing the invalid data.
318        bus: BusId,
319    },
320    /// A send has an invalid gain.
321    InvalidAudioSend {
322        /// Track or bus the send is on.
323        from: AudioNodeRef,
324        /// Bus the send names.
325        to: BusId,
326    },
327    /// An output or send names a bus the project does not have.
328    UnknownAudioBus {
329        /// Track or bus whose output or send it is.
330        from: AudioNodeRef,
331        /// The missing bus.
332        bus: BusId,
333    },
334    /// Two buses share an id.
335    DuplicateAudioBus {
336        /// The repeated id.
337        bus: BusId,
338    },
339    /// Outputs, sends and effect keys lead from a track or bus back to itself.
340    AudioRoutingCycle {
341        /// The first track or bus, in document order, on a cycle.
342        node: AudioNodeRef,
343    },
344    /// The project holds more buses than [`crate::MAX_AUDIO_BUSES`].
345    TooManyAudioBuses {
346        /// Number supplied by the document.
347        found: usize,
348        /// Largest supported count.
349        maximum: usize,
350    },
351    /// An effect's `sidechain` names a track or bus the project does not have.
352    UnknownAudioSidechain {
353        /// Track or bus whose effect it is; `None` is the master.
354        owner: Option<AudioNodeRef>,
355        /// The missing track or bus.
356        sidechain: AudioNodeRef,
357    },
358    /// A clip names an absent composition.
359    UnknownComposition {
360        /// The clip.
361        clip: ClipId,
362        /// The composition it names.
363        composition: CompositionId,
364    },
365    /// A clip's composition has a non-positive scale, reaches a composition time past
366    /// [`alpha2_time::MAX_SERIALIZED_TICKS`] before the clip ends, or is on an audio clip.
367    InvalidClipComposition {
368        /// The clip.
369        clip: ClipId,
370    },
371    /// Two compositions share one ID.
372    DuplicateCompositionId(CompositionId),
373    /// A node ID contains `/`, which is reserved for joining the IDs of inlined group and media
374    /// nodes.
375    InvalidNodeId {
376        /// The composition.
377        composition: CompositionId,
378        /// The node ID.
379        node: NodeId,
380    },
381    /// Two nodes of one composition share an ID.
382    DuplicateNodeId {
383        /// The composition.
384        composition: CompositionId,
385        /// The repeated node ID.
386        node: NodeId,
387    },
388    /// A composition has more nodes than the renderer will schedule.
389    TooManyCompositionNodes {
390        /// The composition.
391        composition: CompositionId,
392        /// Number supplied by the document.
393        found: usize,
394        /// Largest supported count.
395        maximum: usize,
396    },
397    /// A node has an invalid setting: effect identity, version, parameter or value; transform,
398    /// progress, opacity or mask out of range; too many merge layers or masks; or a bypass on a
399    /// node without inputs.
400    InvalidNode {
401        /// The composition.
402        composition: CompositionId,
403        /// The node.
404        node: NodeId,
405    },
406    /// A node input names a node its composition does not have.
407    UnknownNodeInput {
408        /// The composition.
409        composition: CompositionId,
410        /// The node whose input it is.
411        node: NodeId,
412        /// The input: `input`, `from`, `to`, `matte`, `layers[i]`, or `inputs[i]`.
413        input: String,
414        /// The node named.
415        target: NodeId,
416    },
417    /// A node depends on its own output.
418    CompositionCycle {
419        /// The composition.
420        composition: CompositionId,
421        /// A node on the cycle.
422        node: NodeId,
423    },
424    /// A composition's output names a node it does not have.
425    UnknownCompositionOutput {
426        /// The composition.
427        composition: CompositionId,
428        /// The node named.
429        node: NodeId,
430    },
431    /// A media node names an absent source or one that is not visual.
432    InvalidNodeSource {
433        /// The composition.
434        composition: CompositionId,
435        /// The node.
436        node: NodeId,
437        /// The source named.
438        source: SourceId,
439    },
440    /// A composition's group inputs are unnamed, repeated or too many, or a control names no
441    /// effect setting of its nodes or repeats one.
442    InvalidCompositionPorts {
443        /// The composition.
444        composition: CompositionId,
445    },
446    /// A group node names an absent composition.
447    UnknownGroupComposition {
448        /// The composition holding the group node.
449        composition: CompositionId,
450        /// The group node.
451        node: NodeId,
452        /// The composition it names.
453        target: CompositionId,
454    },
455    /// A time offset node reads what has no other time.
456    InvalidTimeOffset {
457        /// The composition.
458        composition: CompositionId,
459        /// The time offset node.
460        node: NodeId,
461        /// What its input reaches.
462        problem: crate::TimeOffsetProblem,
463    },
464    /// A composition uses itself through group nodes or composition sources.
465    CompositionUsesItself {
466        /// A composition on the loop.
467        composition: CompositionId,
468    },
469    /// Compositions nest, through group nodes, composition sources or time offset nodes, more
470    /// than [`crate::MAX_COMPOSITION_DEPTH`] deep.
471    CompositionNestedTooDeep {
472        /// The first such composition, in document order.
473        composition: CompositionId,
474        /// Deepest supported nesting.
475        maximum: usize,
476    },
477    /// The compositions draw more than [`crate::MAX_INLINED_NODES`] nodes, counting each group
478    /// node and composition-source media node as the nodes it draws and each time offset node
479    /// as the nodes it draws again.
480    TooManyInlinedNodes {
481        /// Largest supported count.
482        maximum: usize,
483    },
484    /// The clips at one instant draw more than [`crate::MAX_FRAME_INLINED_NODES`] composition
485    /// nodes together.
486    FrameDrawsTooManyNodes {
487        /// The first clip, in document order, starting at the first such instant.
488        clip: ClipId,
489        /// The nodes the clips at that instant draw.
490        found: u64,
491        /// Largest supported count.
492        maximum: usize,
493    },
494    /// A composition source names an absent composition, or has what only a host-supplied
495    /// source has.
496    InvalidCompositionSource {
497        /// The source.
498        source: SourceId,
499    },
500    /// An adjustment source has more than an ID and its kind: a locator, generator,
501    /// composition, color, fingerprint, provider or audio.
502    InvalidAdjustmentSource {
503        /// The source.
504        source: SourceId,
505    },
506    /// A transition from or to an adjustment clip is not `alpha2.transition.fade`.
507    InvalidAdjustmentTransition {
508        /// The track holding the transition.
509        track: TrackId,
510        /// The adjustment clip it names, its `from_clip` first.
511        clip: ClipId,
512    },
513    /// An effect, transition or generator under [`crate::BUILTIN_EFFECT_PREFIX`] that does not
514    /// match its built-in definition.
515    InvalidEffect {
516        /// Where it is used.
517        owner: Box<EffectOwner>,
518        /// The effect id.
519        id: String,
520        /// How it fails its definition.
521        problem: EffectProblem,
522    },
523    /// A node's effect references an absent effect resource.
524    UnknownNodeResource {
525        /// The composition.
526        composition: CompositionId,
527        /// The node.
528        node: NodeId,
529        /// The resource named.
530        resource: EffectResourceId,
531    },
532    /// A generated source's effect generator references an absent effect resource.
533    UnknownSourceResource {
534        /// The source.
535        source: SourceId,
536        /// The resource named.
537        resource: EffectResourceId,
538    },
539    /// A clip has more than [`crate::MAX_CLIP_MASKS`] masks, a mask with a negative or
540    /// non-finite value, or masks on an audio track.
541    InvalidClipMask {
542        /// Clip carrying the invalid mask.
543        clip: ClipId,
544    },
545    /// A caption track has no captions, has clips or transitions, or has invalid cues or style;
546    /// or another track has captions.
547    InvalidCaptions {
548        /// The track.
549        track: TrackId,
550    },
551    /// A caption cue names a style its track does not have.
552    UnknownCaptionStyle {
553        /// The track.
554        track: TrackId,
555        /// The cue's position among the track's cues.
556        cue: usize,
557        /// The style it names.
558        name: String,
559    },
560    /// A caption cue's style (its track's with the cue's patch applied), or the style of one of
561    /// its runs over it, is out of range, or its runs draw in more than
562    /// [`crate::MAX_TEXT_FONT_SELECTIONS`] font selections.
563    InvalidCaptionCueStyle {
564        /// The track.
565        track: TrackId,
566        /// The cue's position among the track's cues.
567        cue: usize,
568    },
569    /// A clip's time map cannot be played: a freeze outside the source range, or a curve
570    /// with unusable keyframes.
571    InvalidTimeMap {
572        /// The clip.
573        clip: ClipId,
574        /// What is wrong with it.
575        problem: crate::TimeMapProblem,
576    },
577    /// A looping clip makes more than [`crate::MAX_LOOP_PASSES`] passes.
578    TooManyLoopPasses {
579        /// The clip.
580        clip: ClipId,
581        /// Its passes, rounded up.
582        found: u64,
583        /// The most passes a clip makes.
584        maximum: u64,
585    },
586    /// A clip's matte names no visual clip, itself, a clip with its own matte, an adjustment
587    /// clip, or a clip in a transition; or an audio or adjustment clip has a matte.
588    InvalidMatte {
589        /// Clip whose matte is invalid.
590        clip: ClipId,
591    },
592    /// A clip outside a video track sets a frame interpolation.
593    InvalidFrameInterpolation {
594        /// Clip whose frame interpolation is invalid.
595        clip: ClipId,
596    },
597    /// A transition is not contained in two distinct visual clips on its track.
598    InvalidClipTransition {
599        /// Track containing the invalid transition.
600        track: TrackId,
601    },
602    /// Transitions active at one instant lead from clip to clip in a circle, leaving no clip to
603    /// show.
604    TransitionCycle {
605        /// Track containing the transitions.
606        track: TrackId,
607        /// An instant they are all active.
608        at: alpha2_time::MediaTime,
609    },
610    /// Two clips have more than one transition between them, in either direction.
611    DuplicateTransition {
612        /// Track containing the transitions.
613        track: TrackId,
614        /// The transition's first clip.
615        first: ClipId,
616        /// The transition's second clip.
617        second: ClipId,
618    },
619    /// A caption track derives more than [`crate::MAX_DERIVED_CUES`] cues from its words, or maps
620    /// more than [`crate::MAX_DATA_WORDS`] words through its clips.
621    TooManyDerivedCues {
622        /// The caption track.
623        track: TrackId,
624    },
625    /// A time lies past [`alpha2_time::MAX_SERIALIZED_TICKS`] either way, which no document holds.
626    UnwritableTime {
627        /// The time, in ticks.
628        ticks: i64,
629    },
630    /// The project has more than [`crate::MAX_EFFECT_RESOURCES`] effect resources.
631    TooManyEffectResources {
632        /// Number supplied by the document.
633        found: usize,
634        /// Largest supported count.
635        maximum: usize,
636    },
637    /// The project contains too many compositions.
638    TooManyCompositions {
639        /// Number supplied by the document.
640        found: usize,
641        /// Largest supported count.
642        maximum: usize,
643    },
644    /// The source table exceeds the bounded project resource budget.
645    TooManySources {
646        /// Number supplied by the document.
647        found: usize,
648        /// Largest supported count.
649        maximum: usize,
650    },
651    /// The timeline contains too many tracks.
652    TooManyTracks {
653        /// Number supplied by the document.
654        found: usize,
655        /// Largest supported count.
656        maximum: usize,
657    },
658    /// The timeline contains too many clips in total.
659    TooManyClips {
660        /// Number supplied by the document.
661        found: usize,
662        /// Largest supported count.
663        maximum: usize,
664    },
665    /// More audio effects hold state at once than [`crate::MAX_AUDIO_EFFECTS`] (see
666    /// [`crate::Project::audio_effect_load`]).
667    TooManyAudioEffects {
668        /// The most effects holding state at once.
669        found: usize,
670        /// Largest supported count.
671        maximum: usize,
672        /// The earliest timeline position, in ticks, at which `found` hold state.
673        at_ticks: i64,
674    },
675    /// The audio effects hold more than [`crate::MAX_AUDIO_DELAY_MS`] of delay at once (see
676    /// [`crate::Project::audio_effect_load`]).
677    TooMuchAudioDelay {
678        /// The most milliseconds held at once, rounded up.
679        found_ms: u64,
680        /// Largest supported total.
681        maximum_ms: u32,
682        /// The earliest timeline position, in ticks, at which `found_ms` is held.
683        at_ticks: i64,
684    },
685    /// One track contains too many clips.
686    TooManyClipsOnTrack {
687        /// Track containing the excessive clip list.
688        track: TrackId,
689        /// Number supplied by the document.
690        found: usize,
691        /// Largest supported count.
692        maximum: usize,
693    },
694    /// A caption track holds more than [`crate::MAX_CLIPS_PER_TRACK`] cues.
695    TooManyCaptionCues {
696        /// The caption track.
697        track: TrackId,
698        /// Number supplied by the document.
699        found: usize,
700        /// Largest supported count.
701        maximum: usize,
702    },
703    /// One track contains too many transitions.
704    TooManyTransitions {
705        /// Track containing the excessive transition list.
706        track: TrackId,
707        /// Number supplied by the document.
708        found: usize,
709        /// Largest supported count.
710        maximum: usize,
711    },
712    /// Width or height was zero.
713    ZeroCanvasDimension,
714    /// A canvas edge exceeds the portable GPU-independent limit.
715    CanvasDimensionTooLarge {
716        /// Supplied width.
717        width: u32,
718        /// Supplied height.
719        height: u32,
720        /// Largest supported edge.
721        maximum: u32,
722    },
723    /// The canvas area exceeds the bounded pixel budget.
724    CanvasPixelBudgetExceeded {
725        /// Supplied pixel area.
726        pixels: u64,
727        /// Largest supported pixel area.
728        maximum: u64,
729    },
730    /// A background component is non-finite or outside `0..=1`.
731    InvalidBackground,
732    /// The color pipeline's HDR peak is outside [`crate::HDR_PEAK_NITS_RANGE`].
733    InvalidColorPipeline,
734    /// A field of the project's shutter is out of range.
735    InvalidShutter(crate::ShutterProblem),
736    /// The project sample rate is outside the supported DSP interval.
737    UnsupportedSampleRate {
738        /// Supplied rate.
739        found: u32,
740        /// Smallest supported rate.
741        minimum: u32,
742        /// Largest supported rate.
743        maximum: u32,
744    },
745}
746
747/// The document object a [`ModelError`] is about, by id.
748#[derive(Clone, Copy, Debug, Eq, PartialEq)]
749#[non_exhaustive]
750pub enum ErrorSubject<'a> {
751    /// A timeline clip.
752    Clip(&'a ClipId),
753    /// A media source.
754    Source(&'a SourceId),
755    /// A track.
756    Track(&'a TrackId),
757    /// A composition.
758    Composition(&'a CompositionId),
759    /// A data set.
760    DataSet(&'a DataSetId),
761    /// An effect resource.
762    EffectResource(&'a EffectResourceId),
763    /// An audio bus.
764    Bus(&'a BusId),
765}
766
767impl ModelError {
768    /// The document object this error is about, or `None` for one about the whole project.
769    ///
770    /// An overlap names its second clip, a duplicate transition its track.
771    #[must_use]
772    #[expect(clippy::too_many_lines, reason = "one arm per subject")]
773    pub const fn subject(&self) -> Option<ErrorSubject<'_>> {
774        use ErrorSubject::{Bus, Clip, Composition, DataSet, EffectResource, Source, Track};
775        Some(match self {
776            Self::UnsupportedSchemaVersion { .. }
777            | Self::TooManyDataSets { .. }
778            | Self::EmptyIdentifier
779            | Self::InvalidMasterAudio
780            | Self::TooManyInlinedNodes { .. }
781            | Self::TooManyCompositions { .. }
782            | Self::TooManyEffectResources { .. }
783            | Self::UnwritableTime { .. }
784            | Self::TooManySources { .. }
785            | Self::TooManyTracks { .. }
786            | Self::TooManyClips { .. }
787            | Self::TooManyAudioEffects { .. }
788            | Self::TooMuchAudioDelay { .. }
789            | Self::TooManyAudioBuses { .. }
790            | Self::UnknownAudioSidechain { owner: None, .. }
791            | Self::ZeroCanvasDimension
792            | Self::CanvasDimensionTooLarge { .. }
793            | Self::CanvasPixelBudgetExceeded { .. }
794            | Self::InvalidBackground
795            | Self::InvalidColorPipeline
796            | Self::InvalidShutter(_)
797            | Self::UnsupportedSampleRate { .. } => return None,
798            Self::DuplicateDataSetId(data) | Self::InvalidDataSet { data, .. } => DataSet(data),
799            Self::InvalidDataBinding { owner, .. } => match owner {
800                DataOwner::Clip(clip) | DataOwner::Transition { from: clip, .. } => Clip(clip),
801                DataOwner::Track(track) => Track(track),
802                DataOwner::Master => return None,
803                DataOwner::Bus(bus) => Bus(bus),
804                DataOwner::Node { composition, .. } => Composition(composition),
805                DataOwner::Source(source) => Source(source),
806            },
807            Self::InvalidShape { owner, .. }
808            | Self::InvalidLayout { owner, .. }
809            | Self::InvalidTextRunStyle { owner, .. } => match owner {
810                ShapeOwner::Source(source) => Source(source),
811                ShapeOwner::Clip(clip) => Clip(clip),
812                ShapeOwner::Node { composition, .. } => Composition(composition),
813            },
814            Self::InvalidEffect { owner, .. } => match &**owner {
815                EffectOwner::Node { composition, .. } => Composition(composition),
816                EffectOwner::Transition { from, .. } => Clip(from),
817                EffectOwner::Source(source) => Source(source),
818            },
819            Self::DuplicateSourceId(source)
820            | Self::EmptyLocator { source }
821            | Self::AudioOnImage { source }
822            | Self::InvalidProvider { source }
823            | Self::InvalidFingerprint { source }
824            | Self::ContradictoryColor { source }
825            | Self::InvalidGenerator { source }
826            | Self::UnknownSourceResource { source, .. }
827            | Self::InvalidCompositionSource { source }
828            | Self::InvalidAdjustmentSource { source } => Source(source),
829            Self::DuplicateEffectResourceId(resource)
830            | Self::EmptyEffectResourceLocator { resource } => EffectResource(resource),
831            Self::InvalidAudioBus { bus } | Self::DuplicateAudioBus { bus } => Bus(bus),
832            Self::InvalidAudioSend { from: node, .. }
833            | Self::UnknownAudioBus { from: node, .. }
834            | Self::UnknownAudioSidechain {
835                owner: Some(node), ..
836            }
837            | Self::AudioRoutingCycle { node } => match node {
838                AudioNodeRef::Track(track) => Track(track),
839                AudioNodeRef::Bus(bus) => Bus(bus),
840            },
841            Self::DuplicateTrackId(track)
842            | Self::InvalidTrackAudio { track }
843            | Self::InvalidCaptions { track }
844            | Self::UnknownCaptionStyle { track, .. }
845            | Self::InvalidCaptionCueStyle { track, .. }
846            | Self::InvalidClipTransition { track }
847            | Self::TransitionCycle { track, .. }
848            | Self::DuplicateTransition { track, .. }
849            | Self::TooManyClipsOnTrack { track, .. }
850            | Self::TooManyCaptionCues { track, .. }
851            | Self::TooManyDerivedCues { track }
852            | Self::TooManyTransitions { track, .. } => Track(track),
853            Self::DuplicateClipId(clip)
854            | Self::UnknownEffectResource { clip, .. }
855            | Self::EmptyClipRange { clip }
856            | Self::NegativeClipRange { clip }
857            | Self::OverlappingClips { second: clip, .. }
858            | Self::UnknownClipSource { clip, .. }
859            | Self::IncompatibleClipSource { clip, .. }
860            | Self::InvalidVisualProperties { clip }
861            | Self::InvalidAudioProperties { clip }
862            | Self::UnknownComposition { clip, .. }
863            | Self::InvalidClipComposition { clip }
864            | Self::FrameDrawsTooManyNodes { clip, .. }
865            | Self::InvalidClipMask { clip }
866            | Self::InvalidTimeMap { clip, .. }
867            | Self::TooManyLoopPasses { clip, .. }
868            | Self::InvalidAdjustmentTransition { clip, .. }
869            | Self::InvalidMatte { clip }
870            | Self::InvalidFrameInterpolation { clip } => Clip(clip),
871            Self::DuplicateCompositionId(composition)
872            | Self::InvalidNodeId { composition, .. }
873            | Self::DuplicateNodeId { composition, .. }
874            | Self::TooManyCompositionNodes { composition, .. }
875            | Self::InvalidNode { composition, .. }
876            | Self::UnknownNodeInput { composition, .. }
877            | Self::CompositionCycle { composition, .. }
878            | Self::UnknownCompositionOutput { composition, .. }
879            | Self::InvalidNodeSource { composition, .. }
880            | Self::InvalidCompositionPorts { composition }
881            | Self::UnknownGroupComposition { composition, .. }
882            | Self::InvalidTimeOffset { composition, .. }
883            | Self::CompositionUsesItself { composition }
884            | Self::CompositionNestedTooDeep { composition, .. }
885            | Self::UnknownNodeResource { composition, .. } => Composition(composition),
886        })
887    }
888}
889
890impl fmt::Display for ModelError {
891    #[allow(clippy::too_many_lines)]
892    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
893        match self {
894            Self::UnsupportedSchemaVersion { found } => {
895                write!(formatter, "unsupported schema version {found}")
896            }
897            Self::EmptyIdentifier => formatter.write_str("document contains an empty identifier"),
898            Self::DuplicateSourceId(id) => write!(formatter, "duplicate source ID {id}"),
899            Self::DuplicateEffectResourceId(id) => {
900                write!(formatter, "duplicate effect resource ID {id}")
901            }
902            Self::DuplicateTrackId(id) => write!(formatter, "duplicate track ID {id}"),
903            Self::DuplicateClipId(id) => write!(formatter, "duplicate clip ID {id}"),
904            Self::EmptyLocator { source } => {
905                write!(formatter, "source {source} has an empty locator")
906            }
907            Self::AudioOnImage { source } => {
908                write!(
909                    formatter,
910                    "source {source} is a still image and has no audio"
911                )
912            }
913            Self::InvalidGenerator { source } => {
914                write!(formatter, "source {source} has an invalid generator")
915            }
916            Self::InvalidShape { owner, at, problem } => {
917                write!(formatter, "{owner} has an invalid shape: {at} {problem}")
918            }
919            Self::InvalidLayout { owner, at, problem } => {
920                write!(formatter, "{owner} has an invalid layout: {at} {problem}")
921            }
922            Self::InvalidTextRunStyle { owner, run } => {
923                write!(
924                    formatter,
925                    "{owner} has text run {run} with a style out of range"
926                )
927            }
928            Self::ContradictoryColor { source } => write!(
929                formatter,
930                "source {source} declares a dynamic range its transfer contradicts"
931            ),
932            Self::InvalidFingerprint { source } => {
933                write!(formatter, "source {source} has an invalid fingerprint")
934            }
935            Self::InvalidProvider { source } => {
936                write!(formatter, "source {source} has an invalid provider")
937            }
938            Self::EmptyEffectResourceLocator { resource } => {
939                write!(formatter, "effect resource {resource} has an empty locator")
940            }
941            Self::UnknownEffectResource { clip, resource } => {
942                write!(
943                    formatter,
944                    "clip {clip} references unknown effect resource {resource}"
945                )
946            }
947            Self::EmptyClipRange { clip } => write!(formatter, "clip {clip} has an empty range"),
948            Self::NegativeClipRange { clip } => {
949                write!(formatter, "clip {clip} has a range before timeline origin")
950            }
951            Self::OverlappingClips {
952                track,
953                first,
954                second,
955            } => write!(
956                formatter,
957                "clips {first} and {second} overlap without a covering transition on track {track}"
958            ),
959            Self::UnknownClipSource { clip, source } => {
960                write!(formatter, "clip {clip} references unknown source {source}")
961            }
962            Self::IncompatibleClipSource {
963                clip,
964                track,
965                source,
966            } => {
967                write!(
968                    formatter,
969                    "clip {clip} cannot use source {source} on track {track}"
970                )
971            }
972            Self::InvalidVisualProperties { clip } => {
973                write!(formatter, "clip {clip} has invalid visual properties")
974            }
975            Self::InvalidAudioProperties { clip } => {
976                write!(formatter, "clip {clip} has invalid audio properties")
977            }
978            Self::InvalidTrackAudio { track } => {
979                write!(formatter, "track {track} has invalid audio bus settings")
980            }
981            Self::InvalidMasterAudio => {
982                write!(formatter, "the project master bus is invalid")
983            }
984            Self::InvalidAudioBus { bus } => {
985                write!(formatter, "bus {bus} has invalid audio bus settings")
986            }
987            Self::InvalidAudioSend { from, to } => {
988                write!(formatter, "{from} has an invalid send to bus {to}")
989            }
990            Self::UnknownAudioBus { from, bus } => {
991                write!(formatter, "{from} routes to unknown bus {bus}")
992            }
993            Self::DuplicateAudioBus { bus } => write!(formatter, "duplicate bus id {bus}"),
994            Self::AudioRoutingCycle { node } => {
995                write!(formatter, "{node} is on an audio routing cycle")
996            }
997            Self::TooManyAudioBuses { found, maximum } => {
998                write!(
999                    formatter,
1000                    "project has {found} audio buses; the maximum is {maximum}"
1001                )
1002            }
1003            Self::UnknownAudioSidechain {
1004                owner: Some(owner),
1005                sidechain,
1006            } => write!(formatter, "{owner} keys an effect from unknown {sidechain}"),
1007            Self::UnknownAudioSidechain {
1008                owner: None,
1009                sidechain,
1010            } => write!(
1011                formatter,
1012                "the master bus keys an effect from unknown {sidechain}"
1013            ),
1014            Self::UnknownComposition { clip, composition } => {
1015                write!(
1016                    formatter,
1017                    "clip {clip} references unknown composition {composition}"
1018                )
1019            }
1020            Self::InvalidClipComposition { clip } => {
1021                write!(
1022                    formatter,
1023                    "clip {clip} has an invalid composition reference"
1024                )
1025            }
1026            Self::DuplicateCompositionId(id) => write!(formatter, "duplicate composition ID {id}"),
1027            Self::InvalidNodeId { composition, node } => {
1028                write!(
1029                    formatter,
1030                    "composition {composition} has node ID {node}, which has a `/`"
1031                )
1032            }
1033            Self::DuplicateNodeId { composition, node } => {
1034                write!(
1035                    formatter,
1036                    "composition {composition} has duplicate node ID {node}"
1037                )
1038            }
1039            Self::TooManyCompositionNodes {
1040                composition,
1041                found,
1042                maximum,
1043            } => write!(
1044                formatter,
1045                "composition {composition} has {found} nodes; the maximum is {maximum}"
1046            ),
1047            Self::InvalidNode { composition, node } => {
1048                write!(
1049                    formatter,
1050                    "node {node} of composition {composition} has an invalid setting"
1051                )
1052            }
1053            Self::UnknownNodeInput {
1054                composition,
1055                node,
1056                input,
1057                target,
1058            } => write!(
1059                formatter,
1060                "input {input} of node {node} in composition {composition} names unknown node {target}"
1061            ),
1062            Self::CompositionCycle { composition, node } => write!(
1063                formatter,
1064                "node {node} of composition {composition} depends on its own output"
1065            ),
1066            Self::UnknownCompositionOutput { composition, node } => write!(
1067                formatter,
1068                "composition {composition} outputs unknown node {node}"
1069            ),
1070            Self::InvalidNodeSource {
1071                composition,
1072                node,
1073                source,
1074            } => write!(
1075                formatter,
1076                "node {node} of composition {composition} cannot show source {source}"
1077            ),
1078            Self::InvalidCompositionPorts { composition } => write!(
1079                formatter,
1080                "composition {composition} has invalid group inputs or controls"
1081            ),
1082            Self::UnknownGroupComposition {
1083                composition,
1084                node,
1085                target,
1086            } => write!(
1087                formatter,
1088                "group node {node} of composition {composition} names unknown composition {target}"
1089            ),
1090            Self::InvalidTimeOffset {
1091                composition,
1092                node,
1093                problem,
1094            } => write!(
1095                formatter,
1096                "time offset node {node} of composition {composition} {problem}"
1097            ),
1098            Self::CompositionUsesItself { composition } => {
1099                write!(formatter, "composition {composition} uses itself")
1100            }
1101            Self::CompositionNestedTooDeep {
1102                composition,
1103                maximum,
1104            } => write!(
1105                formatter,
1106                "composition {composition} nests compositions and time offsets more than {maximum} deep"
1107            ),
1108            Self::TooManyInlinedNodes { maximum } => write!(
1109                formatter,
1110                "compositions draw more than {maximum} nodes, counting each group and composition source as the nodes it draws and each time offset as the nodes it draws again"
1111            ),
1112            Self::FrameDrawsTooManyNodes {
1113                clip,
1114                found,
1115                maximum,
1116            } => write!(
1117                formatter,
1118                "the clips on the timeline where clip {clip} starts draw {found} composition nodes; a frame draws at most {maximum}"
1119            ),
1120            Self::InvalidCompositionSource { source } => {
1121                write!(
1122                    formatter,
1123                    "source {source} is not a valid composition source"
1124                )
1125            }
1126            Self::InvalidAdjustmentSource { source } => write!(
1127                formatter,
1128                "source {source} is not a valid adjustment source: it has only an id and its kind"
1129            ),
1130            Self::InvalidAdjustmentTransition { track, clip } => write!(
1131                formatter,
1132                "a transition on track {track} names adjustment clip {clip}, which only fades"
1133            ),
1134            Self::InvalidEffect { owner, id, problem } => {
1135                write!(formatter, "effect {id} in {owner} {problem}")
1136            }
1137            Self::UnknownNodeResource {
1138                composition,
1139                node,
1140                resource,
1141            } => write!(
1142                formatter,
1143                "node {node} of composition {composition} references unknown effect resource {resource}"
1144            ),
1145            Self::UnknownSourceResource { source, resource } => write!(
1146                formatter,
1147                "source {source} references unknown effect resource {resource}"
1148            ),
1149            Self::InvalidClipMask { clip } => write!(formatter, "clip {clip} has an invalid mask"),
1150            Self::InvalidMatte { clip } => write!(formatter, "clip {clip} has an invalid matte"),
1151            Self::InvalidFrameInterpolation { clip } => write!(
1152                formatter,
1153                "clip {clip} sets a frame interpolation outside a video track"
1154            ),
1155            Self::InvalidCaptions { track } => {
1156                write!(formatter, "track {track} has invalid captions")
1157            }
1158            Self::UnknownCaptionStyle { track, cue, name } => write!(
1159                formatter,
1160                "cue {cue} of track {track} names style {name:?}, which the track does not have"
1161            ),
1162            Self::InvalidCaptionCueStyle { track, cue } => write!(
1163                formatter,
1164                "cue {cue} of track {track} has a style out of range once its patch is applied"
1165            ),
1166            Self::InvalidTimeMap { clip, problem } => {
1167                use crate::TimeMapProblem as Problem;
1168                match problem {
1169                    Problem::FreezeOutsideSource => {
1170                        write!(formatter, "clip {clip} freezes outside its source range")
1171                    }
1172                    Problem::KeyframeCount { found } => write!(
1173                        formatter,
1174                        "the time curve of clip {clip} takes 2 to {} keyframes, not {found}",
1175                        crate::MAX_KEYFRAMES
1176                    ),
1177                    Problem::TimeOrder { keyframe } => write!(
1178                        formatter,
1179                        "keyframe {keyframe} of clip {clip}'s time curve is not after the one \
1180                         before it"
1181                    ),
1182                    Problem::OutsideSource { keyframe } => write!(
1183                        formatter,
1184                        "keyframe {keyframe} of clip {clip}'s time curve is outside its source \
1185                         range"
1186                    ),
1187                    Problem::Interpolation { keyframe } => write!(
1188                        formatter,
1189                        "keyframe {keyframe} of clip {clip}'s time curve has an invalid \
1190                         interpolation"
1191                    ),
1192                    Problem::Passes { found } => write!(
1193                        formatter,
1194                        "clip {clip}'s time curve plays {found} segments; the maximum is {}",
1195                        crate::MAX_KEYFRAMES
1196                    ),
1197                }
1198            }
1199            Self::TooManyLoopPasses {
1200                clip,
1201                found,
1202                maximum,
1203            } => write!(
1204                formatter,
1205                "clip {clip} loops {found} times; the maximum is {maximum}"
1206            ),
1207            Self::InvalidClipTransition { track } => {
1208                write!(formatter, "track {track} has an invalid clip transition")
1209            }
1210            Self::TransitionCycle { track, at } => write!(
1211                formatter,
1212                "transitions on track {track} active at {} ticks lead from clip to clip in a circle",
1213                at.ticks()
1214            ),
1215            Self::DuplicateTransition {
1216                track,
1217                first,
1218                second,
1219            } => write!(
1220                formatter,
1221                "clips {first} and {second} have more than one transition on track {track}"
1222            ),
1223            Self::TooManyDerivedCues { track } => write!(
1224                formatter,
1225                "track {track} derives more than {} caption cues or maps more than {} words",
1226                crate::MAX_DERIVED_CUES,
1227                crate::MAX_DATA_WORDS
1228            ),
1229            Self::UnwritableTime { ticks } => write!(
1230                formatter,
1231                "a time of {ticks} ticks is beyond {} either way",
1232                alpha2_time::MAX_SERIALIZED_TICKS
1233            ),
1234            Self::TooManyEffectResources { found, maximum } => write!(
1235                formatter,
1236                "project has {found} effect resources; the maximum is {maximum}"
1237            ),
1238            Self::TooManyCompositions { found, maximum } => write!(
1239                formatter,
1240                "project has {found} compositions; the maximum is {maximum}"
1241            ),
1242            Self::TooManySources { found, maximum } => {
1243                write!(
1244                    formatter,
1245                    "project has {found} sources; the maximum is {maximum}"
1246                )
1247            }
1248            Self::TooManyTracks { found, maximum } => {
1249                write!(
1250                    formatter,
1251                    "project has {found} tracks; the maximum is {maximum}"
1252                )
1253            }
1254            Self::TooManyClips { found, maximum } => {
1255                write!(
1256                    formatter,
1257                    "project has {found} clips; the maximum is {maximum}"
1258                )
1259            }
1260            Self::TooManyAudioEffects {
1261                found,
1262                maximum,
1263                at_ticks,
1264            } => write!(
1265                formatter,
1266                "project has {found} audio effects holding state at tick {at_ticks}; the maximum at once is {maximum}"
1267            ),
1268            Self::TooMuchAudioDelay {
1269                found_ms,
1270                maximum_ms,
1271                at_ticks,
1272            } => write!(
1273                formatter,
1274                "project audio effects hold {found_ms} ms of delay at tick {at_ticks}; the maximum at once is {maximum_ms} ms"
1275            ),
1276            Self::TooManyClipsOnTrack {
1277                track,
1278                found,
1279                maximum,
1280            } => write!(
1281                formatter,
1282                "track {track} has {found} clips; the maximum is {maximum}"
1283            ),
1284            Self::TooManyCaptionCues {
1285                track,
1286                found,
1287                maximum,
1288            } => write!(
1289                formatter,
1290                "track {track} has {found} caption cues; the maximum is {maximum}"
1291            ),
1292            Self::TooManyTransitions {
1293                track,
1294                found,
1295                maximum,
1296            } => write!(
1297                formatter,
1298                "track {track} has {found} transitions; the maximum is {maximum}"
1299            ),
1300            Self::ZeroCanvasDimension => formatter.write_str("canvas dimensions must be positive"),
1301            Self::CanvasDimensionTooLarge {
1302                width,
1303                height,
1304                maximum,
1305            } => write!(
1306                formatter,
1307                "canvas {width}x{height} exceeds the maximum edge {maximum}"
1308            ),
1309            Self::CanvasPixelBudgetExceeded { pixels, maximum } => write!(
1310                formatter,
1311                "canvas has {pixels} pixels; the maximum is {maximum}"
1312            ),
1313            Self::InvalidBackground => {
1314                formatter.write_str("background color components must be finite and within 0..=1")
1315            }
1316            Self::InvalidColorPipeline => {
1317                formatter.write_str("the HDR peak must be within 100..=10000 nits")
1318            }
1319            Self::InvalidShutter(problem) => problem.fmt(formatter),
1320            Self::TooManyDataSets { found, maximum } => write!(
1321                formatter,
1322                "project has {found} data sets; the maximum is {maximum}"
1323            ),
1324            Self::DuplicateDataSetId(data) => write!(formatter, "duplicate data set id {data}"),
1325            Self::InvalidDataSet { data, problem } => {
1326                write!(formatter, "data set {data} {problem}")
1327            }
1328            Self::InvalidDataBinding { owner, at, problem } => {
1329                write!(formatter, "{owner} {at} {problem}")
1330            }
1331            Self::UnsupportedSampleRate {
1332                found,
1333                minimum,
1334                maximum,
1335            } => write!(
1336                formatter,
1337                "sample rate {found} is outside {minimum}..={maximum}"
1338            ),
1339        }
1340    }
1341}
1342
1343impl std::error::Error for ModelError {}
1344
1345/// Failure while decoding a serialized project document.
1346#[derive(Debug)]
1347#[non_exhaustive]
1348pub enum ProjectLoadError {
1349    /// The JSON input exceeded the parser byte budget.
1350    TooLarge {
1351        /// Supplied UTF-8 byte length.
1352        bytes: usize,
1353        /// Largest accepted byte length.
1354        maximum: usize,
1355    },
1356    /// JSON syntax or shape was invalid.
1357    Json(serde_json::Error),
1358    /// The decoded document violated a model invariant.
1359    InvalidProject(ModelError),
1360}
1361
1362impl fmt::Display for ProjectLoadError {
1363    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1364        match self {
1365            Self::TooLarge { bytes, maximum } => {
1366                write!(
1367                    formatter,
1368                    "project JSON is {bytes} bytes; the maximum is {maximum}"
1369                )
1370            }
1371            Self::Json(error) => write!(formatter, "invalid project JSON: {error}"),
1372            Self::InvalidProject(error) => write!(formatter, "invalid project: {error}"),
1373        }
1374    }
1375}
1376
1377impl std::error::Error for ProjectLoadError {
1378    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1379        match self {
1380            Self::TooLarge { .. } => None,
1381            Self::Json(error) => Some(error),
1382            Self::InvalidProject(error) => Some(error),
1383        }
1384    }
1385}
1386
1387/// Why [`crate::Project::to_json`] wrote no document.
1388#[derive(Debug)]
1389#[non_exhaustive]
1390pub enum ProjectWriteError {
1391    /// The document would be longer than any loader accepts.
1392    TooLarge {
1393        /// Largest accepted byte length, which the document passes.
1394        maximum: usize,
1395    },
1396    /// Serialization failed.
1397    Json(serde_json::Error),
1398}
1399
1400impl fmt::Display for ProjectWriteError {
1401    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
1402        match self {
1403            Self::TooLarge { maximum } => write!(
1404                formatter,
1405                "the project JSON would be longer than {maximum} bytes, the maximum a project loads with"
1406            ),
1407            Self::Json(error) => write!(formatter, "the project does not serialize: {error}"),
1408        }
1409    }
1410}
1411
1412impl std::error::Error for ProjectWriteError {
1413    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
1414        match self {
1415            Self::TooLarge { .. } => None,
1416            Self::Json(error) => Some(error),
1417        }
1418    }
1419}