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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::hdu::extension::{AsyncXtensionHDU, XtensionHDU};
use crate::hdu::primary::{AsyncPrimaryHDU, PrimaryHDU};

use crate::hdu::header::extension::asciitable::AsciiTable;
use crate::hdu::header::extension::bintable::BinTable;
use crate::hdu::header::extension::image::Image;

use crate::hdu::data::{DataAsyncBufRead, DataBufRead};

#[derive(Debug)]
pub struct Fits<'a, R>
where
    R: DataBufRead<'a, Image> + DataBufRead<'a, BinTable> + DataBufRead<'a, AsciiTable>,
{
    pub hdu: PrimaryHDU<'a, R>,
}

use crate::error::Error;
impl<'a, R> Fits<'a, R>
where
    R: DataBufRead<'a, Image> + DataBufRead<'a, BinTable> + DataBufRead<'a, AsciiTable>,
{
    /// Parse a FITS file
    /// # Params
    /// * `reader` - a reader created i.e. from the opening of a file
    pub fn from_reader(reader: &'a mut R) -> Result<Self, Error> {
        let hdu = PrimaryHDU::new(reader)?;

        Ok(Self { hdu })
    }

    pub fn get_primary_hdu(self) -> PrimaryHDU<'a, R> {
        self.hdu
    }

    pub fn get_xtension_hdu(self, mut idx: usize) -> Result<XtensionHDU<'a, R>, Error> {
        let mut hdu_ext = self.hdu.next()?;
        if idx == 0 {
            if let Some(hdu) = hdu_ext {
                Ok(hdu)
            } else {
                Err(Error::StaticError("No more ext HDU found"))
            }
        } else {
            while let Some(hdu) = hdu_ext {
                hdu_ext = hdu.next()?;
                idx -= 1;

                if idx == 0 {
                    break;
                }
            }

            if let Some(hdu) = hdu_ext {
                Ok(hdu)
            } else {
                Err(Error::StaticError("No more ext HDU found"))
            }
        }
    }
}

#[derive(Debug)]
pub struct AsyncFits<'a, R>
where
    R: DataAsyncBufRead<'a, Image>
        + DataAsyncBufRead<'a, BinTable>
        + DataAsyncBufRead<'a, AsciiTable>,
{
    pub hdu: AsyncPrimaryHDU<'a, R>,
}

impl<'a, R> AsyncFits<'a, R>
where
    R: DataAsyncBufRead<'a, Image>
        + DataAsyncBufRead<'a, BinTable>
        + DataAsyncBufRead<'a, AsciiTable>,
{
    /// Parse a FITS file
    /// # Params
    /// * `reader` - a reader created i.e. from the opening of a file
    pub async fn from_reader(reader: &'a mut R) -> Result<AsyncFits<'a, R>, Error> {
        let hdu = AsyncPrimaryHDU::new(reader).await?;

        Ok(Self { hdu })
    }

    pub fn get_primary_hdu(self) -> AsyncPrimaryHDU<'a, R> {
        self.hdu
    }

    pub async fn get_xtension_hdu(self, mut idx: usize) -> Result<AsyncXtensionHDU<'a, R>, Error> {
        let mut hdu_ext = self.hdu.next().await?;
        if idx == 0 {
            if let Some(hdu) = hdu_ext {
                Ok(hdu)
            } else {
                Err(Error::StaticError("No more ext HDU found"))
            }
        } else {
            while let Some(hdu) = hdu_ext {
                hdu_ext = hdu.next().await?;
                idx -= 1;

                if idx == 0 {
                    break;
                }
            }

            if let Some(hdu) = hdu_ext {
                Ok(hdu)
            } else {
                Err(Error::StaticError("No more ext HDU found"))
            }
        }
    }
}