rs-sb3 0.3.1

sb3 json serializer
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! Module to deal with Scratch block

use crate::prelude::*;
use utils::{deserialize_json_str, serialize_json_str};

/// Scratch scripting block
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Block {
    /// A string naming the block.
    pub opcode: OpCode<String>,

    /// Wiki says nothing about this, probably comment id that this block attached to.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub comment: Option<Id>,

    /// The Id of the next block or null.
    pub next: Option<Id>,

    /// If the block is a stack block and is preceded, this is the Id of the preceding block.
    /// If the block is the first stack block in a C mouth, this is the Id of the C block.
    /// If the block is an input to another block, this is the Id of that other block.
    /// Otherwise it is none.
    pub parent: Option<Id>,

    /// See [`BlockInput`]
    pub inputs: StringHashMap<BlockInput>,

    /// See [`BlockField`]
    pub fields: StringHashMap<BlockField>,

    /// True if this is a shadow block and false otherwise.
    pub shadow: bool,

    /// False if the block has a parent and true otherwise.
    pub top_level: bool,

    /// Mutations are present some blocks that has a certain opcode.
    /// See [`BlockMutationEnum`] for availiable mutations.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mutation: Option<BlockMutation>,

    /// X Position of the top level block.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub x: Option<Number>,

    /// Y Position of the top level block.
    #[serde(skip_serializing_if = "Option::is_none", default)]
    pub y: Option<Number>,
}

/// A struct representing inputs into which other blocks may be dropped, including C mouths.
/// idk if is this possible without vec
#[derive(Debug, Default, Clone, PartialEq)]
pub struct BlockInput {
    /// See [`ShadowInputType`]
    pub shadow: ShadowInputType,

    /// Inputs
    pub inputs: Vec<Option<IdOrValue>>,
}

/// Used for [`BlockInput`]
/// When the input could be either [`Id`] or [`BlockInputValue`]
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum IdOrValue {
    /// When it's [`Id`]
    Id(Id),
    /// When it's [`BlockInputValue`]
    Value(BlockInputValue),
}

/// Field of the block
#[derive(Debug, Clone, PartialEq)]
pub enum BlockField {
    /// Field when Id are sometimes needed
    WithId {
        /// Value of the field
        value: Value,

        /// For certain fields,
        /// such as variable and broadcast dropdown menus,
        /// there is also a second element, which is the Id of the field's value.
        id: Option<Id>,
    },
    /// Field with no Id needed
    NoId {
        /// Value of the field
        value: Value,
    },
}

/// Mutation for procedural block (custom block) or stop block
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct BlockMutation {
    /// Always equal to "mutation".
    /// Don't know if there any other tag
    /// Wiki says there's only "mutation" though
    tag_name: String,

    /// Seems to always be an empty array.
    children: [(); 0],

    /// See [`BlockMutationEnum`]
    #[serde(flatten)]
    pub mutation_enum: BlockMutationEnum,
}

/// Different mutation has different properties.
/// This enum define them.
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
pub enum BlockMutationEnum {
    /// opcode is `"procedures_prototype"` mutations have the following additional properties
    ProceduresPrototype {
        /// The name of the custom block, including inputs: %s for string/number inputs and %b for boolean inputs.
        proccode: String,

        /// An array of the ids of the arguments; these can also be found in the input property of the main block.
        #[serde(
            deserialize_with = "deserialize_json_str",
            serialize_with = "serialize_json_str"
        )]
        argumentids: Vec<Id>,

        /// An array of the names of the arguments.
        #[serde(
            deserialize_with = "deserialize_json_str",
            serialize_with = "serialize_json_str"
        )]
        argumentnames: Vec<Name>,

        /// An array of the defaults of the arguments.
        ///  - String default is an empty string
        ///  - bool default is `false`
        #[serde(
            deserialize_with = "deserialize_json_str",
            serialize_with = "serialize_json_str"
        )]
        argumentdefaults: Vec<ValueWithBool>,

        /// Whether to run the block without screen refresh or not.
        #[serde(
            deserialize_with = "deserialize_json_str",
            serialize_with = "serialize_json_str"
        )]
        warp: Option<bool>,
    },

    /// opcode is `"procedures_call"` mutations have the following additional properties
    ProceduresCall {
        /// The name of the custom block, including inputs: %s for string/number inputs and %b for boolean inputs.
        proccode: String,

        /// An array of the ids of the arguments; these can also be found in the input property of the main block.
        #[serde(
            deserialize_with = "deserialize_json_str",
            serialize_with = "serialize_json_str"
        )]
        argumentids: Vec<Id>,

        /// Whether to run the block without screen refresh or not.
        #[serde(
            deserialize_with = "deserialize_json_str",
            serialize_with = "serialize_json_str"
        )]
        warp: Option<bool>,
    },

    /// opcode is `"control_stop"` mutations have the following additional property
    ControlStop {
        /// Whether the block has a block following it or not
        ///  - false for stop all and stop all in sprite
        ///  - true for stop other scripts in sprite)
        #[serde(
            deserialize_with = "deserialize_json_str",
            serialize_with = "serialize_json_str"
        )]
        hasnext: bool,
    },
}

