Skip to main content

scratch_test_model/
lib.rs

1//! Model to parse the [`*.sb3` file format](https://en.scratch-wiki.info/wiki/Scratch_File_Format)
2//! of the [Scratch](https://scratch.mit.edu/) block-oriented programming language
3//!
4//! (I want to note that I was unable to find the above link to the format specification
5//! when I was developing this so I reverse-engineered the format from example files.
6//! Luckily, it looks like I correctly understood the meaning of the components.
7//! This is the second version of the parser that supports all standard blocks
8//! and some extensions even blocks that aren't supported by the interpreter.)
9//!
10//!
11//! The program components extracted from a `*.sb3` file are represented by the
12//! [`ProjectDoc`] type which offers different methods for parsing a file or
13//! a sequence of bytes representing the file's content.
14//! The simplest is to first parse the document as json as this value
15//! will be borrowed internally.
16//!
17//! ```no_run
18//! # use scratch_test_model::{ProjectDoc, json_from_sb3_file};
19//! let json = json_from_sb3_file("/path/to/file.sb3").unwrap();
20//! let doc = ProjectDoc::from_json(&json);
21//! println!("{doc:#?}");
22//! ```
23//!
24//! # Steps
25//!
26//! ## JSON extraction
27//!
28//! A [Scratch file](https://en.scratch-wiki.info/wiki/Scratch_File_Format#Project_Files)
29//! is a ZIP file with images, sounds, ... and a `project.json`
30//! that contains all information about the program structure.
31//!
32//! If the used decompression algorithm is unable to read the file
33//! or if you want to provide the `project.json` file directly
34//! you can use [`ProjectDoc::from_json`] which can be called with
35//! an already parsed JSON document.
36//!
37//! (See [`ProjectDoc`] for details.)
38//!
39//! (The next sections will explain how all program blocks are
40//! extracted from an entered JSON document
41//! – regardless how it was obtained in this section.)
42//!
43//! ## Targets
44//!
45//! Each document contains some meta information and – most
46//! importantly – a `targets` field containing a list of "targets".
47//! A [target](https://en.scratch-wiki.info/wiki/Scratch_File_Format#Targets)
48//! is a sprite or the background and contains different data.
49//! (The sprites have _local_ variables and the background's
50//! variables and lists are seen as _global_.)
51//!
52//! The relevant information stored in the modeled target:
53//! - [`TargetVariables`]: all variables of the target (local for sprites and global for the stage)
54//! - [`TargetLists`]: all lists of the target (local for sprites and global for the stage)
55//! - [`TargetBlocks`]: collection of all statement, expression, comparison, event, ... blocks of the program
56//! - [`TargetProcedures`]: generated from the blocks.
57//!   (The information about a procedure is split
58//!   across multiple blocks in a weird format.
59//!   This combines this data per procedure)
60//!
61//! (See [`Target`] for details.)
62
63#[allow(unused)]
64use scopes::{Target, TargetBlocks, TargetLists, TargetProcedures, TargetVariables};
65
66pub mod attrs;
67mod aux;
68pub mod blocks;
69pub mod error;
70mod reader;
71pub mod scopes;
72
73#[cfg(feature = "sb3")]
74pub use reader::json_from_sb3_file;
75#[cfg(feature = "sb3")]
76pub use reader::json_from_sb3_stream;
77pub use scopes::ProjectDoc;
78pub type Id = svalue::ARc<str>;
79
80/// everything that the proc-macro needs to reference is here
81#[allow(unused)]
82mod _exports {
83    pub use crate::aux::AttrLocation;
84    pub(crate) use crate::aux::do_for_attrs::{DoForAttrs, DoForAttrsStrategy};
85    pub(crate) use crate::aux::errors::BlockKindError;
86    pub use crate::aux::opcode_trait::{AsOpcodeName, AsOpcodeUnit};
87    pub(crate) use crate::aux::parse_attr::helper_attr_access;
88    pub(crate) use crate::aux::parse_block::{BlockProperties, JsonBlocks, ParseJsonBlock};
89}