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
pub mod asciitable;
pub mod bintable;
pub mod image;

use std::io::Read;

use async_trait::async_trait;
use futures::AsyncRead;

use crate::card::Value;
use crate::error::Error;
use crate::hdu::primary::check_card_keyword;

use std::collections::HashMap;

#[derive(Debug)]
pub enum XtensionType {
    Image,
    BinTable,
    AsciiTable,
}

pub fn parse_xtension_card(card: &[u8; 80]) -> Result<XtensionType, Error> {
    let xtension = check_card_keyword(card, b"XTENSION")?.check_for_string()?;
    match xtension.as_bytes() {
        b"IMAGE   " | b"IUEIMAGE" => Ok(XtensionType::Image),
        b"TABLE   " => Ok(XtensionType::AsciiTable),
        b"BINTABLE" => Ok(XtensionType::BinTable),
        _ => Err(Error::NotSupportedXtensionType(xtension)),
    }
}

#[async_trait(?Send)]
pub trait Xtension {
    fn get_num_bytes_data_block(&self) -> u64;

    fn update_with_parsed_header(&mut self, cards: &HashMap<[u8; 8], Value>) -> Result<(), Error>;

    // Parse the Xtension keywords
    // During the parsing, some checks will be made
    fn parse<R: Read>(
        reader: &mut R,
        num_bytes_read: &mut u64,
        card_80_bytes_buf: &mut [u8; 80],
    ) -> Result<Self, Error>
    where
        Self: Sized;

    // Async equivalent method
    async fn parse_async<R>(
        reader: &mut R,
        num_bytes_read: &mut u64,
        card_80_bytes_buf: &mut [u8; 80],
    ) -> Result<Self, Error>
    where
        Self: Sized,
        R: AsyncRead + std::marker::Unpin;
}