1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::error::Error;
use std::fmt;
use std::io;
use std::path::Path;

use fitrs::{Fits, Hdu};

use super::IOValue;

impl IOValue {
    pub fn save<P: AsRef<Path>>(&self, path: P) -> Result<Fits, ExportError> {
        Fits::create(
            path,
            match self {
                IOValue::Image1d(arr) => Hdu::new(
                    arr.shape(),
                    arr.as_slice()
                        .expect("Could not get slice out of array")
                        .to_owned(),
                ),
                IOValue::Image2d(arr) => Hdu::new(
                    arr.shape(),
                    arr.as_slice()
                        .expect("Could not get slice out of array")
                        .to_owned(),
                ),
                _ => {
                    return Err(ExportError::NotImplemented(
                        "Can only save Image1d and Image2d",
                    ))
                }
            },
        ).map_err(ExportError::IOError)
    }
}

#[derive(Debug)]
pub enum ExportError {
    IOError(io::Error),
    NotImplemented(&'static str),
}

impl fmt::Display for ExportError {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ExportError::IOError(e) => write!(fmt, "{}", e),
            ExportError::NotImplemented(e) => write!(fmt, "Not implemented: {}", e),
        }
    }
}

impl Error for ExportError {
    /// description is deprecated. See https://github.com/rust-lang/rust/issues/44842
    /// Implement for compilation to succeed on older compilers.
    fn description(&self) -> &str {
        "ExportError"
    }
}