zinc64_loader/
lib.rs

1// This file is part of zinc64.
2// Copyright (c) 2016-2019 Sebastian Jastrzebski. All rights reserved.
3// Licensed under the GPLv3. See LICENSE file in the project root for full license text.
4
5#![cfg_attr(not(feature = "std"), no_std)]
6#![cfg_attr(not(feature = "std"), feature(alloc))]
7
8#[cfg(not(feature = "std"))]
9#[macro_use]
10extern crate alloc;
11#[cfg(feature = "std")]
12extern crate core;
13#[macro_use]
14extern crate log;
15
16mod bin;
17mod crt;
18mod io;
19mod p00;
20mod prg;
21mod tap;
22
23#[cfg(not(feature = "std"))]
24use alloc::prelude::*;
25use zinc64_emu::system::{AutostartMethod, Image};
26
27pub use crate::bin::BinLoader;
28pub use crate::io::{Reader, Result};
29
30pub enum Format {
31    Bin,
32    Crt,
33    P00,
34    Prg,
35    Tap,
36}
37
38impl Format {
39    pub fn from_ext(ext: Option<&str>) -> Option<Format> {
40        match ext {
41            Some("bin") => Some(Format::Bin),
42            Some("crt") => Some(Format::Crt),
43            Some("p00") => Some(Format::P00),
44            Some("P00") => Some(Format::P00),
45            Some("prg") => Some(Format::Prg),
46            Some("tap") => Some(Format::Tap),
47            _ => None,
48        }
49    }
50}
51
52pub trait Loader {
53    fn autostart(&self, path: &mut dyn Reader) -> io::Result<AutostartMethod>;
54    fn load(&self, path: &mut dyn Reader) -> io::Result<Box<dyn Image>>;
55}
56
57pub struct Loaders;
58
59impl Loaders {
60    pub fn from(kind: Format) -> Box<Loader> {
61        match kind {
62            Format::Bin => Box::new(bin::BinLoader::new(1024)),
63            Format::Crt => Box::new(crt::CrtLoader::new()),
64            Format::P00 => Box::new(p00::P00Loader::new()),
65            Format::Prg => Box::new(prg::PrgLoader::new()),
66            Format::Tap => Box::new(tap::TapLoader::new()),
67        }
68    }
69
70    pub fn from_ext(ext: Option<&str>) -> Result<Box<Loader>> {
71        if let Some(kind) = Format::from_ext(ext) {
72            Ok(Loaders::from(kind))
73        } else {
74            Err(format!("Unknown image extension {}", ext.unwrap_or("")))
75        }
76    }
77}