sb3_decoder/decoder/raw_structs/
raw_target.rs

1//! The raw_target module contains the [`RawTarget`] enum.
2//!
3//! It also contains the [`RawSprite`], [`RawVariable`] and [`RawStage`] structs.
4
5use std::collections::HashMap;
6
7use crate::decoder::{RawBlock, RawCostume, RawSound};
8
9/// The [`RawTarget`] enum represents either a sprite or the stage in its raw form in a Scratch 3.0
10/// project.
11pub enum RawTarget {
12    /// A sprite target.
13    Sprite(RawSprite),
14
15    /// The stage target.
16    Stage(RawStage),
17}
18
19impl<'de> serde::Deserialize<'de> for RawTarget {
20    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
21    where
22        D: serde::Deserializer<'de>,
23    {
24        let v = serde_json::Value::deserialize(deserializer)?;
25        let is_stage = v
26            .get("isStage")
27            .and_then(|val| val.as_bool())
28            .ok_or_else(|| serde::de::Error::missing_field("isStage"))?;
29        if is_stage {
30            let stage: RawStage = serde_json::from_value(v).map_err(serde::de::Error::custom)?;
31            Ok(RawTarget::Stage(stage))
32        } else {
33            let sprite: RawSprite = serde_json::from_value(v).map_err(serde::de::Error::custom)?;
34            Ok(RawTarget::Sprite(sprite))
35        }
36    }
37}
38
39/// The [`RawSprite`] struct represents a sprite in its raw form in a Scratch 3.0 project.
40#[derive(serde::Deserialize)]
41#[serde(rename_all = "camelCase")]
42pub struct RawSprite {
43    /// The name of the sprite.
44    pub name: String,
45
46    /// The variables of the sprite.
47    pub variables: HashMap<String, RawVariable>,
48
49    /// The lists of the sprite.
50    pub lists: HashMap<String, (String, Vec<serde_json::Value>)>,
51
52    /// The blocks of the sprite stored as a map from block ID to [`RawBlock`].
53    pub blocks: HashMap<String, RawBlock>,
54
55    /// The index of the current costume.
56    pub current_costume: usize,
57
58    /// The costumes of the sprite.
59    pub costumes: Vec<RawCostume>,
60
61    /// The sounds of the sprite.
62    pub sounds: Vec<RawSound>,
63
64    /// The volume of the sprite.
65    pub volume: u8,
66
67    /// The layer of the sprite.
68    pub layer_order: isize,
69
70    /// If the sprite is visible.
71    pub visible: bool,
72
73    /// The x position of the sprite.
74    pub x: i32,
75
76    /// The y position of the sprite.
77    pub y: i32,
78
79    /// The size of the sprite.
80    pub size: u32,
81
82    /// The direction of the sprite.
83    pub direction: i32,
84
85    /// If the sprite is draggable.
86    pub draggable: bool,
87
88    /// The rotation style of the sprite.
89    pub rotation_style: String,
90}
91
92/// The [`RawStage`] struct represents the stage in its raw form in a Scratch 3.0 project.
93#[derive(serde::Deserialize)]
94#[serde(rename_all = "camelCase")]
95pub struct RawStage {
96    /// All the global variables that can be accessed by all sprites.
97    pub variables: HashMap<String, RawVariable>,
98
99    /// All the global lists that can be accessed by all sprites.
100    pub lists: HashMap<String, (String, Vec<serde_json::Value>)>,
101
102    /// All the broadcasts that can be used by all sprites.
103    pub broadcasts: HashMap<String, String>,
104
105    /// The blocks of the stage stored as a map from block ID to [`RawBlock`].
106    pub blocks: HashMap<String, RawBlock>,
107
108    /// The index of the current backdrop.
109    #[serde(rename = "currentCostume")]
110    pub current_backdrop: usize,
111
112    /// The backdrops of the stage.
113    #[serde(rename = "costumes")]
114    pub backdrops: Vec<RawCostume>,
115
116    /// The sounds of the stage.
117    pub sounds: Vec<RawSound>,
118
119    /// The volume of the stage.
120    pub volume: u8,
121}
122
123/// The [`RawVariable`] struct represents a variable in its raw form in a Scratch 3.0 project.
124#[derive(serde::Deserialize)]
125pub struct RawVariable {
126    /// The name of the variable.
127    pub name: String,
128
129    /// The value of the variable.
130    pub value: serde_json::Value,
131}