Skip to main content

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::AzBuf;
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 AzBuf 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    fn azalea_write(&self, buf: &mut impl Write) -> io::Result<()> {
37        match self {
38            NumberFormat::Blank => NumberFormatKind::Blank.azalea_write(buf)?,
39            NumberFormat::Styled { style } => {
40                NumberFormatKind::Styled.azalea_write(buf)?;
41                style.azalea_write(buf)?;
42            }
43            NumberFormat::Fixed { value } => {
44                NumberFormatKind::Fixed.azalea_write(buf)?;
45                value.azalea_write(buf)?;
46            }
47        }
48        Ok(())
49    }
50}