Skip to content

Commit

Permalink
return a proper error when a parsing of a card fails
Browse files Browse the repository at this point in the history
  • Loading branch information
bmatthieu3 committed Jul 18, 2024
1 parent 66dff0c commit 6928981
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fitsrs"
version = "0.2.9"
version = "0.2.10"
authors = ["Matthieu Baumann <[email protected]>"]
edition = "2018"
description = "Implementation of the FITS image parser"
Expand Down
5 changes: 4 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ quick_error! {
ValueBadParsing {
display("A value could not be parsed correctly")
}
FailTypeCardParsing(card: String, t: String) {
display("{} card is not of type {}", card, t)
}
NotSupportedXtensionType(extension: String) {
display("`{}` extension is not supported. Only BINTABLE, TABLE and IMAGE are.", extension)
}
Expand All @@ -31,4 +34,4 @@ quick_error! {
display("Fail to parse a keyword as a utf8 string")
}
}
}
}
6 changes: 3 additions & 3 deletions src/hdu/data/asciitable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ where

let bytes = &bytes[start_byte_pos..end_byte_pos];

let w = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
let c = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
unsafe {
let word: &UnsafeCell<[u8]> = &*w;
let x_mut_ref = &mut *word.get();
let cell: &UnsafeCell<[u8]> = &*c;
let x_mut_ref = &mut *cell.get();

let (_, data, _) = x_mut_ref.align_to_mut::<u8>();
let data = &data[..num_bytes_read];
Expand Down
6 changes: 3 additions & 3 deletions src/hdu/data/bintable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ where

let bytes = &bytes[start_byte_pos..end_byte_pos];

let w = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
let c = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
unsafe {
let word: &UnsafeCell<[u8]> = &*w;
let x_mut_ref = &mut *word.get();
let cell: &UnsafeCell<[u8]> = &*c;
let x_mut_ref = &mut *cell.get();

let (_, data, _) = x_mut_ref.align_to_mut::<u8>();
let data = &data[..num_bytes_read];
Expand Down
7 changes: 3 additions & 4 deletions src/hdu/data/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,10 @@ where

let bytes = &bytes[start_byte_pos..end_byte_pos];

let w = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
//let x_ptr = UnsafeCell::new(bytes as *const [u8] as *mut [u8];
let c = bytes as *const [u8] as *mut UnsafeCell<[u8]>;
unsafe {
let word: &UnsafeCell<[u8]> = &*w;
let x_mut_ref = &mut *word.get();
let cell: &UnsafeCell<[u8]> = &*c;
let x_mut_ref = &mut *cell.get();

match bitpix {
BitpixValue::U8 => {
Expand Down
35 changes: 25 additions & 10 deletions src/hdu/header/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ fn parse_generic_card(card: &[u8; 80]) -> Result<Option<Card>, Error> {
b"HISTORY " => {
let v = Value::String(String::from_utf8_lossy(&card[8..]).to_string());
Some(v)
},
}
_ => {
let str = String::from_utf8_lossy(&card[8..]);
// Take until the comment beginning with '/'
Expand All @@ -70,9 +70,9 @@ fn parse_generic_card(card: &[u8; 80]) -> Result<Option<Card>, Error> {
&str
};

// remove the ' ' before and after
// remove the ' ' before and after
let str = str.trim();

if str.is_empty() {
Value::Undefined
} else if let Ok(val) = str.parse::<f64>() {
Expand All @@ -86,7 +86,7 @@ fn parse_generic_card(card: &[u8; 80]) -> Result<Option<Card>, Error> {
} else {
// Last case check for a string
let inside_str = str.split('\'').collect::<Vec<_>>();

if inside_str.len() >= 2 {
// This is a true string because it is nested inside simple quotes
Value::String(inside_str[1].to_string())
Expand Down Expand Up @@ -119,10 +119,14 @@ pub fn check_card_keyword(card: &[u8; 80], keyword: &[u8; 8]) -> Result<card::Va
if &kw == keyword {
Ok(v)
} else {
Err(Error::FailFindingKeyword(std::str::from_utf8(keyword)?.to_owned()))
Err(Error::FailFindingKeyword(
std::str::from_utf8(keyword)?.to_owned(),
))
}
} else {
Err(Error::FailFindingKeyword(std::str::from_utf8(keyword)?.to_owned()))
Err(Error::FailFindingKeyword(
std::str::from_utf8(keyword)?.to_owned(),
))
}
}

Expand All @@ -145,7 +149,14 @@ fn parse_naxis_card(card: &[u8; 80]) -> Result<usize, Error> {
Ok(naxis as usize)
}

const NAXIS_KW: [&[u8; 8]; 6] = [b"NAXIS1 ", b"NAXIS2 ", b"NAXIS3 ", b"NAXIS4 ", b"NAXIS5 ", b"NAXIS6 "];
const NAXIS_KW: [&[u8; 8]; 6] = [
b"NAXIS1 ",
b"NAXIS2 ",
b"NAXIS3 ",
b"NAXIS4 ",
b"NAXIS5 ",
b"NAXIS6 ",
];

#[derive(Debug, PartialEq, Serialize, Clone, Copy)]
pub enum BitpixValue {
Expand All @@ -167,7 +178,7 @@ pub(crate) fn parse_card_value(buf: &[u8]) -> IResult<&[u8], Value> {
parse_character_string,
parse_logical,
parse_float,
parse_integer
parse_integer,
)),
),
parse_undefined,
Expand Down Expand Up @@ -259,8 +270,12 @@ where
where
T: CardValue,
{
self.get(key)
.map(|value| <T as CardValue>::parse(value.clone()))
self.get(key).map(|value| {
<T as CardValue>::parse(value.clone()).map_err(|e| {
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 6928981

Please sign in to comment.