support using strings directly as level input and output data

This commit is contained in:
Crispy 2024-12-05 14:23:21 +01:00
parent ef01de9dae
commit 36b1b8672b
9 changed files with 35 additions and 38 deletions

View file

@ -9,12 +9,24 @@ pub struct Level {
#[serde(default)]
is_sandbox: bool,
init_board: Option<String>,
inputs: Vec<u8>,
outputs: Vec<u8>,
#[serde(default)]
input_is_text: bool,
#[serde(default)]
output_is_text: bool,
inputs: IOData,
outputs: IOData,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(untagged)]
pub enum IOData {
Bytes(Vec<u8>),
Text(String),
}
impl IOData {
pub fn as_bytes(&self) -> &[u8] {
match self {
IOData::Bytes(b) => b,
IOData::Text(t) => t.as_bytes(),
}
}
}
impl Level {
@ -43,18 +55,18 @@ impl Level {
}
pub fn inputs(&self) -> &[u8] {
&self.inputs
self.inputs.as_bytes()
}
pub fn outputs(&self) -> &[u8] {
&self.outputs
self.outputs.as_bytes()
}
pub fn input_is_text(&self) -> bool {
self.input_is_text
matches!(self.inputs, IOData::Text(_))
}
pub fn output_is_text(&self) -> bool {
self.output_is_text
matches!(self.outputs, IOData::Text(_))
}
}