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
use serde::{Deserialize, Serialize};
use smart_default::SmartDefault;

#[derive(Debug, Serialize, Deserialize, SmartDefault, PartialEq, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Script {
    #[serde(flatten)]
    pub common_data: crate::CommonData<ConstGmScript>,

    /// Is this used in DragNDrop? Hopefully not! that would get messy.
    pub is_dn_d: bool,

    /// Is this an autogenerated compatibility script?
    pub is_compatibility: bool,

    pub parent: crate::ViewPath,
}

#[derive(Debug, Copy, Serialize, Deserialize, SmartDefault, PartialEq, Eq, Clone)]
pub enum ConstGmScript {
    #[serde(rename = "GMScript")]
    #[default]
    Const,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{utils::TrailingCommaUtility, ResourceVersion, ViewPath, ViewPathLocation};
    use include_dir::{include_dir, Dir, DirEntry};
    use pretty_assertions::assert_eq;

    static ALL_SCRIPTS: Dir = include_dir!("$CARGO_MANIFEST_DIR/data/scripts");

    #[test]
    fn trivial_sprite_parsing() {
        let tcu = TrailingCommaUtility::new();

        for object_file in ALL_SCRIPTS.find("**/*.yy").unwrap() {
            if let DirEntry::File(file) = object_file {
                println!("parsing {}", file.path().display());
                let our_str = std::str::from_utf8(file.contents()).unwrap();
                let our_str = tcu.clear_trailing_comma(our_str);
                serde_json::from_str::<Script>(&our_str).unwrap();
            }
        }
    }

    #[test]
    fn deep_equality() {
        let script_raw = include_str!("../../data/scripts/CameraClass.yy");

        let script_parsed: Script =
            serde_json::from_str(&TrailingCommaUtility::clear_trailing_comma_once(script_raw))
                .unwrap();

        let script = Script {
            common_data: crate::CommonData {
                resource_version: ResourceVersion::default(),
                name: "CameraClass".to_string(),
                resource_type: ConstGmScript::Const,
            },
            is_dn_d: false,
            is_compatibility: false,
            parent: ViewPath {
                name: "Camera".to_string(),
                path: ViewPathLocation("folders/Scripts/Gameplay Systems/Camera.yy".to_string()),
            },
        };

        assert_eq!(script_parsed, script);
    }
}