Skip to content

Commit

Permalink
Refactoring with API improvements (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
vaIgarashi authored Jan 16, 2021
1 parent 5e892d2 commit 1e56293
Show file tree
Hide file tree
Showing 7 changed files with 880 additions and 859 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,3 @@ byteorder = "1.3"
named-binary-tag = "0.3"
bitvec = "0.17"
log = "0.4.11"

[dev-dependencies]
tempfile = "3.1"
35 changes: 29 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,22 @@ anvil-region = "0.4"
#### Read

```rust
use anvil_region::AnvilChunkProvider;
use anvil_region::provider::{FolderRegionProvider, RegionProvider};

let chunk_provider = AnvilChunkProvider::new("test/region");
let provider = FolderRegionProvider::new("test/region");

let chunk_compound_tag = chunk_provider.load_chunk(4, 2).unwrap();
let chunk_x = 4;
let chunk_z = 2;

let region_x = chunk_x >> 31;
let region_z = chunk_z >> 31;

let region_chunk_x = (chunk_x & 31) as u8;
let region_chunk_z = (chunk_z & 31) as u8;

let mut region = provider.get_region(region_x, region_z).unwrap();

let chunk_compound_tag = region.read_chunk(region_chunk_x, region_chunk_z).unwrap();
let level_compound_tag = chunk_compound_tag.get_compound_tag("Level").unwrap();

assert_eq!(level_compound_tag.get_i32("xPos").unwrap(), 4);
Expand All @@ -34,10 +45,22 @@ assert_eq!(level_compound_tag.get_i32("zPos").unwrap(), 2);
#### Write

```rust
use anvil_region::AnvilChunkProvider;
use anvil_region::provider::{FolderRegionProvider, RegionProvider};
use nbt::CompoundTag;

let chunk_provider = AnvilChunkProvider::new("test/region");
let provider = FolderRegionProvider::new("test/region");

let chunk_x = 31;
let chunk_z = 16;

let region_x = chunk_x >> 31;
let region_z = chunk_z >> 31;

let region_chunk_x = (chunk_x & 31) as u8;
let region_chunk_z = (chunk_z & 31) as u8;

let mut region = provider.get_region(region_x, region_z).unwrap();

let mut chunk_compound_tag = CompoundTag::new();
let mut level_compound_tag = CompoundTag::new();

Expand All @@ -48,5 +71,5 @@ level_compound_tag.insert_i32("zPos", 16);

chunk_compound_tag.insert_compound_tag("Level", level_compound_tag);

chunk_provider.save_chunk(31, 16, chunk_compound_tag);
region.write_chunk(region_chunk_x, region_chunk_z, chunk_compound_tag);
```
72 changes: 72 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
use nbt::decode::TagDecodeError;
use std::io;

/// Possible errors while loading the chunk.
#[derive(Debug)]
pub enum ChunkReadError {
/// Chunk at specified coordinates inside region not found.
ChunkNotFound {
region_chunk_x: u8,
region_chunk_z: u8,
},
/// Chunk length overlaps declared maximum.
///
/// This should not occur under normal conditions.
///
/// Region file are corrupted.
LengthExceedsMaximum {
/// Chunk length.
length: u32,
/// Chunk maximum expected length.
maximum_length: u32,
},
/// Currently are only 2 types of compression: Gzip and Zlib.
///
/// This should not occur under normal conditions.
///
/// Region file are corrupted or was introduced new compression type.
UnsupportedCompressionScheme {
/// Compression scheme type id.
compression_scheme: u8,
},
/// I/O Error which happened while were reading chunk data from region file.
IOError { io_error: io::Error },
/// Error while decoding binary data to NBT tag.
///
/// This should not occur under normal conditions.
///
/// Region file are corrupted or a developer error in the NBT library.
TagDecodeError { tag_decode_error: TagDecodeError },
}

impl From<io::Error> for ChunkReadError {
fn from(io_error: io::Error) -> Self {
ChunkReadError::IOError { io_error }
}
}

impl From<TagDecodeError> for ChunkReadError {
fn from(tag_decode_error: TagDecodeError) -> Self {
ChunkReadError::TagDecodeError { tag_decode_error }
}
}

/// Possible errors while saving the chunk.
#[derive(Debug)]
pub enum ChunkWriteError {
/// Chunk length exceeds 1 MB.
///
/// This should not occur under normal conditions.
LengthExceedsMaximum {
/// Chunk length.
length: u32,
},
/// I/O Error which happened while were writing chunk data to region.
IOError { io_error: io::Error },
}

impl From<io::Error> for ChunkWriteError {
fn from(io_error: io::Error) -> Self {
ChunkWriteError::IOError { io_error }
}
}
Loading

0 comments on commit 1e56293

Please sign in to comment.