Skip to content

Commit

Permalink
feat: composition parsing errors are now public (#19)
Browse files Browse the repository at this point in the history
Makes potential errors public so developers can use it.
  • Loading branch information
simbleau committed May 19, 2024
1 parent 81e1937 commit 9eb02ae
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
20 changes: 14 additions & 6 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -30,24 +30,32 @@ pub struct Composition {
pub layers: Vec<model::Layer>,
}

/// 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<Composition, serde_json::Error> {
pub fn from_slice(source: impl AsRef<[u8]>) -> Result<Composition, VelatoError> {
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<Composition, serde_json::Error> {
pub fn from_json(v: serde_json::Value) -> Result<Composition, VelatoError> {
let source = Animation::from_json(v)?;
let composition = import::conv_animation(source);
Ok(composition)
}
}

impl std::str::FromStr for Composition {
type Err = serde_json::Error;
type Err = VelatoError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let source = Animation::from_str(s)?;
Expand Down

0 comments on commit 9eb02ae

Please sign in to comment.