Skip to content

Commit

Permalink
remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatthieu3 committed Jul 18, 2024
1 parent 6928981 commit 86dba4f
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 115 deletions.
87 changes: 4 additions & 83 deletions src/card.rs
Original file line number Diff line number Diff line change
@@ -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,
};

Expand Down Expand Up @@ -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)
}
14 changes: 5 additions & 9 deletions src/hdu/header/extension/bintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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?;
Expand Down
24 changes: 1 addition & 23 deletions src/hdu/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<R: Read>(
reader: &mut R,
buf: &mut [u8; 80],
Expand Down Expand Up @@ -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<X> {
/* Non mandatory keywords */
Expand Down Expand Up @@ -271,7 +249,7 @@ where
T: CardValue,
{
self.get(key).map(|value| {
<T as CardValue>::parse(value.clone()).map_err(|e| {
<T as CardValue>::parse(value.clone()).map_err(|_| {
let card = String::from_utf8_lossy(key);
Error::FailTypeCardParsing(card.to_string(), std::any::type_name::<T>().to_string())
})
Expand Down

0 comments on commit 86dba4f

Please sign in to comment.