diff --git a/changelog.md b/changelog.md index 93b4318..ba41b36 100644 --- a/changelog.md +++ b/changelog.md @@ -1,4 +1,5 @@ # 0.7.0 (UNRELEASED) +- Add `AttachmentLoader` for creating region attachments - Upstream fixes - Reduce allocations ([EsotericSoftware/spine-runtimes#2325](https://github.com/EsotericSoftware/spine-runtimes/issues/2325)) - Fix double free of sequences in mesh attachments ([EsotericSoftware/spine-runtimes#2394](https://github.com/EsotericSoftware/spine-runtimes/issues/2394)) diff --git a/ci/src/main.rs b/ci/src/main.rs index 70ef150..1a188d0 100644 --- a/ci/src/main.rs +++ b/ci/src/main.rs @@ -118,6 +118,7 @@ fn doc_check(sh: &Shell) -> anyhow::Result<()> { sh, "cargo doc --workspace --all-features --no-deps --document-private-items" ) + .env("RUSTDOCFLAGS", "-Dwarnings") .run()?; Ok(()) } diff --git a/src/attachment_loader.rs b/src/attachment_loader.rs index fb911dd..1a17077 100644 --- a/src/attachment_loader.rs +++ b/src/attachment_loader.rs @@ -4,20 +4,12 @@ use crate::{ spAttachmentLoader_dispose, }, c_interface::{NewFromPtr, SyncPtr}, - Atlas, Attachment, AttachmentType, RegionProps, Skin, + Atlas, Attachment, AttachmentType, RegionProps, Skin, SpineError, }; -/// Error types related to [`AttachmentLoader`](`crate::AttachmentLoader`). -#[derive(Debug)] -pub enum AttachmentLoaderError { - /// Creating an attachment failed. - /// Check [`error1`](`Self::error1`) and [`error2`](`Self::error2`) for more information. - CreateAttachmentFailed, - InvalidArgument { - field: &'static str, - }, -} - +/// A loader for creating custom attachments. +/// +/// Currently only supports [`Atlas`](`crate::Atlas`) based attachments. #[derive(Debug)] pub struct AttachmentLoader { c_attachment_loader: SyncPtr, @@ -46,20 +38,18 @@ impl AttachmentLoader { /// /// # Errors /// - /// Returns [`AttachmentLoaderError::CreateAttachmentFailed`] if creating the attachment failed. + /// Returns [`SpineError::CreationFailed`] if creating the attachment failed. /// Check [`error1`](`Self::error1`) and [`error2`](`Self::error2`) for more information. - /// Returns [`AttachmentLoaderError::InvalidArgument`] if `name` or `path` contain a null byte. + /// Returns [`SpineError::NulError`] if `name` or `path` contain a null byte. pub fn create_attachment( &self, skin: Option, attachment_type: AttachmentType, name: &str, path: &str, - ) -> Result { - let c_name = std::ffi::CString::new(name) - .map_err(|_| AttachmentLoaderError::InvalidArgument { field: "name" })?; - let c_path = std::ffi::CString::new(path) - .map_err(|_| AttachmentLoaderError::InvalidArgument { field: "path" })?; + ) -> Result { + let c_name = std::ffi::CString::new(name)?; + let c_path = std::ffi::CString::new(path)?; unsafe { let c_name = c_name.as_ptr(); @@ -77,7 +67,7 @@ impl AttachmentLoader { ); if attachment.is_null() { - Err(AttachmentLoaderError::CreateAttachmentFailed) + Err(SpineError::new_creation_failed("Attachment")) } else { Ok(Attachment::new_from_ptr(attachment)) } @@ -88,20 +78,20 @@ impl AttachmentLoader { /// /// # Errors /// - /// Returns [`AttachmentLoaderError::CreateAttachmentFailed`] if creating the attachment failed. + /// Returns [`SpineError::CreationFailed`] if creating the attachment failed. /// Check [`error1`](`Self::error1`) and [`error2`](`Self::error2`) for more information. - /// Returns [`AttachmentLoaderError::InvalidArgument`] if `name` or `path` contain a null byte. + /// Returns [`SpineError::NulError`] if `name` or `path` contain a null byte. pub fn create_region_attachment( &self, skin: Option, name: &str, path: &str, props: &RegionProps, - ) -> Result { + ) -> Result { let attachment = self.create_attachment(skin, AttachmentType::Region, name, path)?; let Some(mut region) = attachment.as_region() else { - return Err(AttachmentLoaderError::CreateAttachmentFailed); + return Err(SpineError::new_creation_failed("RegionAttachment")); }; region.update_from_props(props); @@ -114,12 +104,6 @@ impl AttachmentLoader { c_ptr!(c_attachment_loader, spAttachmentLoader); } -impl Clone for AttachmentLoader { - fn clone(&self) -> Self { - unsafe { AttachmentLoader::new_from_ptr(self.c_ptr()) } - } -} - impl Drop for AttachmentLoader { fn drop(&mut self) { unsafe { diff --git a/src/error.rs b/src/error.rs index fe41dc4..72ade7c 100644 --- a/src/error.rs +++ b/src/error.rs @@ -13,6 +13,8 @@ pub enum SpineError { FailedToReadFile { file: String }, /// An error when a specified path is not utf-8. PathNotUtf8, + /// Failed to create the requested type. + CreationFailed { what: String }, } impl SpineError { @@ -28,6 +30,12 @@ impl SpineError { name: name.to_owned(), } } + + pub(crate) fn new_creation_failed(what: &str) -> Self { + Self::CreationFailed { + what: what.to_owned(), + } + } } impl From for SpineError { @@ -60,6 +68,10 @@ impl fmt::Display for SpineError { write!(f, "Path not utf-8")?; Ok(()) } + SpineError::CreationFailed { what } => { + write!(f, "Failed to create {what}")?; + Ok(()) + } } } } diff --git a/src/region_attachment.rs b/src/region_attachment.rs index d12bdd8..3751f78 100644 --- a/src/region_attachment.rs +++ b/src/region_attachment.rs @@ -12,6 +12,7 @@ use crate::{ #[cfg(feature = "mint")] use mint::Vector2; +/// Properties for updating [`RegionAttachment`]. #[derive(Debug)] pub struct RegionProps { pub x: f32,