/// Shadow enum for [`BlockInput`]
///
/// Shadow is area inside block input/arg/param or whatever you wanted to call it.
/// It's consisting of:
///  - raw input field where you just type stuff in and optionally can put a reporter in
///  - menu that you can choose but cannot put a reporter in
///  - menu that you can chose and optionally can put a reporter in
///  - or others I might not catch while developing this
///
/// This documentation might not be completed or is completed, idk.
/// Scratch wiki didn't tell anything about this.
#[derive(Debug, Default, Clone, PartialEq, Deserialize_repr, Serialize_repr)]
#[repr(u8)]
pub enum ShadowInputType {
    /// There is a shadow
    Shadow = 1,

    /// There is no shadow
    #[default]
    NoShadow = 2,

    /// There is a shadow but obscured by the input.
    /// The shadow is obscured when reporter is inserted.
    ShadowObscured = 3,
}

/// Input of the BlockInput
#[derive(Debug, Clone, PartialEq)]
pub enum BlockInputValue {
    /// Number input
    Number {
        /// The value
        value: Value,
    },

    /// Postive number input
    PositiveNumber {
        /// The value
        value: Value,
    },

    /// Postive integer input
    PositiveInteger {
        /// The value
        value: Value,
    },

    /// Integer input
    Integer {
        /// The value
        value: Value,
    },

    /// Angle input
    Angle {
        /// The value
        value: Value,
    },

    /// Color input
    Color {
        /// Value, a `#` followed by a hexadecimal numeral representing the color
        value: Value,
    },

    /// String input
    String {
        /// The value
        value: Value,
    },

    /// Broadcast input
    Broadcast {
        /// Name of the broadcast
        name: Name,

        /// Id of the broadcast
        id: Id,
    },

    /// Variable input
    Variable {
        /// Name of the variable
        name: Name,
        /// Id of the variable
        id: Id,
        /// Position X of the variable if top_level
        x: Option<Number>,
        /// Position y of the variable if top_level
        y: Option<Number>,
    },

    /// List input
    List {
        /// Name of the list
        name: Name,
        /// Id of the list
        id: Id,
        /// Position X of the variable if top_level
        x: Option<Number>,
        /// Position y of the variable if top_level
        y: Option<Number>,
    },
}

impl Default for Block {
    /// This create new block that act like it's a top most block
    fn default() -> Self {
        Block {
            opcode: OpCode::default(),
            comment: None,
            next: None,
            parent: None,
            inputs: StringHashMap::default(),
            fields: StringHashMap::default(),
            shadow: false,
            top_level: true,
            mutation: None,
            x: Some(0.into()),
            y: Some(0.into()),
        }
    }
}

// Serde impl ==================================================================

impl BlockInput {
    /// Use for serializing
    fn size_hint(&self) -> usize {
        1 + self.inputs.len()
    }
}

struct BlockInputVisitor;

impl<'de> Visitor<'de> for BlockInputVisitor {
    type Value = BlockInput;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("list that is a block input")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        use serde::de::Error;

