cpclib_asm/implementation/instructions.rs
1use cpclib_crunchers::CompressMethod;
2use cpclib_tokens::CrunchType;
3
4use crate::error::AssemblerError;
5
6pub trait Cruncher {
7 /// Crunch the raw data with the dedicated algorithm.
8 /// Fail when there is no data to crunch
9 fn crunch(&self, raw: &[u8]) -> Result<Vec<u8>, AssemblerError>;
10}
11
12impl Cruncher for CrunchType {
13 fn crunch(&self, raw: &[u8]) -> Result<Vec<u8>, AssemblerError> {
14 if raw.is_empty() {
15 return Err(AssemblerError::NoDataToCrunch);
16 }
17
18 let method = match self {
19 CrunchType::LZ48 => Ok(CompressMethod::Lz48),
20 CrunchType::LZ49 => Ok(CompressMethod::Lz49),
21
22 CrunchType::LZSA1 => {
23 Ok(CompressMethod::Lzsa(
24 cpclib_crunchers::lzsa::LzsaVersion::V1,
25 None
26 ))
27 },
28 CrunchType::LZSA2 => {
29 Ok(CompressMethod::Lzsa(
30 cpclib_crunchers::lzsa::LzsaVersion::V2,
31 None
32 ))
33 },
34
35 CrunchType::LZX7 => {
36 Err(AssemblerError::AssemblingError {
37 msg: "LZX7 compression not implemented".to_owned()
38 })
39 },
40 #[cfg(not(target_arch = "wasm32"))]
41 CrunchType::LZ4 => Ok(CompressMethod::Lz4),
42 #[cfg(not(target_arch = "wasm32"))]
43 CrunchType::LZX0 => Ok(CompressMethod::Zx0),
44 #[cfg(not(target_arch = "wasm32"))]
45 CrunchType::LZEXO => Ok(CompressMethod::Exomizer),
46 #[cfg(not(target_arch = "wasm32"))]
47 CrunchType::LZAPU => Ok(CompressMethod::Apultra),
48 #[cfg(not(target_arch = "wasm32"))]
49 CrunchType::Shrinkler => Ok(CompressMethod::Shrinkler(Default::default())),
50 #[cfg(not(target_arch = "wasm32"))]
51 CrunchType::Upkr => Ok(CompressMethod::Upkr) /* #[cfg(target_arch = "wasm32")]
52 * CrunchType::LZ4 => {
53 * Err(AssemblerError::AssemblingError {
54 * msg: "LZ4 compression not available".to_owned()
55 * })
56 * },
57 * #[cfg(target_arch = "wasm32")]
58 * CrunchType::LZX0 => {
59 * Err(AssemblerError::AssemblingError {
60 * msg: "LZX0 compression not available".to_owned()
61 * })
62 * },
63 * #[cfg(target_arch = "wasm32")]
64 * CrunchType::LZEXO => {
65 * Err(AssemblerError::AssemblingError {
66 * msg: "LZEXO compression not available".to_owned()
67 * })
68 * },
69 * #[cfg(target_arch = "wasm32")]
70 * CrunchType::LZAPU => {
71 * Err(AssemblerError::AssemblingError {
72 * msg: "LZAPU compression not available".to_owned()
73 * })
74 * }, */
75 }?;
76
77 method.compress(raw).map_err(|_| {
78 AssemblerError::AssemblingError {
79 msg: "Error when crunching".to_string()
80 }
81 })
82 }
83}