extern crate alloc;
use alloc::{borrow::Cow, collections::BTreeMap};
use crate::MASK_28_BIT;
#[derive(Debug)]
pub struct ResourceBundle<'a> {
name: Cow<'a, str>,
root: Resource<'a>,
pub is_locale_fallback_enabled: bool,
}
impl<'a> ResourceBundle<'a> {
pub fn new(name: Cow<'a, str>, root: Resource<'a>, is_locale_fallback_enabled: bool) -> Self {
Self {
name,
root,
is_locale_fallback_enabled,
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn root(&self) -> &Resource<'_> {
&self.root
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum Resource<'a> {
String(Cow<'a, str>),
Array(Vec<Resource<'a>>),
Table(Table<'a>),
Binary(Cow<'a, [u8]>),
Integer(Int28),
IntVector(Vec<u32>),
}
pub type Table<'a> = BTreeMap<Key<'a>, Resource<'a>>;
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Key<'a>(Cow<'a, str>);
impl Key<'_> {
pub fn as_bytes(&self) -> &[u8] {
self.0.as_bytes()
}
}
impl<'a> From<&'a str> for Key<'a> {
fn from(value: &'a str) -> Self {
Self(Cow::from(value))
}
}
impl From<String> for Key<'_> {
fn from(value: String) -> Self {
Self(Cow::from(value))
}
}
impl<'a> From<Key<'a>> for String {
fn from(value: Key<'a>) -> Self {
value.0.into_owned()
}
}
#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Int28(u32);
impl From<Int28> for i32 {
fn from(value: Int28) -> Self {
((value.0 as i32) << 4) >> 4
}
}
impl From<Int28> for u32 {
fn from(value: Int28) -> Self {
value.0
}
}
impl From<i32> for Int28 {
fn from(value: i32) -> Self {
Self::from(value as u32)
}
}
impl From<u32> for Int28 {
fn from(value: u32) -> Self {
Self(value & MASK_28_BIT)
}
}