forge
Explore
Log in

Don't you dare go Hollow

A merged group stays one placement per lane

Browse files at this commit

b1724e2b74e5394dfc90e0ee3256a18de8191c2aAhmet Kaan GümüşParent: 675eb6f
  • fix
    • a merged group stays one placement per lane
Show full message
🐛 fix(engine): a merged group stays one placement per lane

group_all folded pre-existing links into the new id transitively with
no check on what the merge produced: two captions on one sub lane, each
grouped with a different picked clip, both took the id -- a state
links_are_consistent only debug_asserts against, where group_of sees
the first and delete_members orphans the second. The merge is now
counted per lane before anything mutates and refused by name when a
lane would carry two, the same refusal remove_lane speaks.
crates/engine/src/project.rsmodified+90 -0
@@ -3037,6 +3037,47 @@ impl Project {
3037 }3037 }
3038 })3038 })
3039 .collect();3039 .collect();
3040+ // The picks are already one per lane (`seen`, above), but a pick's
3041+ // *existing* group can carry a second member onto a lane another
3042+ // pick (or another existing group in the merge) already occupies --
3043+ // two captions on one sub lane, say, each grouped with a different
3044+ // clip before this call. Merging both in would leave that lane with
3045+ // two members of one id, which `links_are_consistent` may never see:
3046+ // refused by name, before anything moves, rather than a group closed
3047+ // broken.
3048+ let labels = handles(&self.lanes);
3049+ let mut per_lane: std::collections::HashMap<usize, u32> = std::collections::HashMap::new();
3050+ for (li, data) in self.lanes.iter().enumerate() {
3051+ let n = data.clips.iter().filter(|c| c.link.is_some() && links.contains(&c.link)).count()
3052+ + data.subs.iter().filter(|s| s.link.is_some() && links.contains(&s.link)).count();
3053+ if n > 0 {
3054+ per_lane.insert(li, n as u32);
3055+ }
3056+ }
3057+ // A pick that carries no link of its own is invisible to the scan
3058+ // above (there is no `Some` to match): it still claims its lane.
3059+ for &(lane, _) in picks {
3060+ let li = self.index(lane).expect("checked above");
3061+ per_lane.entry(li).or_insert(0);
3062+ if !self.lanes[li]
3063+ .clips
3064+ .iter()
3065+ .any(|c| c.link.is_some() && links.contains(&c.link))
3066+ && !self.lanes[li]
3067+ .subs
3068+ .iter()
3069+ .any(|s| s.link.is_some() && links.contains(&s.link))
3070+ {
3071+ *per_lane.get_mut(&li).expect("just inserted") += 1;
3072+ }
3073+ }
3074+ if let Some((&li, _)) = per_lane.iter().find(|&(_, &n)| n > 1) {
3075+ return Err(format!(
3076+ "a group is one placement per lane: merging these groups would put two on {}",
3077+ labels[li].label()
3078+ )
3079+ .into());
3080+ }
3040 self.snapshot();3081 self.snapshot();
3041 let id = self.new_link();3082 let id = self.new_link();
3042 // Whatever any pick was grouped with comes along, for [`group`]'s3083 // Whatever any pick was grouped with comes along, for [`group`]'s
@@ -5762,6 +5803,55 @@ mod tests {
5762 assert!(links_are_consistent(&p.lanes).is_ok());5803 assert!(links_are_consistent(&p.lanes).is_ok());
5763 }5804 }
5764 5805
5806+ /// Two captions on one sub lane, each already grouped with a *different*
5807+ /// clip: `group_all` over both those clips would merge the two existing
5808+ /// groups into one id and leave both captions on `s1` carrying it --
5809+ /// `links_are_consistent`'s one-member-per-lane law broken the moment the
5810+ /// group closes. Refused by name, and nothing moves.
5811+ #[test]
5812+ fn group_all_refuses_a_merge_that_doubles_up_a_lane() {
5813+ let caption = |start: u32, frames: u32| SubClip {
5814+ start,
5815+ frames,
5816+ track: 0,
5817+ in_us: 0,
5818+ out_us: i64::from(frames) * 1_000_000,
5819+ link: None,
5820+ };
5821+ let track = || SubtitleTrack {
5822+ path: FILE.into(),
5823+ track: None,
5824+ language: "eng".into(),
5825+ name: String::new(),
5826+ label: "eng".into(),
5827+ cues: Vec::new(),
5828+ bitmap: false,
5829+ refused: None,
5830+ };
5831+ let mut p = Project::single(FILE, 90).with_subtitles(vec![track()]);
5832+ let v2 = p.add_lane(LaneKind::Video);
5833+ let s1 = p.add_lane(LaneKind::Subtitle);
5834+ assert!(p.place(v2, 0, clip(0, 0, 90, 0)));
5835+ p.place_sub(s1, 0, caption(0, 30)).expect("placed");
5836+ p.place_sub(s1, 30, caption(30, 30)).expect("placed");
5837+ // V1 grouped with the first caption, v2 with the second -- two
5838+ // separate groups, each already one member per lane.
5839+ p.group_all(&[(Lane::V1, 0), (s1, 0)]).expect("grouped");
5840+ p.group_all(&[(v2, 0), (s1, 1)]).expect("grouped");
5841+ let (v1_link, v2_link) = (p.lane(Lane::V1)[0].link, p.lane(v2)[0].link);
5842+
5843+ let history = p.history.len();
5844+ let err = p
5845+ .group_all(&[(Lane::V1, 0), (v2, 0)])
5846+ .unwrap_err()
5847+ .to_string();
5848+ assert!(err.contains("one placement per lane"), "{err}");
5849+ assert_eq!(p.history.len(), history, "the refusal costs no undo step");
5850+ assert_eq!(p.lane(Lane::V1)[0].link, v1_link, "the first group untouched");
5851+ assert_eq!(p.lane(v2)[0].link, v2_link, "the second group untouched");
5852+ assert!(links_are_consistent(&p.lanes).is_ok());
5853+ }
5854+
5765 /// A split hands the caption of a group the same ids it hands the clips:5855 /// A split hands the caption of a group the same ids it hands the clips:
5766 /// the left halves keep the group, the right halves share a fresh one.5856 /// the left halves keep the group, the right halves share a fresh one.
5767 #[test]5857 #[test]