Skip to main content

conspire/io/netcdf/from/
mod.rs

1use crate::io::netcdf::NetCDF;
2use std::{
3    error::Error,
4    ffi::NulError,
5    fmt::{self, Debug, Display},
6    path::Path,
7};
8
9pub enum NetCdfError {
10    InvalidPath,
11    Nul(NulError),
12}
13
14impl Debug for NetCdfError {
15    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
16        Display::fmt(self, f)
17    }
18}
19
20impl Display for NetCdfError {
21    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
22        match self {
23            Self::InvalidPath => write!(f, "path is not valid UTF-8"),
24            Self::Nul(error) => write!(f, "{error}"),
25        }
26    }
27}
28
29impl Error for NetCdfError {}
30
31impl From<NulError> for NetCdfError {
32    fn from(error: NulError) -> Self {
33        Self::Nul(error)
34    }
35}
36
37impl TryFrom<&Path> for NetCDF {
38    type Error = NetCdfError;
39    fn try_from(path: &Path) -> Result<Self, Self::Error> {
40        let path = path.to_str().ok_or(NetCdfError::InvalidPath)?;
41        let mut netcdf = Self::create(path)?;
42        netcdf.global();
43        Ok(netcdf)
44    }
45}