1use crate::edit_spec::{BlockRef, PickupOp, Ref, SolveOp, SolveWhat, SurfaceOp};
6
7pub fn put_solve(ops: &mut Vec<SurfaceOp>, o: SolveOp<BlockRef>, shown: bool) {
10 let (at, c) = (o.surface.clone(), o.is_curv());
11 let own =
12 |q: &SurfaceOp| matches!(q, SurfaceOp::AddSolve(q) if q.surface == at && q.is_curv() == c);
13 let from_source = shown && !ops.iter().any(own);
14 ops.retain(|q| !own(q));
15 let removed = ops.iter().any(|q| solve_of(q, &at, c));
16 if from_source && !removed {
17 let what = if c { SolveWhat::C } else { SolveWhat::T };
18 ops.push(SurfaceOp::RemoveSolve { surface: at, what });
19 }
20 ops.push(SurfaceOp::AddSolve(o));
21}
22
23pub fn put_pickup(ops: &mut Vec<SurfaceOp>, o: PickupOp<BlockRef>, shown: bool) {
25 let at = o.dst.clone();
26 let what = o.what.clone();
27 let own = |q: &SurfaceOp| matches!(q, SurfaceOp::AddPickup(q) if q.dst == at && q.what == what);
28 let from_source = shown && !ops.iter().any(own);
29 ops.retain(|q| !own(q));
30 let removed = ops.iter().any(|q| pickup_of(q, &at, &what));
31 if from_source && !removed {
32 ops.push(SurfaceOp::RemovePickup { dst: at, what });
33 }
34 ops.push(SurfaceOp::AddPickup(o));
35}
36
37pub fn drop_solve(ops: &mut Vec<SurfaceOp>, i: usize, what: SolveWhat) {
39 drop_solve_at(ops, BlockRef::Sys(Ref::Index(i)), what)
40}
41
42pub fn drop_solve_at(ops: &mut Vec<SurfaceOp>, here: BlockRef, what: SolveWhat) {
44 let c = what == SolveWhat::C;
45 let own = |q: &SurfaceOp| matches!(q, SurfaceOp::AddSolve(q) if q.surface == here && q.is_curv() == c);
46 if ops.iter().any(own) {
47 ops.retain(|q| !own(q));
48 } else {
49 ops.retain(|q| !solve_of(q, &here, c));
50 ops.push(SurfaceOp::RemoveSolve {
51 surface: here,
52 what,
53 });
54 }
55}
56
57pub fn drop_pickup(ops: &mut Vec<SurfaceOp>, i: usize, what: String) {
59 drop_pickup_at(ops, BlockRef::Sys(Ref::Index(i)), what)
60}
61
62pub fn drop_pickup_at(ops: &mut Vec<SurfaceOp>, here: BlockRef, what: String) {
64 let own =
65 |q: &SurfaceOp| matches!(q, SurfaceOp::AddPickup(q) if q.dst == here && q.what == what);
66 if ops.iter().any(own) {
67 ops.retain(|q| !own(q));
68 } else {
69 ops.retain(|q| !pickup_of(q, &here, &what));
70 ops.push(SurfaceOp::RemovePickup { dst: here, what });
71 }
72}
73
74fn solve_of(q: &SurfaceOp, at: &BlockRef, curv: bool) -> bool {
76 match q {
77 SurfaceOp::AddSolve(q) => q.surface == *at && q.is_curv() == curv,
78 SurfaceOp::RemoveSolve { surface, what } => {
79 surface == at && (*what == SolveWhat::C) == curv
80 }
81 _ => false,
82 }
83}
84
85fn pickup_of(q: &SurfaceOp, at: &BlockRef, what: &str) -> bool {
87 match q {
88 SurfaceOp::AddPickup(q) => q.dst == *at && q.what == what,
89 SurfaceOp::RemovePickup { dst, what: w } => dst == at && w == what,
90 _ => false,
91 }
92}
93
94#[cfg(test)]
95mod tests {
96 use super::*;
97
98 fn solve(i: usize) -> SolveOp<BlockRef> {
99 SolveOp {
100 surface: BlockRef::Sys(Ref::Index(i)),
101 other: None,
102 kind: Some("marginal_height".into()),
103 value: None,
104 param: None,
105 macro_ref: None,
106 }
107 }
108
109 fn pickup(i: usize) -> PickupOp<BlockRef> {
110 PickupOp {
111 dst: BlockRef::Sys(Ref::Index(i)),
112 src: BlockRef::Sys(Ref::Index(1)),
113 what: "t".into(),
114 scale: None,
115 offset: None,
116 from: None,
117 }
118 }
119
120 #[test]
122 fn solves_replace_and_remove_the_source() {
123 let mut ops = Vec::new();
124 put_solve(&mut ops, solve(2), false);
125 put_solve(&mut ops, solve(2), true);
126 assert_eq!(ops.len(), 1);
127 drop_solve(&mut ops, 2, SolveWhat::T);
128 assert!(ops.is_empty());
129 put_solve(&mut ops, solve(3), true);
131 assert!(matches!(ops[0], SurfaceOp::RemoveSolve { .. }));
132 assert_eq!(ops.len(), 2);
133 drop_solve(&mut ops, 3, SolveWhat::T);
134 assert_eq!(ops.len(), 1);
135 assert!(matches!(ops[0], SurfaceOp::RemoveSolve { .. }));
136 drop_solve(&mut ops, 3, SolveWhat::T);
138 drop_solve(&mut ops, 3, SolveWhat::C);
139 assert_eq!(ops.len(), 2);
140 }
141
142 #[test]
144 fn pickups_replace_and_remove_the_source() {
145 let mut ops = Vec::new();
146 put_pickup(&mut ops, pickup(2), true);
147 put_pickup(&mut ops, pickup(2), true);
148 assert_eq!(ops.len(), 2);
149 drop_pickup(&mut ops, 2, "t".into());
150 assert_eq!(ops.len(), 1);
151 assert!(matches!(ops[0], SurfaceOp::RemovePickup { .. }));
152 }
153}