1use std::fmt;
5use std::str::{self, FromStr, Utf8Error};
6use std::string::FromUtf8Error;
7
8#[derive(Clone, PartialEq, Eq, Hash)]
10pub struct ResRef(pub(crate) Vec<u8>);
11
12impl ResRef {
13 #[inline]
15 pub fn as_str(&self) -> Result<&str, Utf8Error> {
16 str::from_utf8(&self.0)
17 }
18 #[inline]
20 pub fn as_string(self) -> Result<String, FromUtf8Error> {
21 String::from_utf8(self.0)
22 }
23}
24
25impl fmt::Debug for ResRef {
26 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27 if let Ok(value) = str::from_utf8(&self.0) {
28 return write!(f, "{}", value);
29 }
30 self.0.fmt(f)
31 }
32}
33
34impl fmt::Display for ResRef {
35 #[inline]
36 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
37 let value = self.as_str().map_err(|_| fmt::Error)?;
38 write!(f, "{}", value)
39 }
40}
41
42impl Into<String> for ResRef {
43 #[inline]
44 fn into(self) -> String {
45 String::from_utf8(self.0).expect("ResRef contains non UTF-8 string")
46 }
47}
48
49impl<'a> Into<&'a str> for &'a ResRef {
50 #[inline]
51 fn into(self) -> &'a str {
52 str::from_utf8(&self.0).expect("ResRef contains non UTF-8 string")
53 }
54}
55
56impl<'a> From<&'a str> for ResRef {
57 #[inline]
58 fn from(str: &'a str) -> Self { ResRef(str.as_bytes().to_owned()) }
59}
60
61impl FromStr for ResRef {
62 type Err = ();
63
64 #[inline]
65 fn from_str(str: &str) -> Result<Self, Self::Err> { Ok(str.into()) }
66}