-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request '
edcode
support for Text' (#55) from text-edcode…
… into main Reviewed-on: https://codeberg.org/DM-Earth/rimecraft/pulls/55 Reviewed-by: C191239 <[email protected]>
- Loading branch information
Showing
11 changed files
with
141 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! `edcode2` crate integration. | ||
use core::marker::PhantomData; | ||
|
||
use edcode2::{Buf, BufMut, Decode, Encode}; | ||
|
||
use crate::nbt::{ReadNbt, WriteNbt}; | ||
|
||
/// A type that wraps a value with a context. | ||
#[derive(Debug)] | ||
pub struct Nbt<T, Cx>(pub T, PhantomData<Cx>); | ||
|
||
impl<T, Cx> Nbt<T, Cx> { | ||
/// Creates a new `Nbt` with the given value. | ||
#[inline] | ||
pub const fn new(value: T) -> Self { | ||
Self(value, PhantomData) | ||
} | ||
|
||
/// Consumes the `Nbt` and returns the inner value. | ||
#[inline] | ||
pub fn into_inner(self) -> T { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<T, Cx> From<T> for Nbt<T, Cx> { | ||
#[inline] | ||
fn from(value: T) -> Self { | ||
Self::new(value) | ||
} | ||
} | ||
|
||
impl<B, T, Cx> Encode<B> for Nbt<T, Cx> | ||
where | ||
B: BufMut, | ||
Cx: for<'s> WriteNbt<&'s T>, | ||
{ | ||
#[inline] | ||
fn encode(&self, buf: B) -> Result<(), edcode2::BoxedError<'static>> { | ||
Cx::write_nbt(&self.0, buf.writer()).map_err(Into::into) | ||
} | ||
} | ||
|
||
impl<'de, B, T, Cx> Decode<'de, B> for Nbt<T, Cx> | ||
where | ||
B: Buf, | ||
Cx: ReadNbt<T>, | ||
{ | ||
#[inline] | ||
fn decode(buf: B) -> Result<Self, edcode2::BoxedError<'de>> { | ||
Cx::read_nbt(buf.reader()) | ||
.map(Self::new) | ||
.map_err(Into::into) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//! NBT `edcode`-ing related marker traits. | ||
use std::io; | ||
|
||
use crate::GlobalContext; | ||
|
||
/// Marker trait for global contexts that can write nbt tags to a [`io::Write`] object. | ||
pub trait WriteNbt<T>: GlobalContext { | ||
/// Function that performs writing operation. | ||
/// | ||
/// # Errors | ||
/// | ||
/// I/O errors. | ||
fn write_nbt<W>(value: T, writer: W) -> Result<(), io::Error> | ||
where | ||
W: io::Write; | ||
} | ||
|
||
/// Marker trait for global contexts that can read nbt tags from a [`io::Read`] object. | ||
pub trait ReadNbt<T>: GlobalContext { | ||
/// Function that performs reading operation. | ||
/// | ||
/// # Errors | ||
/// | ||
/// I/O errors. | ||
fn read_nbt<R>(reader: R) -> Result<T, io::Error> | ||
where | ||
R: io::Read; | ||
} | ||
|
||
/// Marker trait for global contexts that can update existing nbt tags from a [`io::Read`] object. | ||
pub trait UpdateNbt<T: ?Sized>: GlobalContext { | ||
/// Function that performs updating operation. | ||
/// | ||
/// # Errors | ||
/// | ||
/// I/O errors. | ||
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), io::Error> | ||
where | ||
R: io::Read; | ||
} | ||
|
||
impl<T, Cx> UpdateNbt<T> for Cx | ||
where | ||
Cx: ReadNbt<T>, | ||
{ | ||
#[inline] | ||
fn update_nbt<R>(value: &mut T, reader: R) -> Result<(), io::Error> | ||
where | ||
R: io::Read, | ||
{ | ||
*value = Self::read_nbt(reader)?; | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters