Skip to content

Commit

Permalink
add module documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
MrlnHi committed Apr 22, 2023
1 parent 7669288 commit 782bb00
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
40 changes: 40 additions & 0 deletions crates/valence_schem/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
# valence_schem

Support for the [Sponge schematic file format](https://github.com/SpongePowered/Schematic-Specification).

This crate implements [Sponge schematics]

Loading schematics (version 1 through 3) from [`Compounds`](Compound) is
supported. Saving schematics to [`Compounds`](Compound) (version 3 only) is
supported.

# Examples

An example that shows how to load and save [schematics] from and to the
filesystem

```rust
# use valence_schem::Schematic;
use flate2::Compression;
fn schem_from_file(path: &str) -> Schematic {
Schematic::load(path).unwrap()
}
fn schem_to_file(schematic: &Schematic, path: &str) {
schematic.save(path);
}
```

There are also methods to serialize and deserialize [schematics] from and to
[`Compounds`](Compound):
```rust
# use valence_schem::Schematic;
use valence_nbt::Compound;
fn schem_from_compound(compound: &Compound) {
let schematic = Schematic::deserialize(compound).unwrap();
let comp = schematic.serialize();
}
```

### See also

Examples in the `examples/` directory

[Sponge schematics]: <https://github.com/SpongePowered/Schematic-Specification>
[schematics]: Schematic
16 changes: 8 additions & 8 deletions crates/valence_schem/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub struct Schematic {
pub offset: IVec3,
blocks: Option<Box<[Block]>>,
biomes: Option<Biomes>,
pub entities: Option<Box<[Entity]>>,
pub entities: Option<Vec<Entity>>,
}

#[derive(Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -431,7 +431,7 @@ impl Schematic {
let Ok(id) = Ident::new(&id[..]) else {
return Err(LoadSchematicError::InvalidBlockEntityId(id.clone()));
};
let Some(kind) = BlockEntityKind::from_ident(id) else {
let Some(kind) = BlockEntityKind::from_ident(id.as_str_ident()) else {
return Err(LoadSchematicError::UnknownBlockEntity(id.to_string()));
};

Expand Down Expand Up @@ -465,10 +465,10 @@ impl Schematic {
let &Value::Int(i) = value else {
return Err(LoadSchematicError::InvalidBiomePalette);
};
let Ok(ident) = Ident::new(biome.clone()) else {
let Ok(ident) = Ident::new(biome) else {
return Err(LoadSchematicError::InvalidBiomeIdent(biome.clone()));
};
Ok((i, ident))
Ok((i, ident.to_string_ident()))
})
.collect();
let palette = palette?;
Expand Down Expand Up @@ -550,7 +550,7 @@ impl Schematic {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let idx = palette.len();
palette.push(biome.clone());
palette.push(biome.to_string_ident());
entry.insert(idx);
idx
}
Expand All @@ -572,7 +572,7 @@ impl Schematic {
_ => unreachable!(),
};

let entities: Option<Box<[Entity]>> = match root.get("Entities") {
let entities = match root.get("Entities") {
Some(Value::List(List::Compound(entities))) => {
let entities: Result<Vec<_>, _> = entities
.iter()
Expand Down Expand Up @@ -604,7 +604,7 @@ impl Schematic {
})
})
.collect();
Some(entities?.into_boxed_slice())
Some(entities?)
}
_ => None,
};
Expand Down Expand Up @@ -1088,7 +1088,7 @@ mod test {
fn schematic_load_save() {
let schem1 = Schematic::load("../../assets/example_schem.schem").unwrap();
const TEST_PATH: &str = "test.schem";
schem1.save(TEST_PATH, Compression::best()).unwrap();
schem1.save(TEST_PATH).unwrap();
let schem2 = Schematic::load(TEST_PATH).unwrap();
assert_eq!(schem1, schem2);
fs::remove_file(TEST_PATH).unwrap();
Expand Down

0 comments on commit 782bb00

Please sign in to comment.