Skip to content

Commit 2975a58

Browse files
committed
Made file paths type more flexible
1 parent b577045 commit 2975a58

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ anyhow = { version = "1.0", features = ["backtrace"] }
4141
float-cmp = "0.10"
4242

4343
[features]
44-
default = ["message_ndarray"]
44+
default = ["message_ndarray", "experimental_index"]
4545
docs = ["eccodes-sys/docs"]
4646
experimental_index = []
4747
message_ndarray = ["dep:ndarray"]

src/codes_handle/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ impl CodesHandle<CodesFile<File>> {
183183
///
184184
///Returns [`CodesError::Internal`] with error code
185185
///when internal [`codes_handle`] cannot be created.
186-
pub fn new_from_file(file_path: &Path, product_kind: ProductKind) -> Result<Self, CodesError> {
186+
pub fn new_from_file<P: AsRef<Path>>(
187+
file_path: P,
188+
product_kind: ProductKind,
189+
) -> Result<Self, CodesError> {
187190
let file = OpenOptions::new().read(true).open(file_path)?;
188191
let file_pointer = open_with_fdopen(&file)?;
189192

src/codes_index.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,8 @@ impl CodesIndex {
172172
/// This function will return [`CodesError::Internal`] if the index file is not valid or
173173
/// the GRIB file is not present in the same relative path as during the index file creation.
174174
#[cfg_attr(docsrs, doc(cfg(feature = "experimental_index")))]
175-
pub fn read_from_file(index_file_path: &Path) -> Result<CodesIndex, CodesError> {
175+
pub fn read_from_file<P: AsRef<Path>>(index_file_path: P) -> Result<CodesIndex, CodesError> {
176+
let index_file_path: &Path = index_file_path.as_ref();
176177
let file_path = index_file_path.to_str().ok_or_else(|| {
177178
std::io::Error::new(std::io::ErrorKind::InvalidData, "Path is not valid utf8")
178179
})?;
@@ -213,7 +214,11 @@ impl CodesIndex {
213214
/// Returns [`CodesError::Internal`] if the file cannot be added to the index.
214215
/// The error might be also caused by incorrectly constructed index.
215216
#[cfg_attr(docsrs, doc(cfg(feature = "experimental_index")))]
216-
pub fn add_grib_file(self, index_file_path: &Path) -> Result<CodesIndex, CodesError> {
217+
pub fn add_grib_file<P: AsRef<Path>>(
218+
self,
219+
index_file_path: P,
220+
) -> Result<CodesIndex, CodesError> {
221+
let index_file_path: &Path = index_file_path.as_ref();
217222
let file_path = index_file_path.to_str().ok_or_else(|| {
218223
std::io::Error::new(std::io::ErrorKind::InvalidData, "Path is not valid utf8")
219224
})?;

src/keyed_message/write.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ impl KeyedMessage {
110110
///
111111
/// Returns [`CodesInternal`](crate::errors::CodesInternal)
112112
/// when internal ecCodes function returns non-zero code.
113-
pub fn write_to_file(&self, file_path: &Path, append: bool) -> Result<(), CodesError> {
113+
pub fn write_to_file<P: AsRef<Path>>(
114+
&self,
115+
file_path: P,
116+
append: bool,
117+
) -> Result<(), CodesError> {
114118
let msg = unsafe { codes_get_message(self.message_handle)? };
115119
let buf = unsafe { slice::from_raw_parts(msg.0.cast::<u8>(), msg.1) };
116120
let mut file = OpenOptions::new()

0 commit comments

Comments
 (0)