alpha2-model 0.6.1

Versioned project and timeline model for alpha2.
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
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
//! Where data can be bound. A value's type decides the kinds it can follow, and the clock its
//! keyframes are on decides the anchors.

use crate::animation::{Keyed, PlaceType, visit_keyed};
use crate::{
    BindingProblem, Clip, ClipId, Composition, CompositionId, DataAnchor, DataBinding, DataKind,
    DataOwner, DataSet, DataSetId, DataSpace, DataVia, EffectParamType, Generator, MediaKind,
    MediaSource, ModelError, NodeOp, Project, TrackKind, VisualProperties,
};
use alpha2_media::SourceId;
use std::collections::HashMap;

/// The clock a value's keyframes are on.
#[derive(Copy, Clone)]
enum Clock<'a> {
    /// Timeline time.
    Timeline,
    /// Composition time in this composition.
    Composition(&'a Composition),
    /// A generator's picture time, or a text's own time from its holder's start.
    Picture,
}

/// The type of the value a binding sits on.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
enum Slot {
    Point,
    Vec2,
    Scalar,
    /// An effect parameter of a known type, or of a host effect whose type the model lacks.
    Param(Option<EffectParamType>),
    Other,
}

/// A canvas-space binding: `follower`'s value depends on `via`'s visual.
struct Follow<'a> {
    follower: &'a ClipId,
    via: &'a ClipId,
    at: String,
}

struct Places<'a> {
    data: HashMap<&'a DataSetId, &'a DataSet>,
    clips: HashMap<&'a ClipId, (&'a Clip, TrackKind)>,
    compositions: HashMap<&'a CompositionId, &'a Composition>,
    follows: Vec<Follow<'a>>,
}

/// Checks every data set, then every binding, correction and caption words reference.
pub(crate) fn validate_data(
    project: &Project,
    sources: &HashMap<&SourceId, &MediaSource>,
) -> Result<(), ModelError> {
    if project.data.len() > crate::MAX_DATA_SETS {
        return Err(ModelError::TooManyDataSets {
            found: project.data.len(),
            maximum: crate::MAX_DATA_SETS,
        });
    }
    let mut data = HashMap::with_capacity(project.data.len());
    for set in &project.data {
        if data.insert(&set.id, set).is_some() {
            return Err(ModelError::DuplicateDataSetId(set.id.clone()));
        }
        let invalid = |problem| ModelError::InvalidDataSet {
            data: set.id.clone(),
            problem,
        };
        if let DataAnchor::Source { source_id } = &set.anchor {
            let source = sources
                .get(source_id)
                .ok_or_else(|| invalid(crate::DataProblem::UnknownSource(source_id.clone())))?;
            if !matches!(
                source.kind,
                MediaKind::Video | MediaKind::Audio | MediaKind::Image
            ) {
                return Err(invalid(crate::DataProblem::SourceWithoutMedia(
                    source_id.clone(),
                )));
            }
        }
        set.validate().map_err(invalid)?;
    }
    let mut places = Places {
        data,
        clips: project
            .timeline
            .tracks
            .iter()
            .flat_map(|track| {
                track
                    .clips
                    .iter()
                    .map(move |clip| (&clip.id, (clip, track.kind)))
            })
            .collect(),
        compositions: project
            .compositions
            .iter()
            .map(|composition| (&composition.id, composition))
            .collect(),
        follows: Vec::new(),
    };
    places.walk_timeline(project)?;
    places.walk_compositions(project)?;
    places.walk_sources(project)?;
    places.check_follow_graph()
}

/// The slot of a visited value; effect parameters are resolved by the caller.
const fn slot_of(place: PlaceType) -> Slot {
    match place {
        PlaceType::Point => Slot::Point,
        PlaceType::Vec2 => Slot::Vec2,
        PlaceType::Scalar => Slot::Scalar,
        PlaceType::Effect => Slot::Param(None),
        PlaceType::Other => Slot::Other,
    }
}

/// Calls `visit` with each data binding of a visual, by property path.
fn visual_bindings<'v>(
    prefix: &str,
    visual: &'v VisualProperties,
    visit: &mut dyn FnMut(&str, &'v DataBinding, PlaceType),
) {
    if let Some(binding) = visual.position.data() {
        visit(&format!("{prefix}.position"), binding, PlaceType::Point);
    }
    if let Some(binding) = visual.scale.data() {
        visit(&format!("{prefix}.scale"), binding, PlaceType::Vec2);
    }
    if let Some(binding) = visual.rotation_degrees.data() {
        visit(
            &format!("{prefix}.rotation_degrees"),
            binding,
            PlaceType::Scalar,
        );
    }
    if let Some(binding) = visual.opacity.data() {
        visit(&format!("{prefix}.opacity"), binding, PlaceType::Scalar);
    }
}

