- attachment data
Publish / Publish (crates.io) (push) Failing after 3m6s

This commit is contained in:
w.pomp
2026-07-14 09:35:34 +02:00
parent 4ccdb7611c
commit 19cdc9a121
7 changed files with 114 additions and 27 deletions
+70 -1
View File
@@ -1,4 +1,5 @@
use crate::error::Error;
use std::ffi::CStr;
use std::mem::MaybeUninit;
pub fn lib_czi_error(code: std::ffi::c_int) -> Result<(), Error> {
@@ -14,7 +15,71 @@ pub fn lib_czi_error(code: std::ffi::c_int) -> Result<(), Error> {
}
}
#[derive(Clone, Debug)]
/// container for some types of attachment data
pub enum AttachmentData {
Float(Vec<f64>),
Xml(String),
Unknown(Vec<u8>),
}
impl AttachmentData {
pub fn attachment_type(&self) -> &str {
match self {
Self::Float(_) => "float",
Self::Xml(_) => "xml",
Self::Unknown(_) => "unknown",
}
}
pub(crate) fn from_float(raw: &[u8]) -> Result<Self, Error> {
if raw.len() < 8 {
Err(Error::MalformedData)
} else {
let number = u32::from_le_bytes(raw[4..8].try_into().unwrap()) as usize;
if raw.len() < 8 * number + 8 {
Err(Error::MalformedData)
} else {
let mut data = Vec::with_capacity(number);
for i in 0..number {
data.push(f64::from_le_bytes(
raw[8 + 8 * i..16 + 8 * i].try_into().unwrap(),
));
}
Ok(Self::Float(data))
}
}
}
pub(crate) fn from_xml(raw: &[u8]) -> Result<Self, Error> {
Ok(Self::Xml(
CStr::from_bytes_until_nul(raw)?
.to_string_lossy()
.to_string(),
))
}
pub fn try_into_float(&self) -> Result<Vec<f64>, Error> {
if let AttachmentData::Float(data) = self {
Ok(data.clone())
} else {
Err(Error::InvalidAttachmentType(
self.attachment_type().to_string(),
))
}
}
pub fn try_into_xml(&self) -> Result<String, Error> {
if let AttachmentData::Xml(data) = self {
Ok(data.clone())
} else {
Err(Error::InvalidAttachmentType(
self.attachment_type().to_string(),
))
}
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
pub enum Dimension {
/// The Z-dimension.
Z = 1,
@@ -48,6 +113,10 @@ impl Dimension {
}
dimensions
}
pub fn is_valid(&self, v: u32) -> bool {
v & (*self as u32).pow(2) > 0
}
}
impl TryFrom<i32> for Dimension {