Skip to content

Use anyhow for errors #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ rayon = "1"
dyn-clone = "1"
indexmap = "1"
rustronomy-core = "0.1"
anyhow = "1"

[dev-dependencies]
dirs = "4"
Expand Down
6 changes: 4 additions & 2 deletions src/api/fits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@

use std::{error::Error, fmt::Display, path::Path};

use anyhow::{Result};

use rustronomy_core::universal_containers::*;

use super::hdu::Hdu;
Expand All @@ -60,15 +62,15 @@ pub struct Fits {
}

impl Fits {
pub fn read(path: &Path) -> Result<Self, Box<dyn Error>> {
pub fn read(path: &Path) -> Result<Self> {
//(1) First we try to open the file
let mut reader = crate::intern::FitsReader::new(path)?;

//(2) Then we read the primary HDU
let (global_tags, hdu0) = crate::intern::read_primary_hdu(&mut reader)?;
todo!()
}
pub fn write(self, path: &Path) -> Result<(), Box<dyn Error>> {
pub fn write(self, path: &Path) -> Result<()> {
todo!()
}
pub fn empty() -> Self {
Expand Down
6 changes: 4 additions & 2 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::{
fmt::{Display, Formatter},
};

use anyhow::Result;

use crate::{
io_err::{self, InvalidFitsFileErr as IFFErr},
raw::{raw_io::RawFitsWriter, BlockSized},
Expand Down Expand Up @@ -72,10 +74,10 @@ impl Display for Extension {
}

impl Extension {
pub(crate) fn write_to_buffer(self, writer: &mut RawFitsWriter) -> Result<(), Box<dyn Error>> {
pub(crate) fn write_to_buffer(self, writer: &mut RawFitsWriter) -> Result<()> {
use Extension::*;
match self {
Corrupted => return Err(Box::new(IFFErr::new(io_err::CORRUPTED))),
Corrupted => return Err(IFFErr::new(io_err::CORRUPTED).into()),
Image(img) => ImgParser::encode_img(img, writer),
AsciiTable(tbl) => AsciiTblParser::encode_tbl(tbl, writer),
}
Expand Down
12 changes: 7 additions & 5 deletions src/extensions/image/image_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use std::{
mem::size_of,
};

use anyhow::Result;

//Rustronomy Imports
use rustronomy_core::data_type_traits::io_utils::{Decode, Encode};

Expand Down Expand Up @@ -60,7 +62,7 @@ impl ImgParser {
reader: &mut RawFitsReader,
shape: &Vec<usize>,
bitpix: Bitpix,
) -> Result<Extension, Box<dyn Error>> {
) -> Result<Extension> {
use Bitpix::*;
use TypedImage::*;

Expand All @@ -77,7 +79,7 @@ impl ImgParser {
fn decode_helper<T>(
reader: &mut RawFitsReader,
shape: &Vec<usize>,
) -> Result<Image<T>, Box<dyn Error>>
) -> Result<Image<T>>
where
T: Debug + Num + Sized + Decode + Encode + Display + Clone + Send,
{
Expand Down Expand Up @@ -156,7 +158,7 @@ impl ImgParser {
pub(crate) fn encode_img(
typed_img: TypedImage,
writer: &mut RawFitsWriter,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
//This function only matches the typed image and calls the appropriate
//helper function
use TypedImage::*;
Expand All @@ -174,7 +176,7 @@ impl ImgParser {
Ok(())
}

fn encode_helper<T>(img: Image<T>, writer: &mut RawFitsWriter) -> Result<(), Box<dyn Error>>
fn encode_helper<T>(img: Image<T>, writer: &mut RawFitsWriter) -> Result<()>
where
T: Debug + Num + Sized + Decode + Encode + Display + Clone,
{
Expand Down Expand Up @@ -207,7 +209,7 @@ impl ImgParser {
match img.get_data().as_slice_memory_order() {
None => {
//Data is NOT continuous, return error!
return Err(Box::new(IMLErr::new()));
return Err(IMLErr::new().into());
}
_ => {} //Data IS continuous, continue!
}
Expand Down
49 changes: 25 additions & 24 deletions src/extensions/image/typed_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use std::{
fmt::{Display, Write},
};

use anyhow::Result;
use ndarray::{Array, IxDyn};

use crate::{
Expand Down Expand Up @@ -128,87 +129,87 @@ impl TypedImage {
}
}

pub fn as_u8_array(&self) -> Result<&Array<u8, IxDyn>, Box<dyn Error>> {
pub fn as_u8_array(&self) -> Result<&Array<u8, IxDyn>> {
match &self {
Self::ByteImg(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::byte()))),
&var => Err(WITErr::new(var, Bitpix::byte()).into()),
}
}

pub fn as_i16_array(&self) -> Result<&Array<i16, IxDyn>, Box<dyn Error>> {
pub fn as_i16_array(&self) -> Result<&Array<i16, IxDyn>> {
match &self {
Self::I16Img(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::short()))),
&var => Err(WITErr::new(var, Bitpix::short()).into()),
}
}

pub fn as_i32_array(&self) -> Result<&Array<i32, IxDyn>, Box<dyn Error>> {
pub fn as_i32_array(&self) -> Result<&Array<i32, IxDyn>> {
match &self {
Self::I32Img(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::int()))),
&var => Err(WITErr::new(var, Bitpix::int()).into()),
}
}

pub fn as_i64_array(&self) -> Result<&Array<i64, IxDyn>, Box<dyn Error>> {
pub fn as_i64_array(&self) -> Result<&Array<i64, IxDyn>> {
match &self {
Self::I64Img(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::long()))),
&var => Err(WITErr::new(var, Bitpix::long()).into()),
}
}

pub fn as_f32_array(&self) -> Result<&Array<f32, IxDyn>, Box<dyn Error>> {
pub fn as_f32_array(&self) -> Result<&Array<f32, IxDyn>> {
match &self {
Self::SpfImg(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::spf()))),
&var => Err(WITErr::new(var, Bitpix::spf()).into()),
}
}

pub fn as_f64_array(&self) -> Result<&Array<f64, IxDyn>, Box<dyn Error>> {
pub fn as_f64_array(&self) -> Result<&Array<f64, IxDyn>> {
match &self {
Self::DpfImg(img) => Ok(img.get_data()),
&var => Err(Box::new(WITErr::new(var, Bitpix::dpf()))),
&var => Err(WITErr::new(var, Bitpix::dpf()).into()),
}
}

pub fn as_owned_u8_array(self) -> Result<Array<u8, IxDyn>, Box<dyn Error>> {
pub fn as_owned_u8_array(self) -> Result<Array<u8, IxDyn>> {
match self {
Self::ByteImg(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::byte()))),
var => Err(WITErr::new(&var, Bitpix::byte()).into()),
}
}

pub fn as_owned_i16_array(self) -> Result<Array<i16, IxDyn>, Box<dyn Error>> {
pub fn as_owned_i16_array(self) -> Result<Array<i16, IxDyn>> {
match self {
Self::I16Img(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::short()))),
var => Err(WITErr::new(&var, Bitpix::short()).into()),
}
}

pub fn as_owned_i32_array(self) -> Result<Array<i32, IxDyn>, Box<dyn Error>> {
pub fn as_owned_i32_array(self) -> Result<Array<i32, IxDyn>> {
match self {
Self::I32Img(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::int()))),
var => Err(WITErr::new(&var, Bitpix::int()).into()),
}
}

pub fn as_owned_i64_array(self) -> Result<Array<i64, IxDyn>, Box<dyn Error>> {
pub fn as_owned_i64_array(self) -> Result<Array<i64, IxDyn>> {
match self {
Self::I64Img(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::long()))),
var => Err(WITErr::new(&var, Bitpix::long()).into()),
}
}

pub fn as_owned_f32_array(self) -> Result<Array<f32, IxDyn>, Box<dyn Error>> {
pub fn as_owned_f32_array(self) -> Result<Array<f32, IxDyn>> {
match self {
Self::SpfImg(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::spf()))),
var => Err(WITErr::new(&var, Bitpix::spf()).into()),
}
}

pub fn as_owned_f64_array(self) -> Result<Array<f64, IxDyn>, Box<dyn Error>> {
pub fn as_owned_f64_array(self) -> Result<Array<f64, IxDyn>> {
match self {
Self::DpfImg(img) => Ok(img.get_data_owned()),
var => Err(Box::new(WITErr::new(&var, Bitpix::dpf()))),
var => Err(WITErr::new(&var, Bitpix::dpf()).into()),
}
}
}
6 changes: 4 additions & 2 deletions src/extensions/table/ascii_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::{
fmt::{self, Display, Formatter},
};

use anyhow::Result;

use crate::{
extensions::ExtensionPrint,
raw::{table_entry_format::TableEntryFormat, BlockSized},
Expand Down Expand Up @@ -138,10 +140,10 @@ impl AsciiTable {
AsciiTable { cols: cols, block_size: Some(size) }
}

pub(crate) fn add_row(&mut self, row: Vec<TableEntry>) -> Result<(), Box<dyn Error>> {
pub(crate) fn add_row(&mut self, row: Vec<TableEntry>) -> Result<()> {
//Adds row to table
if row.len() != self.cols.len() {
return Err(Box::new(ShapeMisMatchErr::new(&row, &self)));
return Err(ShapeMisMatchErr::new(&row, &self).into());
}

//Add row to the table
Expand Down
6 changes: 4 additions & 2 deletions src/extensions/table/ascii_tbl_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ use std::{
str::{self, Utf8Error},
};

use anyhow::Result;

use crate::{
extensions::{table::column::AsciiCol, Extension},
raw::{
Expand All @@ -49,7 +51,7 @@ impl AsciiTblParser {
row_index_col_start: Vec<usize>, //row index where each column starts
field_format: Vec<String>, //data format (incl length) of each field
field_labels: Option<Vec<String>>, //field labels
) -> Result<Extension, Box<dyn Error>> {
) -> Result<Extension> {
/* (1)
Tables are usually pretty small compared to images. Hence it's
probably ok to read the whole table in one go. We should be careful
Expand Down Expand Up @@ -190,7 +192,7 @@ impl AsciiTblParser {
pub(crate) fn encode_tbl(
tbl: AsciiTable,
writer: &mut RawFitsWriter,
) -> Result<(), Box<dyn Error>> {
) -> Result<()> {
/* Note:
This parser assumes that certain necessary keywords to decode a HDU
containing a table have already been set while encoding the header
Expand Down
5 changes: 3 additions & 2 deletions src/fits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{
fmt::{Display, Formatter},
path::Path,
};
use anyhow::Result;

use crate::{
header_data_unit::HeaderDataUnit,
Expand All @@ -42,7 +43,7 @@ pub struct Fits {
}

impl Fits {
pub fn open(path: &Path) -> Result<Self, Box<dyn Error>> {
pub fn open<P:AsRef<Path>>(path: P) -> Result<Self> {
//(1) Construct a RawFitsReader
let mut reader = RawFitsReader::new(path)?;

Expand All @@ -57,7 +58,7 @@ impl Fits {
Ok(Fits { hdus: hdus })
}

pub fn write(self, path: &Path) -> Result<(), Box<dyn Error>> {
pub fn write(self, path: &Path) -> Result<()> {
//(1) Construct a RawFitsWriter
let mut writer = RawFitsWriter::new(path)?;

Expand Down
12 changes: 7 additions & 5 deletions src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ use std::{
str::FromStr,
};

use anyhow::Result;

use chrono::{Datelike, Utc};
use indexmap::IndexMap;

Expand All @@ -50,7 +52,7 @@ pub struct Header {
}

impl Header {
pub fn decode_header(raw: &mut RawFitsReader) -> Result<Self, Box<dyn Error>> {
pub fn decode_header(raw: &mut RawFitsReader) -> Result<Self> {
/* Setup:
We'll keep reading headerblocks (= FITS blocks) until we encounter
the END keyword. We'll also have to keep track of the block size of
Expand All @@ -75,7 +77,7 @@ impl Header {
Ok(Self::from_parts(hbs, block_len)?)
}

fn from_parts(hbs: Vec<HeaderBlock>, block_len: usize) -> Result<Self, Box<dyn Error>> {
fn from_parts(hbs: Vec<HeaderBlock>, block_len: usize) -> Result<Self> {
//Parse the Keywordrecords to plain Key-Data pairs
let mut parsed_map: IndexMap<Rc<String>, KeywordRecord> = IndexMap::new();

Expand Down Expand Up @@ -116,7 +118,7 @@ impl Header {
Ok(Header { records: parsed_map, block_len: block_len })
}

pub fn encode_header(self, writer: &mut RawFitsWriter) -> Result<(), Box<dyn Error>> {
pub fn encode_header(self, writer: &mut RawFitsWriter) -> Result<()> {
//Buffer to write whole header in one go.
//Also keeps track of number of bytes we wrote to the header!
let mut buf = Vec::new();
Expand Down Expand Up @@ -207,10 +209,10 @@ impl Header {
}

//Helper function for parsing keyword records
pub fn get_value_as<T>(&self, keyword: &str) -> Result<T, Box<dyn Error>>
pub fn get_value_as<T>(&self, keyword: &str) -> Result<T>
where
T: FromStr,
<T as FromStr>::Err: 'static + Error,
<T as FromStr>::Err: 'static + Error + Send + Sync,
{
match self.get_value(&keyword.to_string()) {
None => Err(MissingRecordError::new(keyword))?,
Expand Down
Loading