mca_cuboids/
lib.rs

1#![deny(clippy::all, clippy::pedantic, clippy::cognitive_complexity)]
2#![allow(clippy::cast_possible_wrap)]
3#![feature(test)]
4
5#[cfg(test)]
6extern crate test;
7
8pub use crate::block_sequence::BlockSequence;
9use crate::block_stack::BlockStack;
10use crate::merge::merge_blocks;
11use anyhow::Result;
12use serde::{Deserialize, Serialize};
13
14mod block_plate;
15mod block_sequence;
16mod block_stack;
17mod merge;
18mod read;
19#[derive(Clone, Default)]
20pub struct ExportParams {
21    pub start: BlockCoordinates,
22    pub end: BlockCoordinates,
23    pub skip_blocks: Vec<String>,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize, Eq, PartialEq, Default)]
27pub struct BlockCoordinates {
28    pub x: i32,
29    pub y: i16,
30    pub z: i32,
31}
32
33impl BlockCoordinates {
34    #[must_use]
35    pub fn new(x: i32, y: i16, z: i32) -> BlockCoordinates {
36        BlockCoordinates { x, y, z }
37    }
38}
39/// # Errors
40///
41/// Will return `Err` if `lvl_path` does not exist or the user does not have
42/// permission to read it.
43pub fn export_cuboids(lvl_path: &str, params: ExportParams) -> Result<Vec<BlockSequence>> {
44    let stack = read::read_level(lvl_path, params)?;
45
46    Ok(merge_blocks(stack))
47}
48
49#[cfg(test)]
50mod tests {
51    use super::*;
52
53    #[test]
54    fn export_cuboids_2x2x1() {
55        let result = export_cuboids(
56            "./assets/test_lvl",
57            ExportParams {
58                start: BlockCoordinates::new(1, -63, 1),
59                end: BlockCoordinates::new(2, -63, 2),
60                ..Default::default()
61            },
62        );
63
64        assert_eq!(
65            result.unwrap(),
66            vec![BlockSequence::new(
67                BlockCoordinates::new(1, -63, 1),
68                BlockCoordinates::new(2, -63, 2)
69            )]
70        );
71    }
72    #[test]
73    fn export_cuboids_1x3x1() {
74        let result = export_cuboids(
75            "./assets/test_lvl",
76            ExportParams {
77                start: BlockCoordinates::new(1, -63, 5),
78                end: BlockCoordinates::new(2, -60, 6),
79                ..Default::default()
80            },
81        );
82
83        assert_eq!(
84            result.unwrap(),
85            vec![BlockSequence::new(
86                BlockCoordinates::new(1, -63, 5),
87                BlockCoordinates::new(1, -61, 5)
88            )]
89        );
90    }
91    #[test]
92    fn export_cuboids_2x2x2() {
93        let result = export_cuboids(
94            "./assets/test_lvl",
95            ExportParams {
96                start: BlockCoordinates::new(1, -63, 8),
97                end: BlockCoordinates::new(4, -60, 10),
98                ..Default::default()
99            },
100        );
101
102        assert_eq!(
103            result.unwrap(),
104            vec![BlockSequence::new(
105                BlockCoordinates::new(1, -63, 8),
106                BlockCoordinates::new(2, -62, 9)
107            )]
108        );
109    }
110    #[test]
111    fn export_cuboids_tetris() {
112        let result = export_cuboids(
113            "./assets/test_lvl",
114            ExportParams {
115                start: BlockCoordinates::new(1, -63, 11),
116                end: BlockCoordinates::new(4, -60, 12),
117                ..Default::default()
118            },
119        );
120
121        assert_eq!(
122            result.unwrap(),
123            vec![
124                BlockSequence::new(
125                    BlockCoordinates::new(1, -63, 11),
126                    BlockCoordinates::new(2, -63, 11)
127                ),
128                BlockSequence::new(
129                    BlockCoordinates::new(1, -63, 12),
130                    BlockCoordinates::new(1, -63, 12)
131                )
132            ]
133        );
134    }
135}