From 9eb02aec6378c23c083fd923cd6040beda6a49d9 Mon Sep 17 00:00:00 2001 From: "Spencer C. Imbleau" Date: Sun, 19 May 2024 11:25:46 -0400 Subject: [PATCH] feat: composition parsing errors are now public (#19) Makes potential errors public so developers can use it. --- CHANGELOG.md | 1 + Cargo.toml | 1 + src/lib.rs | 2 +- src/runtime/mod.rs | 20 ++++++++++++++------ 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index cd7dfe2..e5db52f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Subheadings to categorize changes are `added, changed, deprecated, removed, fixe ### Fixed - ViewBox clipping is now applied to the animation +- Errors that may occur on parsing a lottie composition are now public as `VelatoError`. ([#19](https://github.com/linebender/velato/pull/19)) ## 0.1.0 (2024-03-26) diff --git a/Cargo.toml b/Cargo.toml index 454c159..adf21a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ vello = { version = "0.1.0", default-features = false } vello = { workspace = true } keyframe = "1.1.1" once_cell = "1.19.0" +thiserror = "1.0.61" # For the parser serde = { version = "1.0.197", features = ["derive"] } diff --git a/src/lib.rs b/src/lib.rs index 58f49a4..3d962c0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,4 +50,4 @@ pub(crate) mod schema; // Re-export vello pub use vello; -pub use runtime::{model, Composition, Renderer}; +pub use runtime::{model, Composition, Renderer, VelatoError}; diff --git a/src/runtime/mod.rs b/src/runtime/mod.rs index 442a612..b625eb0 100644 --- a/src/runtime/mod.rs +++ b/src/runtime/mod.rs @@ -3,16 +3,16 @@ mod render; +use crate::import; +use crate::schema::Animation; use std::collections::HashMap; use std::ops::Range; +use thiserror::Error; pub mod model; pub use render::Renderer; -use crate::import; -use crate::schema::Animation; - /// Model of a Lottie file. #[derive(Clone, Default, Debug)] pub struct Composition { @@ -30,16 +30,24 @@ pub struct Composition { pub layers: Vec, } +/// Triggered when is an issue parsing a lottie file. +#[derive(Error, Debug)] +#[non_exhaustive] +pub enum VelatoError { + #[error("Error parsing lottie: {0}")] + Json(#[from] serde_json::Error), +} + impl Composition { /// Creates a new runtime composition from a buffer of Lottie file contents. - pub fn from_slice(source: impl AsRef<[u8]>) -> Result { + pub fn from_slice(source: impl AsRef<[u8]>) -> Result { let source = Animation::from_slice(source.as_ref())?; let composition = import::conv_animation(source); Ok(composition) } /// Creates a new runtime composition from a json object of Lottie file contents. - pub fn from_json(v: serde_json::Value) -> Result { + pub fn from_json(v: serde_json::Value) -> Result { let source = Animation::from_json(v)?; let composition = import::conv_animation(source); Ok(composition) @@ -47,7 +55,7 @@ impl Composition { } impl std::str::FromStr for Composition { - type Err = serde_json::Error; + type Err = VelatoError; fn from_str(s: &str) -> Result { let source = Animation::from_str(s)?;