Skip to content

Commit

Permalink
StorePrefix/StoreKey/NodeName new Into<String>
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Oct 21, 2023
1 parent 897fa8a commit 9a97141
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `sharded_array_write_read` example now prints storage operations and demonstrates retrieving inner chunks directly from a partial decoder
- the zarrs version and a link to the source code is now written to the `_zarrs` attribute in array metadata, this can be disabled with `set_include_zarrs_metadata(false)`
- **Breaking**: Rename `FillValueMetadata::Uint` to `UInt` for consistency with the `DataType::UInt` variants.
- `StorePrefix`, `StoreKey`, `NodeName` `new` methods are now `: impl Into<String>`

### Fixed
- Bytes codec handling of complex and raw bits data types
Expand Down
13 changes: 7 additions & 6 deletions src/node/node_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ impl NodeName {
/// # Errors
///
/// Returns [`NodeNameError`] if `name` is not valid according to [`NodeName::validate`()].
pub fn new(name: &str) -> Result<Self, NodeNameError> {
if Self::validate(name) {
Ok(Self(name.to_string()))
pub fn new(name: impl Into<String>) -> Result<Self, NodeNameError> {
let name = name.into();
if Self::validate(&name) {
Ok(Self(name))
} else {
Err(NodeNameError(name.to_string()))
Err(NodeNameError(name))
}
}

Expand All @@ -35,8 +36,8 @@ impl NodeName {
///
/// `name` is not validated, so this can result in an invalid node name.
#[must_use]
pub unsafe fn new_unchecked(name: &str) -> Self {
Self(name.to_string())
pub unsafe fn new_unchecked(name: impl Into<String>) -> Self {
Self(name.into())
}

/// The root node.
Expand Down
6 changes: 3 additions & 3 deletions src/storage/store/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ impl FilesystemStore {
let path = pathdiff::diff_paths(path, &self.base_path)
.ok_or_else(|| StoreKeyError::from(path.to_str().unwrap_or_default().to_string()))?;
let path_str = path.to_string_lossy();
StoreKey::new(&path_str)
StoreKey::new(path_str)
}

/// Maps a store [`StorePrefix`] to a filesystem [`PathBuf`].
Expand Down Expand Up @@ -364,11 +364,11 @@ impl ListableStorageTraits for FilesystemStore {
let path = fs_path.file_name().unwrap();
if fs_path.is_dir() {
prefixes.push(StorePrefix::new(
&(prefix.as_str().to_string() + path.to_str().unwrap() + "/"),
prefix.as_str().to_string() + path.to_str().unwrap() + "/",
)?);
} else {
keys.push(StoreKey::new(
&(prefix.as_str().to_owned() + path.to_str().unwrap()),
prefix.as_str().to_owned() + path.to_str().unwrap(),
)?);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/storage/store/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ impl HTTPStore {
pub fn key_to_url(&self, key: &StoreKey) -> Result<Url, url::ParseError> {
let mut url = self.base_url.as_str().to_string();
if !key.as_str().is_empty() {
url += &("/".to_string() + key.as_str().strip_prefix('/').unwrap_or(key.as_str()));
url +=
("/".to_string() + key.as_str().strip_prefix('/').unwrap_or(key.as_str())).as_str();
}
Url::parse(&url)
}
Expand Down
14 changes: 8 additions & 6 deletions src/storage/store/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ impl StoreKey {
/// # Errors
///
/// Returns [`StoreKeyError`] if `key` is not valid according to [`StoreKey::validate()`].
pub fn new(key: &str) -> Result<Self, StoreKeyError> {
if Self::validate(key) {
Ok(Self(key.to_string()))
pub fn new(key: impl Into<String>) -> Result<Self, StoreKeyError> {
let key = key.into();
if Self::validate(&key) {
Ok(Self(key))
} else {
Err(StoreKeyError(key.to_string()))
Err(StoreKeyError(key))
}
}

Expand All @@ -37,7 +38,8 @@ impl StoreKey {
///
/// `key` is not validated, so this can result in an invalid store key.
#[must_use]
pub unsafe fn new_unchecked(key: String) -> Self {
pub unsafe fn new_unchecked(key: impl Into<String>) -> Self {
let key = key.into();
debug_assert!(Self::validate(&key));
Self(key)
}
Expand Down Expand Up @@ -65,7 +67,7 @@ impl StoreKey {
/// Convert to a [`StoreKey`].
#[must_use]
pub fn to_prefix(&self) -> StorePrefix {
StorePrefix::new(&(self.0.clone() + "/")).unwrap_or_else(|_| StorePrefix::root())
StorePrefix::new(self.0.clone() + "/").unwrap_or_else(|_| StorePrefix::root())
}

/// Returns the parent of this key, or [`None`] this key is the root key and it has no parent.
Expand Down
2 changes: 1 addition & 1 deletion src/storage/store/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ impl ListableStorageTraits for MemoryStore {
let components: Vec<_> = key_strip.split('/').collect();
if components.len() > 1 {
prefixes.insert(StorePrefix::new(
&(prefix.as_str().to_string() + components[0] + "/"),
prefix.as_str().to_string() + components[0] + "/",
)?);
} else if let Some(parent) = key.parent() {
if parent.eq(prefix) {
Expand Down
20 changes: 11 additions & 9 deletions src/storage/store/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,12 @@ impl StorePrefix {
/// # Errors
///
/// Returns [`StorePrefixError`] if `prefix` is not valid according to [`StorePrefix::validate`()].
pub fn new(prefix: &str) -> Result<Self, StorePrefixError> {
if Self::validate(prefix) {
Ok(Self(prefix.to_string()))
pub fn new(prefix: impl Into<String>) -> Result<Self, StorePrefixError> {
let prefix = prefix.into();
if Self::validate(&prefix) {
Ok(Self(prefix))
} else {
Err(StorePrefixError(prefix.to_string()))
Err(StorePrefixError(prefix))
}
}

Expand All @@ -46,9 +47,10 @@ impl StorePrefix {
///
/// `prefix` is not validated, so this can result in an invalid store prefix.
#[must_use]
pub unsafe fn new_unchecked(prefix: &str) -> Self {
debug_assert!(Self::validate(prefix));
Self(prefix.to_string())
pub unsafe fn new_unchecked(prefix: impl Into<String>) -> Self {
let prefix = prefix.into();
debug_assert!(Self::validate(&prefix));
Self(prefix)
}

/// The root prefix.
Expand Down Expand Up @@ -79,7 +81,7 @@ impl StorePrefix {
if parent.is_empty() {
unsafe { Self::new_unchecked("") }
} else {
unsafe { Self::new_unchecked(&(parent.to_string() + "/")) }
unsafe { Self::new_unchecked(parent.to_string() + "/") }
}
})
}
Expand All @@ -101,7 +103,7 @@ impl TryFrom<&NodePath> for StorePrefix {
if path.eq("/") {
Self::new("")
} else {
Self::new(&(path.strip_prefix('/').unwrap_or(path).to_string() + "/"))
Self::new(path.strip_prefix('/').unwrap_or(path).to_string() + "/")
}
}
}

0 comments on commit 9a97141

Please sign in to comment.