Skip to main content

twine_codec/
rloc16.rs

1// Copyright (c) 2026 Jake Swensen
2// SPDX-License-Identifier: MPL-2.0
3//
4// This Source Code Form is subject to the terms of the Mozilla Public
5// License, v. 2.0. If a copy of the MPL was not distributed with this
6// file, You can obtain one at http://mozilla.org/MPL/2.0/.
7
8use core::str::FromStr;
9
10use crate::error::TwineCodecError;
11
12pub struct Rloc16(u16);
13
14impl From<Rloc16> for u16 {
15    fn from(value: Rloc16) -> Self {
16        value.0
17    }
18}
19
20impl From<u16> for Rloc16 {
21    fn from(rloc16: u16) -> Self {
22        Self(rloc16)
23    }
24}
25
26impl FromStr for Rloc16 {
27    type Err = TwineCodecError;
28
29    fn from_str(s: &str) -> Result<Self, Self::Err> {
30        let value = u16::from_str_radix(s, 16).map_err(|_| TwineCodecError::StringParseError)?;
31        Ok(Self(value))
32    }
33}
34
35impl core::fmt::Display for Rloc16 {
36    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
37        write!(f, "0x{:04x}", self.0)
38    }
39}