Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

App steps: { frames: 30 } and { dialog: 1 } play again; a count from JS arrives as 30.0, which u32 and usize refused (#75)

shane39aade79f7b7
diff --git a/engine/rune/src/script.rs b/engine/rune/src/script.rs
index 262b75dc..a712753a 100644
--- a/engine/rune/src/script.rs
+++ b/engine/rune/src/script.rs
@@ -2,7 +2,7 @@
//! element, each by data-testid), plus a key at the focus and frames to wait, played through Rune's input. T3's set adds
//! a hover (the pointer left over an element), the system's appearance and the window's width (docs/targets.feature T3a).
-use serde::Deserialize;
+use serde::{Deserialize, Deserializer, de::Error as _};
use crate::js::App;
@@ -18,13 +18,19 @@ pub enum Step {
/// a key at the focus, by DOM's name ("Enter", "Escape", "ArrowDown", "a")
Key { key: String },
/// frames to run with no input
- Frames { frames: u32 },
+ Frames {
+ #[serde(deserialize_with = "whole")]
+ frames: u32,
+ },
/// the files the app's next fs.pick picks (T2d: a headless check opens no dialog)
Pick { pick: Vec<String> },
/// the file the app's next fs.save saves to
Save { save: String },
/// the button pressed in the app's next dialog.message
- Dialog { dialog: usize },
+ Dialog {
+ #[serde(deserialize_with = "whole")]
+ dialog: usize,
+ },
/// the pointer moved to the element's middle and left there (T3: hover)
Hover { hover: String },
/// the system's appearance (T3: Tailwind's dark:)
@@ -33,6 +39,16 @@ pub enum Step {
Width { width: f32 },
}
+/// A count from JS: JSON from a JS engine writes every number as a double (`30.0`), so a whole double is taken
+/// as the count it is, and anything else (`2.5`, `-1`) is refused.
+fn whole<'de, D: Deserializer<'de>, T: TryFrom<u64>>(d: D) -> Result<T, D::Error> {
+ let n = f64::deserialize(d)?;
+ if !(n >= 0.0 && n.fract() == 0.0 && n < 9_007_199_254_740_992.0) {
+ return Err(D::Error::custom(format!("{n} isn't a whole number of 0 or more")));
+ }
+ T::try_from(n as u64).map_err(|_| D::Error::custom(format!("{n} is too large")))
+}
+
/// `steps` as JSON: an array of steps.
pub fn parse(json: &str) -> Result<Vec<Step>, String> {
serde_json::from_str(json).map_err(|e| format!("the script: {e}"))
@@ -104,3 +120,24 @@ fn queue(s: raea_io_os::Scripted) -> Result<(), String> {
raea_io_os::script(s);
Ok(())
}
+
+#[cfg(test)]
+mod tests {
+ use super::{Step, parse};
+
+ #[test]
+ fn counts_from_js_are_doubles() {
+ let steps = parse(r#"[{"frames":30.0},{"frames":30},{"dialog":1.0},{"press":"add"}]"#).unwrap();
+ assert!(matches!(steps[0], Step::Frames { frames: 30 }));
+ assert!(matches!(steps[1], Step::Frames { frames: 30 }));
+ assert!(matches!(steps[2], Step::Dialog { dialog: 1 }));
+ assert!(matches!(steps[3], Step::Press { .. }));
+ }
+
+ #[test]
+ fn a_count_that_isnt_whole_is_refused() {
+ assert!(parse(r#"[{"frames":2.5}]"#).is_err());
+ assert!(parse(r#"[{"frames":-1}]"#).is_err());
+ assert!(parse(r#"[{"frames":5000000000}]"#).is_err());
+ }
+}