        let shadow = seq.next_element::<ShadowInputType>()?.ok_or_else(|| {
            A::Error::invalid_length(0, &"Expected 2 or more elements for block input")
        })?;

        let mut inputs = vec![];
        while let Some(v) = seq.next_element::<Option<IdOrValue>>()? {
            inputs.push(v)
        }

        Ok(BlockInput { shadow, inputs })
    }
}

impl<'de> Deserialize<'de> for BlockInput {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(BlockInputVisitor)
    }
}

impl Serialize for BlockInput {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeSeq;
        let mut s = serializer.serialize_seq(Some(self.size_hint()))?;
        s.serialize_element(&self.shadow)?;
        for v in &self.inputs {
            s.serialize_element(&v)?;
        }
        s.end()
    }
}

impl BlockInputValue {
    fn get_id(&self) -> u8 {
        use BlockInputValue::*;

        match self {
            Number { value: _ } => 4,
            PositiveNumber { value: _ } => 5,
            PositiveInteger { value: _ } => 6,
            Integer { value: _ } => 7,
            Angle { value: _ } => 8,
            Color { value: _ } => 9,
            String { value: _ } => 10,
            Broadcast { name: _, id: _ } => 11,
            Variable {
                name: _,
                id: _,
                x: _,
                y: _,
            } => 12,
            List {
                name: _,
                id: _,
                x: _,
                y: _,
            } => 13,
        }
    }

    fn hint_size(&self) -> usize {
        use BlockInputValue::*;

        match self {
            Number { value: _ } => 1,
            PositiveNumber { value: _ } => 1,
            PositiveInteger { value: _ } => 1,
            Integer { value: _ } => 1,
            Angle { value: _ } => 1,
            Color { value: _ } => 1,
            String { value: _ } => 1,
            Broadcast { name: _, id: _ } => 2,
            Variable {
                name: _,
                id: _,
                x,
                y,
            } => {
                let mut n = 2;
                if x.is_some() {
                    n += 1
                }
                if y.is_some() {
                    n += 1
                }
                n
            }
            List {
                name: _,
                id: _,
                x,
                y,
            } => {
                let mut n = 2;
                if x.is_some() {
                    n += 1
                }
                if y.is_some() {
                    n += 1
                }
                n
            }
        }
    }
}

struct BlockInputValueVisitor;

impl<'de> Visitor<'de> for BlockInputValueVisitor {
    type Value = BlockInputValue;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("list that is a block input value")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        use serde::de::Error;
        use BlockInputValue::{
            Angle, Broadcast, Color, Integer, List, Number as BlockInputNumber, PositiveInteger,
            PositiveNumber, String, Variable,
        };

        fn seq_next_element_error<'de, T, A>(
            seq: &mut A,
            len: usize,
            error: &str,
        ) -> Result<T, A::Error>
        where
            A: serde::de::SeqAccess<'de>,
            T: Deserialize<'de>,
        {
            seq.next_element::<T>()?
                .ok_or_else(|| A::Error::invalid_length(len, &error))
        }

        let vtype: u8 = seq_next_element_error(
            &mut seq,
            0,
            "Expecting 2 or more elements for block input value with any Id",
        )?;

        let value = seq_next_element_error(
            &mut seq,
            1,
            "Expecting 2 or more elements for block input value with any Id",
        )?;

        let res = match vtype {
            4 => BlockInputNumber { value },
            5 => PositiveNumber { value },
            6 => PositiveInteger { value },
            7 => Integer { value },
            8 => Angle { value },
            9 => Color { value },
            10 => String { value },
            11 => {
                let id = seq_next_element_error(
                    &mut seq,
                    3,
                    "Expecting 3 or more elements for block input value with Id 11",
                )?;

                let name = match value {
                    Value::Text(s) => s,
                    Value::Number(_) => {
                        return Err(A::Error::invalid_value(
                            serde::de::Unexpected::Other("number"),
                            &"a string",
                        ))
                    }
                };

                Broadcast { name, id }
            }
            12 => {
                let id = seq_next_element_error(
                    &mut seq, 3,
                    "Expecting 3 or 5 or more elements for block input value with Id 12 - 13 inclusive"
                )?;
                let x = seq.next_element::<Number>()?;
                let y = seq.next_element::<Number>()?;
                let name = match value {
                    Value::Text(s) => s,
                    Value::Number(_) => {
                        return Err(A::Error::invalid_value(
                            serde::de::Unexpected::Other("number"),
                            &"a string",
                        ))
                    }
                };
                Variable { name, id, x, y }
            }
            13 => {
                let id = seq_next_element_error(
                    &mut seq, 3,
                    "Expecting 3 or 5 or more elements for block input value with Id 12 - 13 inclusive"
                )?;
                let x = seq.next_element::<Number>()?;
                let y = seq.next_element::<Number>()?;
                let name = match value {
                    Value::Text(s) => s,
                    Value::Number(_) => {
                        return Err(A::Error::invalid_value(
                            serde::de::Unexpected::Other("number"),
                            &"a string",
                        ))
                    }
                };
                List { name, id, x, y }
            }
            v => {
                return Err(A::Error::invalid_value(
                    serde::de::Unexpected::Unsigned(v.into()),
                    &"Expecting a type id between 4 - 13 inclusive",
                ))
            }
        };

        Ok(res)
    }
}

