mappers/
errors.rs

1use thiserror::Error;
2
3/// An interface for errors used within the crate and that the user may face.
4#[derive(Error, Debug)]
5pub enum ProjectionError {
6    /// Returned when given parameter is not finite.
7    #[error("Projection parameter {0} is not finite")]
8    ParamNotFinite(&'static str),
9
10    /// Returned when required parameter is missing.
11    #[error("Parameter {0} must be definied")]
12    ParamRequired(&'static str),
13
14    /// Returned when parameter is too big or too small.
15    #[error("Parameter {0} is out of required range {1}..{2}")]
16    ParamOutOfRange(&'static str, f64, f64),
17
18    /// Returned when the projection definition parameters are incorrect.
19    #[error("Incorrect projection parameters: {0}")]
20    IncorrectParams(&'static str),
21
22    /// Returned when projection of given values results in not finite results.
23    #[error("Attempt to project lon: {0} lat: {1} results in not finite result")]
24    ProjectionImpossible(f64, f64),
25
26    /// Returned when inverse projection of given values results in not finite results.
27    #[error("Attempt to inverse project x: {0} y: {1} results in not finite result")]
28    InverseProjectionImpossible(f64, f64),
29}
30
31macro_rules! unpack_required_parameter {
32    ($self:ident, $param: ident) => {
33        $self
34            .$param
35            .ok_or(ProjectionError::ParamRequired(stringify!($param)))?
36    };
37}
38pub(crate) use unpack_required_parameter;
39
40macro_rules! ensure_finite {
41    ($param:ident) => {
42        if !$param.is_finite() {
43            return Err(ProjectionError::ParamNotFinite(stringify!($param)));
44        }
45    };
46
47    ($first:ident, $($rest:ident),+$(,)?) => {
48        ensure_finite!($first);
49        ensure_finite!($($rest),+);
50    };
51}
52pub(crate) use ensure_finite;
53
54macro_rules! ensure_within_range {
55    ($param:ident, $range:expr) => {
56        if !($range).contains(&$param) {
57            return Err(ProjectionError::ParamOutOfRange(
58                stringify!($param),
59                $range.start,
60                $range.end,
61            ));
62        }
63    };
64}
65pub(crate) use ensure_within_range;