Skip to main content

nwnrs_resmemfile/
types.rs

1use std::{fmt, io};
2
3use nwnrs_resman::prelude::*;
4use nwnrs_resref::prelude::*;
5
6/// Errors returned while building an in-memory resource container.
7#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ResMemFileError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            ResMemFileError::Io(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "Io",
                    &__self_0),
            ResMemFileError::ResMan(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f, "ResMan",
                    &__self_0),
        }
    }
}Debug)]
8pub enum ResMemFileError {
9    /// Stream setup failed.
10    Io(io::Error),
11    /// Resource manager setup failed.
12    ResMan(ResManError),
13}
14
15impl fmt::Display for ResMemFileError {
16    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
17        match self {
18            Self::Io(error) => error.fmt(f),
19            Self::ResMan(error) => error.fmt(f),
20        }
21    }
22}
23
24impl std::error::Error for ResMemFileError {}
25
26impl From<io::Error> for ResMemFileError {
27    fn from(value: io::Error) -> Self {
28        Self::Io(value)
29    }
30}
31
32impl From<ResManError> for ResMemFileError {
33    fn from(value: ResManError) -> Self {
34        Self::ResMan(value)
35    }
36}
37
38/// An in-memory resource exposed as a one-entry resource container.
39#[derive(#[automatically_derived]
impl ::core::fmt::Debug for ResMemFile {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "ResMemFile",
            "label", &self.label, "entry", &self.entry, "len", &&self.len)
    }
}Debug, #[automatically_derived]
impl ::core::clone::Clone for ResMemFile {
    #[inline]
    fn clone(&self) -> ResMemFile {
        ResMemFile {
            label: ::core::clone::Clone::clone(&self.label),
            entry: ::core::clone::Clone::clone(&self.entry),
            len: ::core::clone::Clone::clone(&self.len),
        }
    }
}Clone)]
40pub struct ResMemFile {
41    pub(crate) label: String,
42    pub(crate) entry: Res,
43    pub(crate) len:   usize,
44}
45
46impl ResMemFile {
47    /// Returns the display label used for this container.
48    #[must_use]
49    pub fn label(&self) -> &str {
50        &self.label
51    }
52
53    /// Returns the byte length of the stored payload.
54    #[must_use]
55    pub fn len(&self) -> usize {
56        self.len
57    }
58
59    /// Returns whether the stored payload is empty.
60    #[must_use]
61    pub fn is_empty(&self) -> bool {
62        self.len == 0
63    }
64
65    /// Returns the contained resource entry.
66    #[must_use]
67    pub fn res(&self) -> Res {
68        self.entry.clone()
69    }
70}
71
72impl fmt::Display for ResMemFile {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        f.write_fmt(format_args!("ResMemFile:{0}", self.label))write!(f, "ResMemFile:{}", self.label)
75    }
76}
77
78impl ResContainer for ResMemFile {
79    fn contains(&self, rr: &ResRef) -> bool {
80        self.entry.resref() == *rr
81    }
82
83    fn demand(&self, rr: &ResRef) -> ResManResult<Res> {
84        if self.contains(rr) {
85            Ok(self.entry.clone())
86        } else {
87            Err(ResManError::Message(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("not found: {0}", rr))
    })format!("not found: {rr}")))
88        }
89    }
90
91    fn count(&self) -> usize {
92        1
93    }
94
95    fn contents(&self) -> Vec<ResRef> {
96        ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [self.entry.resref()]))vec![self.entry.resref()]
97    }
98}
99
100/// Result type for in-memory resource operations.
101pub type ResMemFileResult<T> = Result<T, ResMemFileError>;