bitsy-parser 0.65.13

A parser for Bitsy game data
Documentation
extern crate loe;

use std::io::Cursor;
use radix_fmt::radix_36;
use loe::{process, Config, TransformMode};

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 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,
    transition: Option<Transition>,
    dialogue_id: Option<String>,
}

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
    }
}

#[inline]
fn from_base36(str: &str) -> u64 {
    u64::from_str_radix(str, 36).expect(&format!("Invalid base36 string: {}", str))
}

/// this doesn't work inside ToBase36 for some reason
#[inline]
fn to_base36(int: u64) -> String {
    format!("{}", radix_36(int))
}

pub trait ToBase36 {
    fn to_base36(&self) -> String;
}

impl ToBase36 for u64 {
    #[inline]
    fn to_base36(&self) -> String {
        to_base36(*self)
    }
}

/// e.g. `\nNAME DLG_0`
#[inline]
fn optional_data_line<T: Display>(label: &str, item: Option<T>) -> String {
    if item.is_some() {
        format!("\n{} {}", label, item.unwrap())
    } else {
        "".to_string()
    }
}

#[inline]
fn transform_line_endings(input: String, mode: TransformMode) -> String {
    let mut input = Cursor::new(input);
    let mut output = Cursor::new(Vec::new());

    process(&mut input, &mut output, Config::default().transform(mode)).unwrap();
    String::from_utf8(output.into_inner()).unwrap()
}

#[inline]
fn segments_from_string(string: String) -> Vec<String> {
    // this is pretty weird but a dialogue can just have an empty line followed by a name
    // however, on entering two empty lines, dialogue will be wrapped in triple quotation marks
    // so, handle this here
    let string = string.replace("\n\nNAME", "\n\"\"\"\n\"\"\"\nNAME");

    let mut output:Vec<String> = Vec::new();
    // are we inside `"""\n...\n"""`? if so, ignore empty lines
    let mut inside_escaped_block = false;
    let mut current_segment : Vec<String> = Vec::new();

    for line in string.lines() {
        if line == "\"\"\"" {
            inside_escaped_block = ! inside_escaped_block;
        }

        if line == "" && !inside_escaped_block {
            output.push(current_segment.join("\n"));
            current_segment = Vec::new();
        } else {
            current_segment.push(line.to_string());
        }
    }

    output.push(current_segment.join("\n"));

    output
}

/// for some reason the sprites with numeric IDs go first,
/// then SPR A (avatar), then all the non-numeric IDs
#[inline]
fn is_string_numeric(str: String) -> bool {
    for c in str.chars() {
        if !c.is_numeric() {
            return false;
        }
    }

    return true;
}

pub trait Quote {
    fn quote(&self) -> String;
}

impl Quote for String {
    #[inline]
    fn quote(&self) -> String {
        format!("\"\"\"\n{}\n\"\"\"", self).to_string()
    }
}

pub trait Unquote {
    fn unquote(&self) -> String;
}

impl Unquote for String {
    #[inline]
    fn unquote(&self) -> String {
        self.trim_matches('\"').trim_matches('\n').to_string()
    }
}

#[cfg(test)]
mod test {
    use crate::{from_base36, ToBase36, optional_data_line, mock, segments_from_string, Quote, Unquote};

    #[test]
    fn test_from_base36() {
        assert_eq!(from_base36("0"), 0);
        assert_eq!(from_base36("0z"), 35);
        assert_eq!(from_base36("11"), 37);
    }

    #[test]
    fn test_to_base36() {
        assert_eq!((37 as u64).to_base36(), "11");
    }

    #[test]
    fn test_optional_data_line() {
        let output = optional_data_line("NAME", mock::item().name);
        assert_eq!(output, "\nNAME door".to_string());
    }

    #[test]
    fn test_string_to_segments() {
        let output = segments_from_string(
            include_str!("./test-resources/segments").to_string()
        );

        let expected = vec![
            "\"\"\"\nthe first segment is a long bit of text\n\n\nit contains empty lines\n\n\"\"\"".to_string(),
            "this is a new segment\nthis is still the second segment\nblah\nblah".to_string(),
            "DLG SEGMENT_3\n\"\"\"\nthis is a short \"long\" bit of text\n\"\"\"".to_string(),
            "this is the last segment".to_string(),
        ];

        assert_eq!(output, expected);
    }

    #[test]
    fn test_quote() {
        let output = "this is a string.\nIt has 2 lines".to_string().quote();
        let expected = "\"\"\"\nthis is a string.\nIt has 2 lines\n\"\"\"".to_string();
        assert_eq!(output, expected);
    }

    #[test]
    fn test_unquote() {
        let output = "\"\"\"\nwho the fuck is scraeming \"LOG OFF\" at my house.\nshow yourself, coward.\ni will never log off\n\"\"\"".to_string().unquote();
        let expected = "who the fuck is scraeming \"LOG OFF\" at my house.\nshow yourself, coward.\ni will never log off".to_string();
        assert_eq!(output, expected);
    }
}