alpm_types/package/contents.rs
1//! Data related to package file contents.
2
3use std::str::FromStr;
4
5/// The name of an [alpm-install-scriptlet] file in an [alpm-package].
6///
7/// [alpm-install-scriptlet]: https://alpm.archlinux.page/specifications/alpm-install-scriptlet.5.html
8/// [alpm-package]: https://alpm.archlinux.page/specifications/alpm-package.7.html
9pub const INSTALL_SCRIPTLET_FILE_NAME: &str = ".INSTALL";
10
11/// The name of a required metadata file in an [alpm-package].
12///
13/// [alpm-package]: https://alpm.archlinux.page/specifications/alpm-package.7.html
14#[derive(
15 strum::AsRefStr,
16 Clone,
17 Copy,
18 Debug,
19 serde::Deserialize,
20 strum::Display,
21 Eq,
22 strum::IntoStaticStr,
23 PartialEq,
24 serde::Serialize,
25)]
26#[serde(try_from = "String", into = "String")]
27pub enum MetadataFileName {
28 /// The [BUILDINFO] file.
29 ///
30 /// [BUILDINFO]: https://alpm.archlinux.page/specifications/BUILDINFO.5.html
31 #[strum(to_string = ".BUILDINFO")]
32 BuildInfo,
33
34 /// The [ALPM-MTREE] file.
35 ///
36 /// [ALPM-MTREE]: ahttps://alpm.archlinux.page/specifications/ALPM-MTREE.5.html
37 #[strum(to_string = ".MTREE")]
38 Mtree,
39
40 /// The [PKGINFO] file.
41 ///
42 /// [PKGINFO]: https://alpm.archlinux.page/specifications/PKGINFO.5.html
43 #[strum(to_string = ".PKGINFO")]
44 PackageInfo,
45}
46
47impl FromStr for MetadataFileName {
48 type Err = crate::Error;
49
50 /// Creates a [`MetadataFileName`] from string slice.
51 ///
52 /// # Errors
53 ///
54 /// Returns an error if `s` does not equal the string representation of a [`MetadataFileName`]
55 /// variant.
56 ///
57 /// # Examples
58 ///
59 /// ```
60 /// use std::str::FromStr;
61 ///
62 /// use alpm_types::MetadataFileName;
63 ///
64 /// # fn main() -> Result<(), alpm_types::Error> {
65 /// assert_eq!(
66 /// MetadataFileName::BuildInfo,
67 /// MetadataFileName::from_str(".BUILDINFO")?
68 /// );
69 /// assert_eq!(
70 /// MetadataFileName::Mtree,
71 /// MetadataFileName::from_str(".MTREE")?
72 /// );
73 /// assert_eq!(
74 /// MetadataFileName::PackageInfo,
75 /// MetadataFileName::from_str(".PKGINFO")?
76 /// );
77 /// assert!(MetadataFileName::from_str(".WRONG").is_err());
78 /// # Ok(())
79 /// # }
80 /// ```
81 fn from_str(s: &str) -> Result<Self, Self::Err> {
82 Ok(match s {
83 ".BUILDINFO" => Self::BuildInfo,
84 ".MTREE" => Self::Mtree,
85 ".PKGINFO" => Self::PackageInfo,
86 _ => {
87 return Err(crate::PackageError::InvalidMetadataFilename {
88 name: s.to_string(),
89 }
90 .into());
91 }
92 })
93 }
94}
95
96impl From<MetadataFileName> for String {
97 /// Creates a [`String`] from [`MetadataFileName`].
98 ///
99 /// # Examples
100 ///
101 /// ```
102 /// use alpm_types::MetadataFileName;
103 ///
104 /// # fn main() -> Result<(), alpm_types::Error> {
105 /// assert_eq!(MetadataFileName::BuildInfo.to_string(), ".BUILDINFO");
106 /// assert_eq!(MetadataFileName::Mtree.to_string(), ".MTREE");
107 /// assert_eq!(MetadataFileName::PackageInfo.to_string(), ".PKGINFO");
108 /// # Ok(())
109 /// # }
110 /// ```
111 fn from(value: MetadataFileName) -> Self {
112 value.to_string()
113 }
114}
115
116impl TryFrom<String> for MetadataFileName {
117 type Error = crate::Error;
118
119 /// Creates a [`MetadataFileName`] from [`String`].
120 ///
121 /// # Errors
122 ///
123 /// Returns an error if `s` does not equal the string representation of a [`MetadataFileName`]
124 /// variant.
125 ///
126 /// # Examples
127 ///
128 /// ```
129 /// use alpm_types::MetadataFileName;
130 ///
131 /// # fn main() -> Result<(), alpm_types::Error> {
132 /// assert_eq!(
133 /// MetadataFileName::BuildInfo,
134 /// MetadataFileName::try_from(".BUILDINFO".to_string())?
135 /// );
136 /// assert_eq!(
137 /// MetadataFileName::Mtree,
138 /// MetadataFileName::try_from(".MTREE".to_string())?
139 /// );
140 /// assert_eq!(
141 /// MetadataFileName::PackageInfo,
142 /// MetadataFileName::try_from(".PKGINFO".to_string())?
143 /// );
144 /// assert!(MetadataFileName::try_from(".WRONG".to_string()).is_err());
145 /// # Ok(())
146 /// # }
147 /// ```
148 fn try_from(value: String) -> Result<Self, Self::Error> {
149 Self::from_str(&value)
150 }
151}