marble-machinations/src/level.rs

73 lines
1.1 KiB
Rust
Raw Normal View History

2024-10-06 12:39:36 +02:00
use serde::Deserialize;
2024-10-06 16:00:12 +02:00
#[derive(Debug, Clone, Deserialize)]
2024-10-06 12:39:36 +02:00
pub struct Level {
id: String,
sort_order: i32,
2024-10-06 12:39:36 +02:00
name: String,
description: String,
#[serde(default)]
is_sandbox: bool,
2024-10-06 12:39:36 +02:00
init_board: Option<String>,
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(),
}
}
2024-10-06 12:39:36 +02:00
}
impl Level {
pub fn id(&self) -> &str {
&self.id
}
pub fn sort_order(&self) -> i32 {
self.sort_order
}
2024-10-06 12:39:36 +02:00
pub fn name(&self) -> &str {
&self.name
}
pub fn description(&self) -> &str {
&self.description
}
pub fn is_sandbox(&self) -> bool {
self.is_sandbox
}
pub fn init_board(&self) -> Option<String> {
2024-10-06 16:00:12 +02:00
self.init_board.clone()
2024-10-06 12:39:36 +02:00
}
pub fn inputs(&self) -> &[u8] {
self.inputs.as_bytes()
2024-10-06 12:39:36 +02:00
}
pub fn outputs(&self) -> &[u8] {
self.outputs.as_bytes()
2024-10-06 12:39:36 +02:00
}
pub fn input_is_text(&self) -> bool {
matches!(self.inputs, IOData::Text(_))
}
pub fn output_is_text(&self) -> bool {
matches!(self.outputs, IOData::Text(_))
}
2024-10-06 12:39:36 +02:00
}