diff --git a/Cargo.toml b/Cargo.toml index 68639a2..e9682b9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "fitsrs" -version = "0.2.9" +version = "0.2.10" authors = ["Matthieu Baumann "] edition = "2018" description = "Implementation of the FITS image parser" diff --git a/src/error.rs b/src/error.rs index 01d93b9..621e334 100644 --- a/src/error.rs +++ b/src/error.rs @@ -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) } @@ -31,4 +34,4 @@ quick_error! { display("Fail to parse a keyword as a utf8 string") } } -} \ No newline at end of file +} diff --git a/src/hdu/data/asciitable.rs b/src/hdu/data/asciitable.rs index 97893d2..69b4e39 100644 --- a/src/hdu/data/asciitable.rs +++ b/src/hdu/data/asciitable.rs @@ -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::(); let data = &data[..num_bytes_read]; diff --git a/src/hdu/data/bintable.rs b/src/hdu/data/bintable.rs index 31ad8a0..0cd7dd4 100644 --- a/src/hdu/data/bintable.rs +++ b/src/hdu/data/bintable.rs @@ -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::(); let data = &data[..num_bytes_read]; diff --git a/src/hdu/data/image.rs b/src/hdu/data/image.rs index b8aabcd..7043bf0 100644 --- a/src/hdu/data/image.rs +++ b/src/hdu/data/image.rs @@ -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 => { diff --git a/src/hdu/header/mod.rs b/src/hdu/header/mod.rs index dfc8057..b4cf9df 100644 --- a/src/hdu/header/mod.rs +++ b/src/hdu/header/mod.rs @@ -53,7 +53,7 @@ fn parse_generic_card(card: &[u8; 80]) -> Result, 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 '/' @@ -70,9 +70,9 @@ fn parse_generic_card(card: &[u8; 80]) -> Result, 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::() { @@ -86,7 +86,7 @@ fn parse_generic_card(card: &[u8; 80]) -> Result, Error> { } else { // Last case check for a string let inside_str = str.split('\'').collect::>(); - + if inside_str.len() >= 2 { // This is a true string because it is nested inside simple quotes Value::String(inside_str[1].to_string()) @@ -119,10 +119,14 @@ pub fn check_card_keyword(card: &[u8; 80], keyword: &[u8; 8]) -> Result Result { 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 { @@ -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, @@ -259,8 +270,12 @@ where where T: CardValue, { - self.get(key) - .map(|value| ::parse(value.clone())) + self.get(key).map(|value| { + ::parse(value.clone()).map_err(|e| { + let card = String::from_utf8_lossy(key); + Error::FailTypeCardParsing(card.to_string(), std::any::type_name::().to_string()) + }) + }) } }