Skip to main content

alpha2_eval/services/
mod.rs

1//! Editor services every surface exports: JSON text in, JSON text out, refusals as a message.
2
3pub mod curves;
4pub mod geometry;
5pub mod time_curves;
6
7use alpha2_geometry::CanvasSize;
8use alpha2_time::MediaTime;
9
10pub(crate) fn canvas(width: u32, height: u32) -> Result<CanvasSize, String> {
11    if width == 0 || height == 0 || width > 16_384 || height > 16_384 {
12        return Err(format!(
13            "a picture is 1 to 16384 pixels on each side, not {width}x{height}"
14        ));
15    }
16    Ok(CanvasSize::new(width, height))
17}
18
19pub(crate) fn aspect(size: CanvasSize) -> f64 {
20    f64::from(size.width) / f64::from(size.height)
21}
22
23/// Ticks from a JavaScript number that is whole and within the safe-integer range.
24///
25/// # Errors
26/// Refuses a number that is not finite, not whole, or beyond the safe-integer range.
27#[allow(clippy::cast_possible_truncation)]
28pub fn time(ticks: f64) -> Result<MediaTime, String> {
29    if !ticks.is_finite() || ticks.fract() != 0.0 || ticks.abs() > 9.007_199_254_740_991e15 {
30        return Err(format!("a time is a whole number of ticks, not {ticks}"));
31    }
32    Ok(MediaTime(ticks as i64))
33}
34
35pub(crate) fn to_text(value: &impl serde::Serialize) -> Result<String, String> {
36    serde_json::to_string(value).map_err(|error| error.to_string())
37}