alpm_db/desc/
schema.rs

1//! Schema definition for the [alpm-db-desc] file format.
2//!
3//! [alpm-db-desc]: https://alpm.archlinux.page/specifications/alpm-db-desc.5.html
4
5use std::{
6    fmt::{Display, Formatter},
7    fs::File,
8    path::{Path, PathBuf},
9    str::FromStr,
10};
11
12use alpm_common::FileFormatSchema;
13use alpm_types::{SchemaVersion, semver_version::Version};
14use fluent_i18n::t;
15
16use crate::Error;
17
18/// An enum describing all valid [alpm-db-desc] schemas.
19///
20/// Each variant corresponds to a specific revision of the
21/// specification.
22///
23/// [alpm-db-desc]: https://alpm.archlinux.page/specifications/alpm-db-desc.5.html
24#[derive(Clone, Debug, Eq, PartialEq)]
25pub enum DbDescSchema {
26    /// Schema for the [alpm-db-descv1] file format.
27    ///
28    /// [alpm-db-descv1]: https://alpm.archlinux.page/specifications/alpm-db-descv1.5.html
29    V1(SchemaVersion),
30    /// Schema for the [alpm-db-descv2] file format.
31    ///
32    /// [alpm-db-descv2]: https://alpm.archlinux.page/specifications/alpm-db-descv2.5.html
33    V2(SchemaVersion),
34}
35
36impl FileFormatSchema for DbDescSchema {
37    type Err = Error;
38
39    /// Returns a reference to the inner [`SchemaVersion`].
40    fn inner(&self) -> &SchemaVersion {
41        match self {
42            DbDescSchema::V1(v) => v,
43            DbDescSchema::V2(v) => v,
44        }
45    }
46
47    /// Derives a [`DbDescSchema`] from an [alpm-db-desc] file on disk.
48    ///
49    /// Opens the `file` and defers to [`DbDescSchema::derive_from_reader`].
50    ///
51    /// # Errors
52    ///
53    /// Returns an error if:
54    ///
55    /// - the file cannot be opened for reading,
56    /// - or deriving a [`DbDescSchema`] from its contents fails.
57    ///
58    /// [alpm-db-desc]: https://alpm.archlinux.page/specifications/alpm-db-desc.5.html
59    fn derive_from_file(file: impl AsRef<Path>) -> Result<Self, Error>
60    where
61        Self: Sized,
62    {
63        let file = file.as_ref();
64        Self::derive_from_reader(File::open(file).map_err(|source| Error::IoPathError {
65            path: PathBuf::from(file),
66            context: t!("error-io-path-schema-file"),
67            source,
68        })?)
69    }
70
71    /// Derives a [`DbDescSchema`] from [alpm-db-desc] data in a reader.
72    ///
73    /// Reads the `reader` to a string and defers to [`DbDescSchema::derive_from_str`].
74    ///
75    /// # Errors
76    ///
77    /// Returns an error if:
78    ///
79    /// - reading from `reader` fails,
80    /// - or deriving a [`DbDescSchema`] from its contents fails.
81    ///
82    /// [alpm-db-desc]: https://alpm.archlinux.page/specifications/alpm-db-desc.5.html
83    fn derive_from_reader(reader: impl std::io::Read) -> Result<Self, Error>
84    where
85        Self: Sized,
86    {
87        let mut buf = String::new();
88        let mut reader = reader;
89        reader
90            .read_to_string(&mut buf)
91            .map_err(|source| Error::IoReadError {
92                context: t!("error-io-read-schema-data"),
93                source,
94            })?;
95        Self::derive_from_str(&buf)
96    }
97
98    /// Derives a [`DbDescSchema`] from a string slice containing [alpm-db-desc] data.
99    ///
100    /// The parser uses a simple heuristic:
101    ///
102    /// - v1 → no `%XDATA%` section present
103    /// - v2 → `%XDATA%` section present
104    ///
105    /// This approach avoids relying on explicit version metadata, as the DB desc
106    /// format itself is not self-describing.
107    ///
108    /// # Examples
109    ///
110    /// ```
111    /// use alpm_common::FileFormatSchema;
112    /// use alpm_db::desc::DbDescSchema;
113    /// use alpm_types::{SchemaVersion, semver_version::Version};
114    ///
115    /// # fn main() -> Result<(), alpm_db::Error> {
116    /// let v1_data = r#"%NAME%
117    /// foo
118    ///
119    /// %VERSION%
120    /// 1.0.0-1
121    /// "#;
122    ///
123    /// assert_eq!(
124    ///     DbDescSchema::V1(SchemaVersion::new(Version::new(1, 0, 0))),
125    ///     DbDescSchema::derive_from_str(v1_data)?
126    /// );
127    ///
128    /// let v2_data = r#"%NAME%
129    /// foo
130    ///
131    /// %VERSION%
132    /// 1.0.0-1
133    ///
134    /// %XDATA%
135    /// pkgtype=pkg
136    /// "#;
137    ///
138    /// assert_eq!(
139    ///     DbDescSchema::V2(SchemaVersion::new(Version::new(2, 0, 0))),
140    ///     DbDescSchema::derive_from_str(v2_data)?
141    /// );
142    /// # Ok(())
143    /// # }
144    /// ```
145    ///
146    /// # Errors
147    ///
148    /// Returns an error only if internal conversion or string handling fails.
149    ///
150    /// [alpm-db-desc]: https://alpm.archlinux.page/specifications/alpm-db-desc.5.html
151    fn derive_from_str(s: &str) -> Result<DbDescSchema, Error> {
152        // Instead of an explicit "format" key, we use a heuristic:
153        // presence of `%XDATA%` implies version 2.
154        if s.contains("%XDATA%") {
155            Ok(DbDescSchema::V2(SchemaVersion::new(Version::new(2, 0, 0))))
156        } else {
157            Ok(DbDescSchema::V1(SchemaVersion::new(Version::new(1, 0, 0))))
158        }
159    }
160}
161
162impl Default for DbDescSchema {
163    /// Returns the default schema variant ([`DbDescSchema::V2`]).
164    fn default() -> Self {
165        Self::V2(SchemaVersion::new(Version::new(2, 0, 0)))
166    }
167}
168
169impl FromStr for DbDescSchema {
170    type Err = Error;
171
172    /// Parses a [`DbDescSchema`] from a version string.
173    ///
174    /// # Errors
175    ///
176    /// Returns an error if:
177    ///
178    /// - the input string is not a valid version,
179    /// - or the version does not correspond to a known schema variant.
180    fn from_str(s: &str) -> Result<DbDescSchema, Self::Err> {
181        match SchemaVersion::from_str(s) {
182            Ok(version) => Self::try_from(version),
183            Err(_) => Err(Error::UnsupportedSchemaVersion(s.to_string())),
184        }
185    }
186}
187
188impl TryFrom<SchemaVersion> for DbDescSchema {
189    type Error = Error;
190
191    /// Converts a [`SchemaVersion`] into a corresponding [`DbDescSchema`].
192    ///
193    /// # Errors
194    ///
195    /// Returns an error if the major version of `SchemaVersion` does not
196    /// correspond to a known [`DbDescSchema`] variant.
197    fn try_from(value: SchemaVersion) -> Result<Self, Self::Error> {
198        match value.inner().major {
199            1 => Ok(DbDescSchema::V1(value)),
200            2 => Ok(DbDescSchema::V2(value)),
201            _ => Err(Error::UnsupportedSchemaVersion(value.to_string())),
202        }
203    }
204}
205
206impl Display for DbDescSchema {
207    fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
208        write!(
209            fmt,
210            "{}",
211            match self {
212                DbDescSchema::V1(version) | DbDescSchema::V2(version) => version.inner().major,
213            }
214        )
215    }
216}