Skip to content

Commit

Permalink
refactor: AssetParser -> Parser.
Browse files Browse the repository at this point in the history
It also removes all the `extension` things. sed sucks btw.
  • Loading branch information
UserIsntAvailable committed Oct 14, 2024
1 parent 7ccca1c commit fa20872
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 144 deletions.
8 changes: 4 additions & 4 deletions ashen/src/asset/color_map.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Deref;

use super::{extension::*, AssetParser};
use super::AssetParser;
use crate::{error, utils::nom::*};

const COLORS_COUNT: usize = 256;
Expand Down Expand Up @@ -35,7 +35,7 @@ impl Color {
}
}

impl AssetParser<Wildcard> for Color {
impl AssetParser for Color {
type Output = Self;

type Context<'ctx> = ();
Expand All @@ -55,7 +55,7 @@ pub struct ColorMap {
pub shades: Box<[[Color; COLORS_COUNT]; SHADES_COUNT]>,
}

impl AssetParser<Pack> for ColorMap {
impl AssetParser for ColorMap {
type Output = Self;

type Context<'ctx> = ();
Expand Down Expand Up @@ -155,7 +155,7 @@ mod tests {
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, color_map) = <ColorMap as AssetParser<Pack>>::parser(())(&COLOR_MAP_DATA)?;
let (_, color_map) = <ColorMap as AssetParser>::parser(())(&COLOR_MAP_DATA)?;

output_file(
parsed_file_path!("color-map/monsters.png"),
Expand Down
6 changes: 3 additions & 3 deletions ashen/src/asset/gamma_table.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::mem;

use super::{extension::*, AssetParser};
use super::AssetParser;
use crate::{error, utils::nom::*};

const ROWS_COUNT: usize = 256;
Expand All @@ -12,7 +12,7 @@ pub struct GammaTable {
pub lookups: Box<[[u8; ROWS_COUNT]; COLS_COUNT]>,
}

impl AssetParser<Pack> for GammaTable {
impl AssetParser for GammaTable {
type Output = Self;

type Context<'ctx> = ();
Expand Down Expand Up @@ -61,7 +61,7 @@ mod tests {
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, gamma_table) = <GammaTable as AssetParser<Pack>>::parser(())(&GAMMA_TABLE_DATA)?;
let (_, gamma_table) = <GammaTable as AssetParser>::parser(())(&GAMMA_TABLE_DATA)?;

let gamma_table = gamma_table
.lookups
Expand Down
50 changes: 1 addition & 49 deletions ashen/src/asset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,55 +10,7 @@ pub mod texture;

use crate::utils::nom::{Input, Result};

/// Definition for all available extensions that the engine can parse.
pub mod extension {
#[sealed::sealed]
pub trait Extension: AsRef<str> + for<'str> TryFrom<&'str str> {}

#[derive(Debug, thiserror::Error)]
#[error("The provided extension is invalid '{}'", self.0)]
pub struct ExtensionMismatchError(String);

macro_rules! impl_extension {
($(#[$docs:meta])+ $name:ident => $ext:literal) => {
$(#[$docs])+
pub struct $name;

impl AsRef<str> for $name {
fn as_ref(&self) -> &str {
$ext
}
}

impl TryFrom<&str> for $name {
type Error = ExtensionMismatchError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
if value == $ext {
Ok(Self)
} else {
Err(ExtensionMismatchError(value.to_owned()))
}
}
}

#[sealed::sealed]
impl Extension for $name {}
};
}

impl_extension!(
/// Wildcard
Wildcard => "*"
);

impl_extension!(
/// Extension that implies that the asset comes from ashen's files (packfile).
Pack => "pack"
);
}

pub trait AssetParser<Ext>
pub trait AssetParser
where
Self: Sized,
{
Expand Down
11 changes: 4 additions & 7 deletions ashen/src/asset/model/dat/frame.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::AssetParser, utils::nom::*};

// TODO(nenikitov): Should probably be a fancy utility class
// With generics for data type and dimension
Expand All @@ -12,7 +9,7 @@ pub struct Vec3 {
pub z: f32,
}

impl AssetParser<Wildcard> for Vec3 {
impl AssetParser for Vec3 {
type Output = Self;

type Context<'ctx> = ();
Expand Down Expand Up @@ -52,7 +49,7 @@ pub struct VertexTransform {
origin: Vec3,
}

impl AssetParser<Wildcard> for ModelVertex {
impl AssetParser for ModelVertex {
type Output = Self;

type Context<'ctx> = VertexTransform;
Expand Down Expand Up @@ -97,7 +94,7 @@ pub struct ModelSpecs {
pub frame_size: u32,
}

impl AssetParser<Wildcard> for ModelFrame {
impl AssetParser for ModelFrame {
type Output = Self;

type Context<'ctx> = ModelSpecs;
Expand Down
7 changes: 2 additions & 5 deletions ashen/src/asset/model/dat/header.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::AssetParser, utils::nom::*};

pub struct ModelHeader {
pub triangle_count: u32,
Expand All @@ -18,7 +15,7 @@ pub struct ModelHeader {
pub locator_nodes: [u8; 16],
}

impl AssetParser<Wildcard> for ModelHeader {
impl AssetParser for ModelHeader {
type Output = Self;

type Context<'ctx> = ();
Expand Down
7 changes: 2 additions & 5 deletions ashen/src/asset/model/dat/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::AssetParser, utils::nom::*};

pub struct ModelSequence {
pub frames: Vec<u32>,
}

impl AssetParser<Wildcard> for ModelSequence {
impl AssetParser for ModelSequence {
type Output = Self;

// TODO(nenikitov): Maybe refactor it to not accept full input.
Expand Down
9 changes: 3 additions & 6 deletions ashen/src/asset/model/dat/triangle.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::AssetParser, utils::nom::*};

pub struct ModelPoint {
pub vertex_index: u16,
Expand All @@ -14,7 +11,7 @@ pub struct TextureDimensions {
pub height: u32,
}

impl AssetParser<Wildcard> for ModelPoint {
impl AssetParser for ModelPoint {
type Output = Self;

type Context<'ctx> = &'ctx TextureDimensions;
Expand All @@ -39,7 +36,7 @@ pub struct ModelTriangle {
pub points: [ModelPoint; 3],
}

impl AssetParser<Wildcard> for ModelTriangle {
impl AssetParser for ModelTriangle {
type Output = Self;

type Context<'ctx> = TextureDimensions;
Expand Down
7 changes: 3 additions & 4 deletions ashen/src/asset/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use dat::{
};

use super::{
extension::*,
texture::{Texture, TextureSize},
AssetParser,
};
Expand All @@ -21,7 +20,7 @@ pub struct Model {
pub frames: Vec<ModelFrame>,
}

impl AssetParser<Pack> for Model {
impl AssetParser for Model {
type Output = Self;

type Context<'ctx> = ();
Expand Down Expand Up @@ -95,9 +94,9 @@ mod tests {
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, model) = <Model as AssetParser<Pack>>::parser(())(&MODEL_DATA)?;
let (_, model) = <Model as AssetParser>::parser(())(&MODEL_DATA)?;
let palette = {
let (_, color_map) = <ColorMap as AssetParser<Pack>>::parser(())(&COLOR_MAP_DATA)?;
let (_, color_map) = <ColorMap as AssetParser>::parser(())(&COLOR_MAP_DATA)?;
color_map.shades[15]
};

Expand Down
4 changes: 2 additions & 2 deletions ashen/src/asset/pack_info.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::ops::Index;

use super::{extension::*, AssetParser};
use super::AssetParser;
use crate::utils::nom::*;

#[derive(Debug)]
Expand All @@ -9,7 +9,7 @@ pub struct PackInfo {
pub size: u32,
}

impl AssetParser<Wildcard> for PackInfo {
impl AssetParser for PackInfo {
type Output = Self;

type Context<'ctx> = ();
Expand Down
5 changes: 2 additions & 3 deletions ashen/src/asset/skybox.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use super::{
extension::*,
texture::{Texture, TextureSize},
AssetParser,
};
Expand All @@ -12,7 +11,7 @@ pub struct Skybox {
pub texture: Texture,
}

impl AssetParser<Pack> for Skybox {
impl AssetParser for Skybox {
type Output = Self;

type Context<'ctx> = ();
Expand Down Expand Up @@ -51,7 +50,7 @@ mod tests {
#[test]
#[ignore = "uses Ashen ROM files"]
fn parse_rom_asset() -> eyre::Result<()> {
let (_, skybox) = <Skybox as AssetParser<Pack>>::parser(())(&SKYBOX_DATA)?;
let (_, skybox) = <Skybox as AssetParser>::parser(())(&SKYBOX_DATA)?;

output_file(
parsed_file_path!("skyboxes/level-1.png"),
Expand Down
4 changes: 2 additions & 2 deletions ashen/src/asset/sound/dat/asset_header.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
asset::{extension::*, pack_info::PackInfo, AssetParser},
asset::{pack_info::PackInfo, AssetParser},
utils::nom::*,
};

Expand All @@ -14,7 +14,7 @@ impl SoundAssetHeader {
const HEADER: &'static str = "TSND";
}

impl AssetParser<Wildcard> for SoundAssetHeader {
impl AssetParser for SoundAssetHeader {
type Output = Self;

type Context<'ctx> = ();
Expand Down
4 changes: 2 additions & 2 deletions ashen/src/asset/sound/dat/chunk_header.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use crate::{
asset::{extension::*, pack_info::PackInfo, AssetParser},
asset::{pack_info::PackInfo, AssetParser},
utils::nom::*,
};

pub struct SoundChunkHeader {
pub infos: Vec<PackInfo>,
}

impl AssetParser<Wildcard> for SoundChunkHeader {
impl AssetParser for SoundChunkHeader {
type Output = Self;

type Context<'ctx> = ();
Expand Down
7 changes: 2 additions & 5 deletions ashen/src/asset/sound/dat/pattern_effect.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use super::{convert_volume, finetune::FineTune};
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::AssetParser, utils::nom::*};

#[derive(Debug, Clone, Copy)]
pub enum Speed {
Expand Down Expand Up @@ -130,7 +127,7 @@ impl PatternEffect {
}
}

impl AssetParser<Wildcard> for Option<PatternEffect> {
impl AssetParser for Option<PatternEffect> {
type Output = Self;

type Context<'ctx> = bool;
Expand Down
15 changes: 6 additions & 9 deletions ashen/src/asset/sound/dat/pattern_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::rc::Rc;
use bitflags::bitflags;

use super::{convert_volume, finetune::FineTune, pattern_effect::PatternEffect, t_instrument::*};
use crate::{
asset::{extension::*, AssetParser},
utils::nom::*,
};
use crate::{asset::AssetParser, utils::nom::*};

#[derive(Default, PartialEq, Clone, Copy, Debug)]
pub enum PatternEventNote {
Expand All @@ -15,7 +12,7 @@ pub enum PatternEventNote {
On(FineTune),
}

impl AssetParser<Wildcard> for Option<PatternEventNote> {
impl AssetParser for Option<PatternEventNote> {
type Output = Self;

type Context<'ctx> = bool;
Expand Down Expand Up @@ -51,7 +48,7 @@ bitflags! {
}
}

impl AssetParser<Wildcard> for PatternEventFlags {
impl AssetParser for PatternEventFlags {
type Output = Self;

type Context<'ctx> = ();
Expand All @@ -71,7 +68,7 @@ impl AssetParser<Wildcard> for PatternEventFlags {
}
}

impl AssetParser<Wildcard> for Option<PatternEventInstrument> {
impl AssetParser for Option<PatternEventInstrument> {
type Output = Self;

type Context<'ctx> = (bool, &'ctx [Rc<TInstrument>]);
Expand Down Expand Up @@ -105,7 +102,7 @@ impl Default for PatternEventVolume {
}
}

impl AssetParser<Wildcard> for Option<PatternEventVolume> {
impl AssetParser for Option<PatternEventVolume> {
type Output = Self;

type Context<'ctx> = bool;
Expand Down Expand Up @@ -152,7 +149,7 @@ impl PatternEvent {
}
}

impl AssetParser<Wildcard> for PatternEvent {
impl AssetParser for PatternEvent {
type Output = Self;

type Context<'ctx> = &'ctx [Rc<TInstrument>];
Expand Down
Loading

0 comments on commit fa20872

Please sign in to comment.