PSArc_lib/archive/
archive_flags.rs

1use crate::prelude::*;
2use anyhow::anyhow;
3
4/// **PSArchiveFlags** is the flags for the Playstation Archive file
5#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum PSArchiveFlags {
8    /// Relative paths
9    RELATIVE,
10    /// Case-insensitive paths
11    IGNORECASE,
12    /// Absolute paths
13    ABSOLUTE,
14    /// Error parsing flags
15    ERROR,
16}
17
18/// Should parse 4 bytes, any more or less will result in an error
19impl Parsable for PSArchiveFlags {
20	type Error = anyhow::Error;
21    fn parse(bytes: impl ConvertAsBytes) -> Result<Self, Self::Error> {
22        let bytes = &bytes.convert_as_bytes()[..];
23		match bytes {
24			&[0, 0, 0, 0] => {
25				return Ok(Self::RELATIVE);
26			}
27			&[0, 0, 0, 1] => {
28				return Ok(Self::IGNORECASE);
29			}
30			&[0, 0, 0, 2] => {
31				return Ok(Self::ABSOLUTE);
32			}
33			_ => {
34				return Err(anyhow!("Invalid flags"));
35			}
36		}
37    }
38}
39
40impl Default for PSArchiveFlags {
41    fn default() -> Self {
42        return Self::ERROR;
43    }
44}
45
46impl std::fmt::Display for PSArchiveFlags {
47    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
48        match self {
49            Self::RELATIVE => {
50                write!(f, "relative")
51            }
52            Self::IGNORECASE => {
53                write!(f, "ignorecase")
54            }
55            Self::ABSOLUTE => {
56                write!(f, "absolute")
57            }
58            Self::ERROR => {
59                write!(f, "Error parsing Archive Flags")
60            }
61        }
62    }
63}
64
65#[cfg(test)]
66#[doc(hidden)]
67mod test {
68	use crate::prelude::*;
69	use super::PSArchiveFlags;
70
71	#[test]
72	fn test_flags_parsing() {
73		let bytes = include_bytes!("../../res/test.pak")[0x1C..0x20].to_vec();
74		let result = PSArchiveFlags::parse(bytes);
75		assert_eq!(result.is_ok(), true);
76		let result = result.unwrap();
77		assert_eq!(result, PSArchiveFlags::ABSOLUTE);
78	}
79}