/// The declared type of a built-in effect's parameter, or `None` for a host effect.
fn param_type(effect: &crate::ClipEffect, name: &str) -> Option<EffectParamType> {
    crate::builtin_effect(&effect.id)
        .and_then(|spec| spec.param(name))
        .map(|param| param.param_type)
}

/// Collects `(path, binding, place)` of every non-constant value a walker reports.
fn collect(
    walk: impl FnOnce(&mut dyn FnMut(&str, Keyed<'_>)),
) -> Vec<(String, DataBinding, PlaceType)> {
    let mut found = Vec::new();
    walk(&mut |at, keyed| {
        if let Keyed::Data(binding, place) = keyed {
            found.push((at.to_owned(), binding.clone(), place));
        }
    });
    found
}

impl<'a> Places<'a> {
    #[allow(clippy::too_many_lines)]
    fn walk_timeline(&mut self, project: &'a Project) -> Result<(), ModelError> {
        for track in &project.timeline.tracks {
            for clip in &track.clips {
                let owner = DataOwner::Clip(clip.id.clone());
                let mut found = Vec::new();
                visual_bindings("visual", &clip.visual, &mut |at, binding, place| {
                    found.push((at.to_owned(), binding.clone(), place));
                });
                for (index, mask) in clip.masks.iter().enumerate() {
                    found.extend(collect(|visit| {
                        mask.shape.for_each_keyed(
                            &format!("masks[{index}].shape"),
                            &mut |at, keyed| {
                                visit(at, keyed);
                            },
                        );
                        visit_keyed(
                            &format!("masks[{index}].feather"),
                            &mask.feather,
                            &mut |at, keyed| {
                                visit(at, keyed);
                            },
                        );
                    }));
                }
                found.extend(collect(|visit| {
                    visit_keyed("audio.gain", &clip.audio.gain, &mut |at, keyed| {
                        visit(at, keyed);
                    });
                    visit_keyed("audio.pan", &clip.audio.pan, &mut |at, keyed| {
                        visit(at, keyed);
                    });
                }));
                for (at, binding, place) in &found {
                    self.check(
                        &owner,
                        at,
                        Clock::Timeline,
                        slot_of(*place),
                        binding,
                        Some(&clip.id),
                    )?;
                }
                if let Some(correction) = &clip.visual.correction {
                    self.check_correction(&owner, clip, &correction.data)?;
                }
            }
            for transition in &track.transitions {
                let owner = DataOwner::Transition {
                    track: track.id.clone(),
                    from: transition.from_clip.clone(),
                    to: transition.to_clip.clone(),
                };
                for (name, value) in &transition.effect.params {
                    if let Some(binding) = value.data() {
                        let slot = Slot::Param(param_type(&transition.effect, name));
                        let at = format!("effect.params.{name}");
                        self.check(&owner, &at, Clock::Timeline, slot, binding, None)?;
                    }
                }
            }
            let owner = DataOwner::Track(track.id.clone());
            for (at, value) in [
                ("audio.gain", &track.audio.gain),
                ("audio.pan", &track.audio.pan),
            ] {
                if let Some(binding) = value.data() {
                    self.check(&owner, at, Clock::Timeline, Slot::Scalar, binding, None)?;
                }
            }
            if let Some(captions) = &track.captions {
                let mut found = collect(|visit| {
                    crate::text_animation::for_each_keyed_animator(
                        &captions.style.animators,
                        "captions.style.animators",
                        &mut |at, keyed| visit(at, keyed),
                    );
                    for (name, patch) in &captions.styles {
                        if let Some(animators) = &patch.animators {
                            crate::text_animation::for_each_keyed_animator(
                                animators,
                                &format!("captions.styles.{name}.animators"),
                                &mut |at, keyed| visit(at, keyed),
                            );
                        }
                    }
                });
                for (at, binding, place) in found.drain(..) {
                    self.check(&owner, &at, Clock::Picture, slot_of(place), &binding, None)?;
                }
                if let Some(words) = &captions.from_words {
                    self.check_words(project, &owner, words)?;
                }
            }
        }
        if let Some(binding) = project.audio.gain.data() {
            self.check(
                &DataOwner::Master,
                "audio.gain",
                Clock::Timeline,
                Slot::Scalar,
                binding,
                None,
            )?;
        }
        Ok(())
    }

    fn walk_compositions(&mut self, project: &'a Project) -> Result<(), ModelError> {
        for composition in &project.compositions {
            for node in &composition.nodes {
                let owner = DataOwner::Node {
                    composition: composition.id.clone(),
                    node: node.id.clone(),
                };
                if let NodeOp::Transform { transform, .. } = &node.op
                    && transform.correction.is_some()
                {
                    return Err(ModelError::InvalidDataBinding {
                        owner,
                        at: "transform.correction".to_owned(),
                        problem: BindingProblem::FieldNotApplicable("correction"),
                    });
                }
                let found = collect(|visit| {
                    crate::lint::node_keyed(&node.op, &mut |at, keyed| visit(at, keyed));
                });
                for (at, binding, place) in found {
                    let clock = if at.starts_with("generator.") {
                        Clock::Picture
                    } else {
                        Clock::Composition(composition)
                    };
                    let slot = match place {
                        PlaceType::Effect => Slot::Param(self.node_param_type(&node.op, &at)),
                        other => slot_of(other),
                    };
                    self.check(&owner, &at, clock, slot, &binding, None)?;
                }
            }
        }
        Ok(())
    }

    fn walk_sources(&mut self, project: &'a Project) -> Result<(), ModelError> {
        for source in &project.sources {
            let Some(generator) = &source.generator else {
                continue;
            };
            let owner = DataOwner::Source(source.id.clone());
            let found = collect(|visit| match generator {
                Generator::Shapes { items } => {
                    crate::shapes::for_each_keyed_item(items, "items", &mut |at, keyed| {
                        visit(at, keyed);
                    });
                }
                Generator::Layout { root } => {
                    root.for_each_keyed("root", &mut |at, keyed| visit(at, keyed));
                }
                Generator::Text { style, .. } => crate::text_animation::for_each_keyed_animator(
                    &style.animators,
                    "style.animators",
                    &mut |at, keyed| visit(at, keyed),
                ),
                Generator::Fill { .. } => {}
            });
            for (at, binding, place) in found {
                self.check(&owner, &at, Clock::Picture, slot_of(place), &binding, None)?;
            }
        }
        Ok(())
    }

    /// The type of an effect node's parameter or a group node's control at path `at`.
    fn node_param_type(&self, op: &NodeOp, at: &str) -> Option<EffectParamType> {
        match op {
            NodeOp::Effect { effect, .. } | NodeOp::Transition { effect, .. } => {
                param_type(effect, at.strip_prefix("effect.params.")?)
            }
            NodeOp::Group { composition, .. } => {
                let name = at.strip_prefix("controls.")?;
                let group = self.compositions.get(composition)?;
                let control = group.controls.iter().find(|control| control.name == name)?;
                let target = group.nodes.iter().find(|node| node.id == control.node)?;
                match &target.op {
                    NodeOp::Effect { effect, .. } | NodeOp::Transition { effect, .. } => {
                        param_type(effect, &control.param)
                    }
                    _ => None,
                }
            }
            _ => None,
        }
    }

    fn data_set(
        &self,
        owner: &DataOwner,
        at: &str,
        id: &DataSetId,
    ) -> Result<&'a DataSet, ModelError> {
        self.data
            .get(id)
            .copied()
            .ok_or_else(|| ModelError::InvalidDataBinding {
                owner: owner.clone(),
                at: at.to_owned(),
                problem: BindingProblem::UnknownData,
            })
    }

    /// Checks one binding at `at`; `follower` owns the value, for the canvas dependency graph.
    #[allow(clippy::too_many_lines)]
    fn check(
        &mut self,
        owner: &DataOwner,
        at: &str,
        clock: Clock<'a>,
        slot: Slot,
        binding: &DataBinding,
        follower: Option<&'a ClipId>,
    ) -> Result<(), ModelError> {
        let refuse = |problem| ModelError::InvalidDataBinding {
            owner: owner.clone(),
            at: at.to_owned(),
            problem,
        };
        let set = self.data_set(owner, at, &binding.data)?;
        let kind = set.kind();
        let accepted: &[DataKind] = match slot {
            Slot::Point | Slot::Vec2 | Slot::Param(Some(EffectParamType::Vec2) | None) => {
                &[DataKind::Points, DataKind::Scalars]
            }
            Slot::Scalar | Slot::Param(Some(EffectParamType::Float)) => &[DataKind::Scalars],
            Slot::Param(Some(_)) | Slot::Other => {
                return Err(refuse(BindingProblem::TypeRefusesData));
            }
        };
        if !accepted.contains(&kind) {
            return Err(refuse(BindingProblem::WrongKind(kind)));
        }
        let samples = set
            .values
            .samples()
            .expect("points and scalars are sampled");
        if samples.channel(&binding.channel).is_none() {
            return Err(refuse(BindingProblem::UnknownChannel));
        }
        if !binding.fields_valid() {
            return Err(refuse(BindingProblem::InvalidRemap));
        }
        let points = kind == DataKind::Points;
        if points && binding.remap.is_some() {
            return Err(refuse(BindingProblem::FieldNotApplicable("remap")));
        }
        if !points && binding.offset.is_some() {
            return Err(refuse(BindingProblem::FieldNotApplicable("offset")));
        }
        if !points && binding.space.is_some() {
            return Err(refuse(BindingProblem::FieldNotApplicable("space")));
        }
        let source = match (&set.anchor, clock) {
            (_, Clock::Picture) | (DataAnchor::Timeline, Clock::Composition(_)) => {
                return Err(refuse(BindingProblem::ClockRefusesAnchor));
            }
            (DataAnchor::Timeline, Clock::Timeline) => {
                if binding.via.is_some() {
                    return Err(refuse(BindingProblem::ViaForbidden));
                }
                if binding.space.is_some() {
                    return Err(refuse(BindingProblem::FieldNotApplicable("space")));
                }
                return Ok(());
            }
            (DataAnchor::Source { source_id }, _) => source_id,
        };
        let via = binding
            .via
            .as_ref()
            .ok_or_else(|| refuse(BindingProblem::ViaRequired))?;
        if points && binding.space.is_none() {
            return Err(refuse(BindingProblem::SpaceRequired));
        }
        match (via, clock) {
            (DataVia::Clip(clip), Clock::Timeline) => {
                let (clip, kind) = self
                    .clips
                    .get(clip)
                    .copied()
                    .ok_or_else(|| refuse(BindingProblem::UnknownViaClip))?;
                if &clip.source_id != source {
                    return Err(refuse(BindingProblem::ViaShowsOtherSource));
                }
                if binding.space == Some(DataSpace::Canvas) {
                    if kind != TrackKind::Video {
                        return Err(refuse(BindingProblem::ViaWrongReader));
                    }
                    match follower {
                        Some(follower) if follower == &clip.id => {
                            return Err(refuse(BindingProblem::Cycle));
                        }
                        Some(follower) => self.follows.push(Follow {
                            follower,
                            via: &clip.id,
                            at: at.to_owned(),
                        }),
                        None => {}
                    }
                }
                Ok(())
            }
            (DataVia::Node(node), Clock::Composition(composition)) => {
                let reads = composition
                    .nodes
                    .iter()
                    .find(|candidate| &candidate.id == node);
                let Some(NodeOp::Media { source_id, .. }) = reads.map(|node| &node.op) else {
                    return Err(refuse(BindingProblem::InvalidViaNode));
                };
                if source_id != source {
                    return Err(refuse(BindingProblem::ViaShowsOtherSource));
                }
                if binding.space == Some(DataSpace::Canvas) {
                    return Err(refuse(BindingProblem::FieldNotApplicable("space")));
                }
                Ok(())
            }
            _ => Err(refuse(BindingProblem::ViaWrongReader)),
        }
    }

    fn check_correction(
        &self,
        owner: &DataOwner,
        clip: &Clip,
        id: &DataSetId,
    ) -> Result<(), ModelError> {
        let at = "visual.correction";
        let set = self.data_set(owner, at, id)?;
        let refuse = |problem| ModelError::InvalidDataBinding {
            owner: owner.clone(),
            at: at.to_owned(),
            problem,
        };
        if set.kind() != DataKind::Similarity {
            return Err(refuse(BindingProblem::WrongKind(set.kind())));
        }
        if set.anchor.source() != Some(&clip.source_id) {
            return Err(refuse(BindingProblem::NotOwnSource));
        }
        Ok(())
    }

    fn check_words(
        &self,
        project: &Project,
        owner: &DataOwner,
        words: &crate::CaptionsFromWords,
    ) -> Result<(), ModelError> {
        let at = "captions.from_words";
        let set = self.data_set(owner, at, &words.data)?;
        let refuse = |problem| ModelError::InvalidDataBinding {
            owner: owner.clone(),
            at: at.to_owned(),
            problem,
        };
        if set.kind() != DataKind::Words {
            return Err(refuse(BindingProblem::WrongKind(set.kind())));
        }
        let track_ok = match (&set.anchor, &words.track) {
            (DataAnchor::Source { .. }, Some(track)) => project
                .timeline
                .tracks
                .iter()
                .any(|candidate| &candidate.id == track && candidate.kind != TrackKind::Caption),
            (DataAnchor::Timeline, None) => true,
            _ => false,
        };
        if !track_ok || words.max_chars == 0 {
            return Err(refuse(BindingProblem::InvalidCaptionTrack));
        }
        Ok(())
    }

    /// Refuses a cycle of canvas-space follows, then a clip that follows more than
    /// [`crate::MAX_FOLLOW_DEPTH`] links deep, naming the first such follow in document order.
    fn check_follow_graph(&self) -> Result<(), ModelError> {
        let graph = FollowGraph::new(&self.follows);
        let (component, depth) = graph.components_and_depths();
        let refuse = |follow: &Follow<'_>, problem| ModelError::InvalidDataBinding {
            owner: DataOwner::Clip(follow.follower.clone()),
            at: follow.at.clone(),
            problem,
        };
        let on_cycle = graph
            .edges
            .iter()
            .position(|&(from, to)| component[from] == component[to]);
        if let Some(at) = on_cycle {
            return Err(refuse(&self.follows[at], BindingProblem::Cycle));
        }
        let too_deep = graph
            .edges
            .iter()
            .position(|&(from, _)| depth[from] > crate::MAX_FOLLOW_DEPTH);
        if let Some(at) = too_deep {
            return Err(refuse(&self.follows[at], BindingProblem::TooDeep));
        }
        Ok(())
    }
}

