azalea_chat/
numbers.rs

1//! Contains a few ways to style numbers. At the time of writing, Minecraft only
2//! uses this for rendering scoreboard objectives.
3
4#[cfg(feature = "azalea-buf")]
5use std::io::{self, Cursor, Write};
6
7#[cfg(feature = "azalea-buf")]
8use azalea_buf::{AzaleaRead, AzaleaWrite};
9#[cfg(feature = "azalea-buf")]
10use azalea_registry::builtin::NumberFormatKind;
11use simdnbt::owned::Nbt;
12
13use crate::FormattedText;
14
15#[derive(Clone, Debug, PartialEq)]
16pub enum NumberFormat {
17    Blank,
18    Styled { style: Nbt },
19    Fixed { value: FormattedText },
20}
21
22#[cfg(feature = "azalea-buf")]
23impl AzaleaRead for NumberFormat {
24    fn azalea_read(buf: &mut Cursor<&[u8]>) -> Result<Self, azalea_buf::BufReadError> {
25        let kind = NumberFormatKind::azalea_read(buf)?;
26        match kind {
27            NumberFormatKind::Blank => Ok(NumberFormat::Blank),
28            NumberFormatKind::Styled => Ok(NumberFormat::Styled {
29                style: simdnbt::owned::read(buf)?,
30            }),
31            NumberFormatKind::Fixed => Ok(NumberFormat::Fixed {
32                value: FormattedText::azalea_read(buf)?,
33            }),
34        }
35    }
36}
37
38#[cfg(feature = "azalea-buf")]
39impl AzaleaWrite for NumberFormat {
40    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
41        match self {
42            NumberFormat::Blank => NumberFormatKind::Blank.azalea_write(buf)?,
43            NumberFormat::Styled { style } => {
44                NumberFormatKind::Styled.azalea_write(buf)?;
45                style.azalea_write(buf)?;
46            }
47            NumberFormat::Fixed { value } => {
48                NumberFormatKind::Fixed.azalea_write(buf)?;
49                value.azalea_write(buf)?;
50            }
51        }
52        Ok(())
53    }
54}