Skip to content

Commit

Permalink
Add zarrsArraySetAttributes
Browse files Browse the repository at this point in the history
  • Loading branch information
LDeakin committed Aug 12, 2024
1 parent 78c8ebf commit ee5fe56
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added
- Add `zarrsArraySetAttributes`

### Changed
- Bump `cbindgen` to 0.27
- Change `zarrs{LastError,ArrayGetMetadataString,ArrayGetAttributesString}` to return non-const pointers
Expand Down
40 changes: 40 additions & 0 deletions src/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ impl std::ops::Deref for ZarrsArray_T {
}
}

impl std::ops::DerefMut for ZarrsArray_T {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}

/// An opaque handle to a zarr array.
pub type ZarrsArray = *mut ZarrsArray_T;

Expand Down Expand Up @@ -533,3 +539,37 @@ pub unsafe extern "C" fn zarrsArrayGetAttributesString(
*LAST_ERROR = "error converting attributes to a json string".to_string();
ZarrsResult::ZARRS_ERROR_INVALID_METADATA
}

/// Set the array attributes from a JSON string.
///
/// # Errors
/// Returns `ZarrsResult::ZARRS_ERROR_INVALID_METADATA` if attributes is not a valid JSON object (map).
///
/// # Safety
/// `array` must be a valid `ZarrsArray` handle.
#[no_mangle]
pub unsafe extern "C" fn zarrsArraySetAttributes(
array: ZarrsArray,
attributes: FfiStr,
) -> ZarrsResult {
// Validation
if array.is_null() {
return ZarrsResult::ZARRS_ERROR_NULL_PTR;
}
let array = &mut **array;

// Deserialise the attributes
let Ok(serde_json::Value::Object(mut attributes)) =
serde_json::from_str::<serde_json::Value>(attributes.into())
else {
*LAST_ERROR = "error interpreting attributes to a json map".to_string();
return ZarrsResult::ZARRS_ERROR_INVALID_METADATA;
};

// Set the array attributes
let array_attributes = array_fn!(array, attributes_mut);
array_attributes.clear();
array_attributes.append(&mut attributes);

ZarrsResult::ZARRS_SUCCESS
}
12 changes: 12 additions & 0 deletions zarrs.h
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,18 @@ ZarrsResult zarrsArrayRetrieveSubsetSharded(ZarrsArray array,
size_t subsetBytesCount,
uint8_t *pSubsetBytes);

/**
* Set the array attributes from a JSON string.
*
* # Errors
* Returns `ZarrsResult::ZARRS_ERROR_INVALID_METADATA` if attributes is not a valid JSON object (map).
*
* # Safety
* `array` must be a valid `ZarrsArray` handle.
*/
ZarrsResult zarrsArraySetAttributes(ZarrsArray array,
const char* attributes);

/**
* Store a chunk.
*
Expand Down

0 comments on commit ee5fe56

Please sign in to comment.