Skip to content

Commit

Permalink
Add From<String> for StorePrefixError and deprecate `StorePrefixE…
Browse files Browse the repository at this point in the history
…rror::new`
  • Loading branch information
LDeakin committed Dec 21, 2023
1 parent 7f71e53 commit 55c57f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>` for `StorePrefixError` and deprecate `StorePrefixError::new`

### Fixed
- Fix `cargo test` with `async` crate feature disabled
Expand Down
27 changes: 22 additions & 5 deletions src/storage/store_prefix.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use derive_more::Display;
use derive_more::{Display, From};
use std::path::Path;
use thiserror::Error;

Expand All @@ -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::<String> method instead")]
/// Create a new invalid store prefix error.
#[must_use]
pub const fn new(prefix: String) -> Self {
Self(prefix)
Expand All @@ -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<String>) -> Result<Self, StorePrefixError> {
let prefix = prefix.into();
Expand All @@ -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<String>) -> Self {
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 55c57f2

Please sign in to comment.