lensbench_project/
expand_error.rs1use crate::{block_id, Block, NodePath, NodeStep};
4
5#[derive(Debug, Clone, PartialEq)]
7pub struct ExpandError {
8 pub error: lensbench_core::Error,
9 pub block: Option<BlockAt>,
10}
11
12#[derive(Debug, Clone, PartialEq, Eq)]
14pub struct BlockAt {
15 pub node: NodePath,
16 pub name: String,
17}
18
19impl BlockAt {
20 pub(crate) fn new(blocks: &[Block], node: NodePath) -> BlockAt {
21 let mut parts = Vec::new();
22 let mut v = blocks;
23 for (k, st) in node.0.iter().enumerate() {
24 let (arm, i) = match *st {
25 NodeStep::Block(i) => (None, i),
26 NodeStep::Arm(a, i) => (Some(a), i),
27 };
28 if let Some(a) = arm {
29 parts.push(["t", "r", "d"][usize::from(a).min(2)].to_string());
30 }
31 let Some(b) = v.get(i) else { break };
32 let id = block_id(b);
33 parts.push(if id.is_empty() {
34 format!("#{}", i + 1)
35 } else {
36 id.to_string()
37 });
38 v = match (b, arm) {
39 (Block::Group { blocks, .. }, _) => blocks,
40 (Block::Splitter { spec, .. }, _) => match node.0.get(k + 1) {
41 Some(NodeStep::Arm(0, _)) => &spec.t,
42 Some(NodeStep::Arm(1, _)) => &spec.r,
43 Some(NodeStep::Arm(_, _)) => &spec.d,
44 _ => &[],
45 },
46 _ => &[],
47 };
48 }
49 BlockAt {
50 node,
51 name: parts.join("/"),
52 }
53 }
54}
55
56impl ExpandError {
57 pub fn kind(&self) -> &'static str {
59 self.error.kind()
60 }
61 pub fn message(&self) -> Option<&str> {
63 self.error.message()
64 }
65 pub fn code(&self) -> &'static str {
67 self.error.code()
68 }
69 pub fn into_error(self) -> lensbench_core::Error {
71 match self.block {
72 Some(b) => {
73 let at = b.name;
74 self.error.map_message(|e| format!("block {at}: {e}"))
75 }
76 None => self.error,
77 }
78 }
79}
80
81impl std::fmt::Display for ExpandError {
82 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83 self.clone().into_error().fmt(f)
84 }
85}
86
87impl std::error::Error for ExpandError {}
88
89impl From<lensbench_core::Error> for ExpandError {
90 fn from(error: lensbench_core::Error) -> Self {
91 ExpandError { error, block: None }
92 }
93}
94
95impl From<ExpandError> for lensbench_core::Error {
96 fn from(e: ExpandError) -> Self {
97 e.into_error()
98 }
99}
100
101impl From<ExpandError> for String {
102 fn from(e: ExpandError) -> Self {
103 e.to_string()
104 }
105}
106
107#[derive(Debug, Clone, PartialEq, Eq)]
109pub struct Note {
110 pub kind: NoteKind,
111 pub block: Option<String>,
113 pub text: String,
115}
116
117#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
119#[non_exhaustive]
120pub enum NoteKind {
121 Pickup,
123 Environment,
125 Coating,
127 Unit,
129 SurfaceOp,
131 Glrs,
133 Mechanics,
135 Medium,
138}
139
140impl Note {
141 pub fn new(kind: NoteKind, block: Option<String>, text: String) -> Note {
142 Note { kind, block, text }
143 }
144
145 pub(crate) fn all(kind: NoteKind, block: Option<String>, v: Vec<String>) -> Vec<Note> {
146 v.into_iter()
147 .map(|t| Note::new(kind, block.clone(), t))
148 .collect()
149 }
150}
151
152impl From<Note> for String {
153 fn from(n: Note) -> String {
154 n.text
155 }
156}
157
158impl std::fmt::Display for Note {
159 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
160 f.write_str(&self.text)
161 }
162}
163
164pub(crate) fn in_medium_notes(
167 s: &lensbench_core::system::System,
168 map: &[crate::SurfOrigin],
169) -> Vec<Note> {
170 let img = s.image();
171 let faces: Vec<usize> = (1..img)
173 .filter(|&j| !s.surfaces[j].is_coord_break())
174 .collect();
175 let node = |j: usize| map.get(j).and_then(|o| o.face.as_ref()).map(|f| &f.node);
176 let name = |j: usize| {
177 map.get(j)
178 .and_then(|o| o.blocks.first().cloned())
179 .unwrap_or_else(|| format!("surface {j}"))
180 };
181 let mut out = Vec::new();
182 for w in faces.windows(2) {
183 let (a, b) = (w[0], w[1]);
184 let m = &s.surfaces[a].mat;
185 if !m.is_medium() || m.is_air() || node(a).is_none() || node(a) == node(b) {
186 continue;
187 }
188 let (na, nb) = (name(a), name(b));
189 out.push(Note::new(
190 NoteKind::Medium,
191 Some(nb.clone()),
192 format!(
193 "{a} ends in {m} (no air after it), so {b} sits in that medium",
194 a = na,
195 m = m.label(),
196 b = nb
197 ),
198 ));
199 }
200 out
201}