impl<'de> Deserialize<'de> for BlockInputValue {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(BlockInputValueVisitor)
    }
}

impl Serialize for BlockInputValue {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeSeq;
        use BlockInputValue::*;

        let mut s = serializer.serialize_seq(Some(self.hint_size()))?;
        s.serialize_element(&self.get_id())?;
        match self {
            Number { value }
            | PositiveNumber { value }
            | PositiveInteger { value }
            | Integer { value }
            | Angle { value }
            | Color { value }
            | String { value } => {
                s.serialize_element(value)?;
            }
            Broadcast { name, id } => {
                s.serialize_element(name)?;
                s.serialize_element(id)?;
            }
            Variable { name, id, x, y } | List { name, id, x, y } => {
                s.serialize_element(name)?;
                s.serialize_element(id)?;
                if let Some(x) = x {
                    s.serialize_element(x)?;
                }
                if let Some(y) = y {
                    s.serialize_element(y)?;
                }
            }
        }
        s.end()
    }
}

impl BlockField {
    /// Value of the field
    #[inline(always)]
    pub fn value(&self) -> &Value {
        match self {
            BlockField::WithId { value, id: _ } => value,
            BlockField::NoId { value } => value,
        }
    }

    /// For certain fields,
    /// such as variable and broadcast dropdown menus,
    /// there is also a second element, which is the Id of the field's value.
    #[inline(always)]
    pub fn id(&self) -> Option<&Id> {
        match self {
            BlockField::WithId { value: _, id } => id.as_ref(),
            BlockField::NoId { value: _ } => None,
        }
    }
}

struct BlockFieldVisitor;

impl<'de> Visitor<'de> for BlockFieldVisitor {
    type Value = BlockField;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        formatter.write_str("sequence of values that is a blockfield")
    }

    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: serde::de::SeqAccess<'de>,
    {
        use serde::de::Error;

        let value = seq
            .next_element::<Value>()?
            .ok_or_else(|| A::Error::invalid_length(1, &"length 1 or 2 for BlockField"))?;
        let id = seq.next_element::<Option<Id>>()?;

        Ok(match id {
            Some(id) => BlockField::WithId { value, id },
            None => BlockField::NoId { value },
        })
    }
}

impl<'de> Deserialize<'de> for BlockField {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_seq(BlockFieldVisitor)
    }
}

impl Serialize for BlockField {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        use serde::ser::SerializeSeq;

        match self {
            BlockField::WithId { value, id } => {
                let mut seq = serializer.serialize_seq(Some(2))?;
                seq.serialize_element(value)?;
                seq.serialize_element(id)?;
                seq.end()
            }
            BlockField::NoId { value } => {
                let mut seq = serializer.serialize_seq(Some(1))?;
                seq.serialize_element(value)?;
                seq.end()
            }
        }
    }
}