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