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
use flate2::bufread::ZlibDecoder;
use flate2::bufread::ZlibEncoder;
use ruby_marshal::ArrayValue;
use ruby_marshal::FromValue;
use ruby_marshal::FromValueContext;
use ruby_marshal::FromValueError;
use ruby_marshal::IntoValue;
use ruby_marshal::IntoValueError;
use ruby_marshal::StringValue;
use ruby_marshal::Value;
use ruby_marshal::ValueArena;
use ruby_marshal::ValueHandle;
use std::io::Read;

/// A list of compressed scripts
#[derive(Debug)]
pub struct CompressedScriptList {
    /// Scripts
    pub scripts: Vec<CompressedScript>,
}

impl<'a> FromValue<'a> for CompressedScriptList {
    fn from_value(ctx: &FromValueContext<'a>, value: &Value) -> Result<Self, FromValueError> {
        Ok(Self {
            scripts: FromValue::from_value(ctx, value)?,
        })
    }
}
impl IntoValue for CompressedScriptList {
    fn into_value(self, arena: &mut ValueArena) -> Result<ValueHandle, IntoValueError> {
        self.scripts.into_value(arena)
    }
}

/// A list of scripts
#[derive(Debug)]
pub struct ScriptList {
    /// Scripts
    pub scripts: Vec<Script>,
}

impl<'a> FromValue<'a> for ScriptList {
    fn from_value(ctx: &FromValueContext<'a>, value: &Value) -> Result<Self, FromValueError> {
        Ok(Self {
            scripts: FromValue::from_value(ctx, value)?,
        })
    }
}
impl IntoValue for ScriptList {
    fn into_value(self, arena: &mut ValueArena) -> Result<ValueHandle, IntoValueError> {
        self.scripts.into_value(arena)
    }
}

/// Invalid script ruby data
#[derive(Debug)]
pub enum ScriptFromValueError {
    /// The array len was invalid.
    InvalidArrayLen { len: usize },

    /// The name was invalid
    InvalidName { error: std::str::Utf8Error },
}

impl std::fmt::Display for ScriptFromValueError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::InvalidArrayLen { len } => {
                write!(f, "invalid script array len of {len}, expected 3")
            }
            Self::InvalidName { .. } => {
                write!(f, "the script name is invalid")
            }
        }
    }
}

impl std::error::Error for ScriptFromValueError {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Self::InvalidName { error } => Some(error),
            _ => None,
        }
    }
}

impl From<ScriptFromValueError> for FromValueError {
    fn from(error: ScriptFromValueError) -> Self {
        FromValueError::Other {
            error: error.into(),
        }
    }
}

/// A compressed script
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct CompressedScript {
    /// A randomly-generated script id.
    ///
    /// This should always be positive.
    pub id: i32,

    /// The name of the script.
    pub name: String,

    /// The zlib-compressed script data.
    pub data: Vec<u8>,
}

impl<'a> FromValue<'a> for CompressedScript {
    fn from_value(ctx: &FromValueContext<'a>, value: &Value) -> Result<Self, FromValueError> {
        let script: &ArrayValue = FromValue::from_value(ctx, value)?;
        let script = script.value();

        let array_len = script.len();
        if array_len != 3 {
            return Err(ScriptFromValueError::InvalidArrayLen { len: array_len }.into());
        }

        let id: i32 = ctx.from_value(script[0])?;

        let name: &StringValue = ctx.from_value(script[1])?;
        let name = std::str::from_utf8(name.value())
            .map_err(|error| ScriptFromValueError::InvalidName { error })?;

        let data: &StringValue = ctx.from_value(script[2])?;
        let data = data.value();

        Ok(Self {
            id,
            name: name.into(),
            data: data.into(),
        })
    }
}

impl IntoValue for CompressedScript {
    fn into_value(self, arena: &mut ValueArena) -> Result<ValueHandle, IntoValueError> {
        let id = self.id.into_value(arena)?;
        let name = arena.create_string(self.name.into()).into_raw();
        let data = arena.create_string(self.data).into_raw();

        let array = arena.create_array(vec![id, name, data]);

        Ok(array.into())
    }
}

/// A decompressed script
#[derive(Debug, Clone, serde::Deserialize, serde::Serialize)]
pub struct Script {
    /// A randomly-generated script id.
    ///
    /// This should always be positive.
    pub id: i32,

    /// The name of the script.
    pub name: String,

    /// The script data.
    pub data: String,
}

impl<'a> FromValue<'a> for Script {
    fn from_value(ctx: &FromValueContext<'a>, value: &Value) -> Result<Self, FromValueError> {
        let script: CompressedScript = FromValue::from_value(ctx, value)?;

        let mut decoder = ZlibDecoder::new(&*script.data);
        let mut data = String::new();
        decoder
            .read_to_string(&mut data)
            .map_err(FromValueError::new_other)?;

        Ok(Self {
            id: script.id,
            name: script.name,
            data,
        })
    }
}

impl IntoValue for Script {
    fn into_value(self, arena: &mut ValueArena) -> Result<ValueHandle, IntoValueError> {
        let id = self.id.into_value(arena)?;
        let name = arena.create_string(self.name.into()).into_raw();

        let mut encoder = ZlibEncoder::new(self.data.as_bytes(), Default::default());
        let mut data = Vec::new();
        encoder
            .read_to_end(&mut data)
            .map_err(IntoValueError::new_other)?;

        let data = arena.create_string(data).into_raw();

        let array = arena.create_array(vec![id, name, data]);

        Ok(array.into())
    }
}