Skip to main content

fits_io/header/
extension_type.rs

1use std::error::Error;
2
3/// Which kind of extension an XTENSION card names.
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum ExtensionType {
6    /// `IMAGE`: an array, like the primary HDU's.
7    Image,
8    /// `BINTABLE`: a table of binary fields.
9    BinTable,
10    /// `TABLE`: a table of fixed-width text.
11    AsciiTable,
12}
13
14impl From<ExtensionType> for String {
15    fn from(value: ExtensionType) -> Self {
16        match value {
17            ExtensionType::Image => "IMAGE".to_string(),
18            ExtensionType::BinTable => "BINTABLE".to_string(),
19            ExtensionType::AsciiTable => "TABLE".to_string(),
20        }
21    }
22}
23
24impl TryFrom<String> for ExtensionType {
25    type Error = Box<dyn Error + Send + Sync>;
26
27    fn try_from(value: String) -> Result<Self, Self::Error> {
28        match value.to_uppercase().as_str() {
29            "IMAGE" => Ok(ExtensionType::Image),
30            "TABLE" => Ok(ExtensionType::AsciiTable),
31            "BINTABLE" => Ok(ExtensionType::BinTable),
32            _ => Err(format!("Unknown extension type: {}", value).into()),
33        }
34    }
35}