diff --git a/src/card.rs b/src/card.rs index 83dca58..a6d1945 100644 --- a/src/card.rs +++ b/src/card.rs @@ -1,10 +1,7 @@ use nom::{ - branch::alt, - bytes::complete::{take_till, take_while}, - character::complete::{char, space0, i64, hex_digit1}, - combinator::{map, value}, - number::complete::{float}, - sequence::{delimited, preceded}, + character::complete::{i64, space0}, + combinator::map, + sequence::preceded, IResult, }; @@ -89,82 +86,6 @@ impl Value { } } -pub(crate) fn white_space0(s: &[u8]) -> IResult<&[u8], &[u8]> { - take_while(|s| s == b' ')(s) -} - -pub(crate) fn parse_undefined(buf: &[u8]) -> IResult<&[u8], Value> { - value(Value::Undefined, white_space0)(buf) -} - -pub(crate) fn parse_character_string(buf: &[u8]) -> IResult<&[u8], Value> { - map( - preceded( - space0, - delimited(char('\''), take_till(|c| c == b'\''), char('\'')) - ), - |str: &[u8]| { - // Copy the bytes to a new string - // This is not a big deal because it only concerns the parsing - // of the FITS header - let str = String::from_utf8_lossy(str).into_owned(); - Value::String(str) - }, - )(buf) -} - -pub(crate) fn parse_logical(buf: &[u8]) -> IResult<&[u8], Value> { - preceded( - space0, - alt(( - value(Value::Logical(true), char('T')), - value(Value::Logical(false), char('F')), - )), - )(buf) -} - -pub(crate) fn parse_float(buf: &[u8]) -> IResult<&[u8], Value> { - preceded( - space0, - map(float, |val| Value::Float(val as f64)) - )(buf) -} - pub(crate) fn parse_integer(buf: &[u8]) -> IResult<&[u8], Value> { - preceded( - space0, - map(i64, |val| Value::Integer(val)), - )(buf) -} - -#[cfg(test)] -mod tests { - use super::{parse_character_string, parse_float, Value}; - use crate::card::parse_integer; - - #[test] - fn test_float() { - assert_eq!( - parse_float(b" -32768.0"), - Ok((b"" as &[u8], Value::Float(-32768.0))) - ); - assert_eq!( - parse_float(b" -32767"), - Ok((b"" as &[u8], Value::Float(-32767.0))) - ); - assert_eq!( - parse_float(b" -32767A"), - Ok((b"A" as &[u8], Value::Float(-32767.0))) - ); - } - #[test] - fn test_string() { - assert_eq!( - parse_character_string(b" 'sdfs Zdfs MLKKLSFD sdf '"), - Ok(( - b"" as &[u8], - Value::String(String::from("sdfs Zdfs MLKKLSFD sdf ")) - )) - ); - } + preceded(space0, map(i64, |val| Value::Integer(val)))(buf) } diff --git a/src/hdu/header/extension/bintable.rs b/src/hdu/header/extension/bintable.rs index 8f50d12..314ed6d 100644 --- a/src/hdu/header/extension/bintable.rs +++ b/src/hdu/header/extension/bintable.rs @@ -7,11 +7,11 @@ use futures::AsyncRead; use nom::AsChar; use serde::Serialize; -use crate::card::parse_integer; use crate::error::Error; use crate::hdu::header::consume_next_card_async; use crate::hdu::header::parse_bitpix_card; use crate::hdu::header::parse_gcount_card; +use crate::hdu::header::parse_integer; use crate::hdu::header::parse_naxis_card; use crate::hdu::header::parse_pcount_card; use crate::hdu::header::BitpixValue; @@ -133,12 +133,10 @@ impl Xtension for BinTable { // NAXIS1 consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; - let naxis1 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; + let naxis1 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; // NAXIS2 consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; - let naxis2 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; + let naxis2 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; // PCOUNT consume_next_card(reader, card_80_bytes_buf, num_bytes_read)?; @@ -197,13 +195,11 @@ impl Xtension for BinTable { // NAXIS1 consume_next_card_async(reader, card_80_bytes_buf, num_bytes_read).await?; - let naxis1 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; + let naxis1 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[0])?.check_for_float()? as u64; // NAXIS2 consume_next_card_async(reader, card_80_bytes_buf, num_bytes_read).await?; - let naxis2 = - check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; + let naxis2 = check_card_keyword(card_80_bytes_buf, NAXIS_KW[1])?.check_for_float()? as u64; // PCOUNT consume_next_card_async(reader, card_80_bytes_buf, num_bytes_read).await?; diff --git a/src/hdu/header/mod.rs b/src/hdu/header/mod.rs index b4cf9df..1fa33c4 100644 --- a/src/hdu/header/mod.rs +++ b/src/hdu/header/mod.rs @@ -3,8 +3,6 @@ //! A header basically consists of a list a 80 long characters CARDS //! Each CARD is a dictionnary tuple-like of the (key, value) form. use futures::{AsyncBufRead, AsyncRead, AsyncReadExt}; -use nom::character::complete::space0; -use nom::character::complete::space1; use serde::Serialize; pub mod extension; @@ -18,8 +16,6 @@ use crate::error::Error; use crate::hdu::Xtension; -use nom::{branch::alt, bytes::complete::tag, sequence::preceded, IResult}; - pub fn consume_next_card( reader: &mut R, buf: &mut [u8; 80], @@ -168,24 +164,6 @@ pub enum BitpixValue { F64 = -64, } -pub(crate) fn parse_card_value(buf: &[u8]) -> IResult<&[u8], Value> { - preceded( - white_space0, - alt(( - preceded( - tag(b"="), - alt(( - parse_character_string, - parse_logical, - parse_float, - parse_integer, - )), - ), - parse_undefined, - )), - )(buf) -} - #[derive(Debug, PartialEq, Serialize)] pub struct Header { /* Non mandatory keywords */ @@ -271,7 +249,7 @@ where T: CardValue, { self.get(key).map(|value| { - ::parse(value.clone()).map_err(|e| { + ::parse(value.clone()).map_err(|_| { let card = String::from_utf8_lossy(key); Error::FailTypeCardParsing(card.to_string(), std::any::type_name::().to_string()) })