Skip to main content

u_nesting_core/
error.rs

1//! Error types for U-Nesting.
2
3use thiserror::Error;
4
5/// Result type alias for U-Nesting operations.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// `value` inside `[min, max]` (a NaN is outside every range), or a
9/// `ConfigError` naming the field and the range.
10pub(crate) fn check_range(name: &str, value: f64, min: f64, max: f64) -> Result<()> {
11    if value >= min && value <= max {
12        Ok(())
13    } else {
14        Err(Error::ConfigError(format!(
15            "{name} must be in [{min}, {max}], got {value}"
16        )))
17    }
18}
19
20/// `value` finite and at least `min`, or a `ConfigError` naming the field.
21pub(crate) fn check_at_least(name: &str, value: f64, min: f64) -> Result<()> {
22    if value.is_finite() && value >= min {
23        Ok(())
24    } else {
25        Err(Error::ConfigError(format!(
26            "{name} must be a finite number >= {min}, got {value}"
27        )))
28    }
29}
30
31/// Errors that can occur during nesting/packing operations.
32#[derive(Debug, Error)]
33pub enum Error {
34    /// Invalid geometry provided.
35    #[error("Invalid geometry: {0}")]
36    InvalidGeometry(String),
37
38    /// Invalid boundary provided.
39    #[error("Invalid boundary: {0}")]
40    InvalidBoundary(String),
41
42    /// Configuration error.
43    #[error("Configuration error: {0}")]
44    ConfigError(String),
45
46    /// NFP computation failed.
47    #[error("NFP computation failed: {0}")]
48    NfpError(String),
49
50    /// No valid placement found.
51    #[error("No valid placement found for geometry: {0}")]
52    NoPlacement(String),
53
54    /// Computation cancelled.
55    #[error("Computation cancelled")]
56    Cancelled,
57
58    /// Timeout exceeded.
59    #[error("Timeout exceeded after {0}ms")]
60    Timeout(u64),
61
62    /// Serialization error.
63    #[cfg(feature = "serde")]
64    #[error("Serialization error: {0}")]
65    SerializationError(String),
66
67    /// Internal error.
68    #[error("Internal error: {0}")]
69    Internal(String),
70}