bitsy-parser/src/lib.rs
2020-04-18 15:58:30 +00:00

116 lines
2.2 KiB
Rust

use radix_fmt::radix_36;
pub mod avatar;
pub mod colour;
pub mod dialogue;
pub mod ending;
pub mod exit;
pub mod game;
pub mod image;
pub mod item;
pub mod mock;
pub mod palette;
pub mod position;
pub mod room;
pub mod sprite;
pub mod text;
pub mod tile;
pub mod variable;
use avatar::Avatar;
use colour::Colour;
use dialogue::Dialogue;
use ending::Ending;
use exit::{Exit, Transition};
use game::{Game, Version};
use image::Image;
use item::Item;
use palette::Palette;
use position::Position;
use room::Room;
use sprite::Sprite;
use std::fmt::Display;
use text::{Font, TextDirection};
use tile::Tile;
use variable::Variable;
#[derive(Debug, Eq, PartialEq)]
pub struct Instance {
position: Position,
id: String, // item / ending.rs id
}
#[derive(Debug, Eq, PartialEq)]
pub struct ExitInstance {
position: Position,
exit: Exit,
}
pub trait AnimationFrames {
fn to_string(&self) -> String;
}
impl AnimationFrames for Vec<Image> {
#[inline]
fn to_string(&self) -> String {
let mut string = String::new();
let last_frame = self.len() - 1;
for (i, frame) in self.into_iter().enumerate() {
string.push_str(&frame.to_string());
if i < last_frame {
string.push_str(&"\n>\n".to_string());
}
}
string
}
}
fn from_base36(str: &str) -> u64 {
u64::from_str_radix(str, 36).unwrap()
}
#[test]
fn test_from_base36() {
assert_eq!(from_base36("0"), 0);
assert_eq!(from_base36("0z"), 35);
assert_eq!(from_base36("11"), 37);
}
/// this doesn't work inside ToBase36 for some reason
fn to_base36(int: u64) -> String {
format!("{}", radix_36(int))
}
pub trait ToBase36 {
fn to_base36(&self) -> String;
}
impl ToBase36 for u64 {
fn to_base36(&self) -> String {
to_base36(*self)
}
}
#[test]
fn test_to_base36() {
assert_eq!((37 as u64).to_base36(), "11");
}
/// e.g. `\nNAME DLG_0`
fn optional_data_line<T: Display>(label: &str, item: Option<T>) -> String {
if item.is_some() {
format!("\n{} {}", label, item.unwrap())
} else {
"".to_string()
}
}
#[test]
fn test_optional_data_line() {
let output = optional_data_line("NAME", mock::item().name);
assert_eq!(output, "\nNAME door".to_string());
}