/// The follows as edges from follower to `via` between dense clip indices, in document order.
struct FollowGraph {
    edges: Vec<(usize, usize)>,
    /// `targets[first[node]..first[node + 1]]` are the clips `node` follows.
    first: Vec<usize>,
    targets: Vec<usize>,
}

impl FollowGraph {
    fn new(follows: &[Follow<'_>]) -> Self {
        let mut nodes: HashMap<&ClipId, usize> = HashMap::new();
        let mut node = |clip| {
            let next = nodes.len();
            *nodes.entry(clip).or_insert(next)
        };
        let edges: Vec<(usize, usize)> = follows
            .iter()
            .map(|follow| (node(follow.follower), node(follow.via)))
            .collect();
        let mut first = vec![0; nodes.len() + 1];
        for &(from, _) in &edges {
            first[from + 1] += 1;
        }
        for at in 1..first.len() {
            first[at] += first[at - 1];
        }
        let mut fill = first.clone();
        let mut targets = vec![0; edges.len()];
        for &(from, to) in &edges {
            targets[fill[from]] = to;
            fill[from] += 1;
        }
        Self {
            edges,
            first,
            targets,
        }
    }

    fn successors(&self, node: usize) -> &[usize] {
        &self.targets[self.first[node]..self.first[node + 1]]
    }

    /// Each clip's strongly connected component and its follow depth, in one iterative Tarjan
    /// pass. A depth is the longest chain of links from the clip and is meaningful only when no
    /// component holds an edge.
    fn components_and_depths(&self) -> (Vec<usize>, Vec<usize>) {
        const UNSEEN: usize = usize::MAX;
        let count = self.first.len() - 1;
        let mut order = vec![UNSEEN; count];
        let mut low = vec![0; count];
        let mut component = vec![UNSEEN; count];
        let mut depth = vec![0_usize; count];
        let mut open = Vec::new();
        let mut calls: Vec<(usize, usize)> = Vec::new();
        let mut visited = 0;
        let mut components = 0;
        for root in 0..count {
            if order[root] != UNSEEN {
                continue;
            }
            order[root] = visited;
            low[root] = visited;
            visited += 1;
            open.push(root);
            calls.push((root, 0));
            while let Some((node, next)) = calls.last_mut() {
                let node = *node;
                if let Some(&target) = self.successors(node).get(*next) {
                    *next += 1;
                    if order[target] == UNSEEN {
                        order[target] = visited;
                        low[target] = visited;
                        visited += 1;
                        open.push(target);
                        calls.push((target, 0));
                    } else if component[target] == UNSEEN {
                        low[node] = low[node].min(order[target]);
                    }
                    continue;
                }
                calls.pop();
                if let Some(&(parent, _)) = calls.last() {
                    low[parent] = low[parent].min(low[node]);
                }
                depth[node] = self
                    .successors(node)
                    .iter()
                    .map(|&target| depth[target].saturating_add(1))
                    .max()
                    .unwrap_or(0);
                if low[node] == order[node] {
                    while let Some(member) = open.pop() {
                        component[member] = components;
                        if member == node {
                            break;
                        }
                    }
                    components += 1;
                }
            }
        }
        (component, depth)
    }
}