use std::path::PathBuf;
use ironlab_ir::{IrError, ValidationReport};
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error(
"cannot tell the figure format of {}: use the extension .fig for Protocol Buffers or .json (such as .fig.json) for JSON",
.0.display()
)]
UnsupportedFormat(PathBuf),
#[error(transparent)]
Ir(#[from] IrError),
#[error(transparent)]
Export(#[from] ironlab_viewer::ExportError),
#[error("viewer failed: {0}")]
Viewer(#[from] eframe::Error),
#[error("the figure is invalid: {}", summarise(.0))]
Invalid(ValidationReport),
#[error(
"a plane of {} rows and {} columns does not match pixels of {} rows and {} columns",
.found[0], .found[1], .expected[0], .expected[1]
)]
PlaneShapeMismatch {
expected: [usize; 2],
found: [usize; 2],
},
#[error(
"the pixel in row {row} and column {col} has a non-finite {}",
describe_channel(*.channel)
)]
NonFiniteComponent {
row: usize,
col: usize,
channel: usize,
},
}
fn summarise(report: &ValidationReport) -> String {
report
.errors
.iter()
.map(|issue| issue.message.as_str())
.collect::<Vec<_>>()
.join("; ")
}
fn describe_channel(channel: usize) -> String {
match channel {
0 => "red component".to_owned(),
1 => "green component".to_owned(),
2 => "blue component".to_owned(),
3 => "alpha".to_owned(),
other => format!("component in channel {other}"),
}
}