diff --git a/CHANGELOG.md b/CHANGELOG.md index 533f5616..24848520 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -26,6 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Add `ArraySubset::{overlap,overlap_unchecked}` and `ArraySubset::{relative_to,relative_to_unchecked}` - These replace `ArraySubset::{in_subset,in_subset_unchecked}`, which are now deprecated + - Add `From` for `StorePrefixError` and deprecate `StorePrefixError::new` ### Fixed - Fix `cargo test` with `async` crate feature disabled diff --git a/src/storage/store_prefix.rs b/src/storage/store_prefix.rs index 7e17a54c..7e34f064 100644 --- a/src/storage/store_prefix.rs +++ b/src/storage/store_prefix.rs @@ -1,4 +1,4 @@ -use derive_more::Display; +use derive_more::{Display, From}; use std::path::Path; use thiserror::Error; @@ -11,12 +11,13 @@ use crate::node::NodePath; pub struct StorePrefix(String); /// An invalid store prefix. -#[derive(Debug, Error)] +#[derive(Debug, Error, From)] #[error("invalid store prefix {0}")] pub struct StorePrefixError(String); impl StorePrefixError { - /// Create a new invalid store prefix. + #[deprecated(since = "0.7.3", note = "please use the From:: method instead")] + /// Create a new invalid store prefix error. #[must_use] pub const fn new(prefix: String) -> Self { Self(prefix) @@ -30,7 +31,6 @@ impl StorePrefix { /// Create a new Zarr Prefix from `prefix`. /// /// # Errors - /// /// Returns [`StorePrefixError`] if `prefix` is not valid according to [`StorePrefix::validate`()]. pub fn new(prefix: impl Into) -> Result { let prefix = prefix.into(); @@ -44,7 +44,6 @@ impl StorePrefix { /// Create a new Zarr Prefix from `prefix`. /// /// # Safety - /// /// `prefix` is not validated, so this can result in an invalid store prefix. #[must_use] pub unsafe fn new_unchecked(prefix: impl Into) -> Self { @@ -107,3 +106,21 @@ impl TryFrom<&NodePath> for StorePrefix { } } } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn valid() { + assert!(StorePrefix::new("").is_ok()); + assert!(StorePrefix::new("a/").is_ok()); + assert!(StorePrefix::new("a/b/").is_ok()); + } + + #[test] + fn invalid() { + assert!(StorePrefix::new("a").is_err()); + assert!(StorePrefix::new("a/b").is_err()); + } +}