Skip to main content

alpm_common/traits/
schema.rs

1//! Traits for schemas.
2
3#[cfg(doc)]
4use std::str::FromStr;
5use std::{io::Read, path::Path};
6
7use alpm_types::SchemaVersion;
8
9/// A trait for file format schemas and their versioning.
10///
11/// File formats are expected to either expose the schema version directly, or at least make it
12/// possible to derive the version from them.
13pub trait FileFormatSchema: AsRef<SchemaVersion> {
14    /// The Error type to use.
15    type Err;
16
17    /// Derives [`Self`] from a `file`.
18    ///
19    /// # Note
20    ///
21    /// This function is meant for implementers to _derive_ [`Self`] based on the properties of the
22    /// `file` contents (e.g. a context-specific version identifier, etc.).
23    /// Instead of creating [`Self`] from considering all of `file`, this function is only used to
24    /// introspect `file` and to retrieve required information to _derive_ which variant of
25    /// [`Self`] to create or whether to fail.
26    fn derive_from_file(file: impl AsRef<Path>) -> Result<Self, Self::Err>
27    where
28        Self: Sized;
29
30    /// Derives [`Self`] from a [`Read`] implementer.
31    ///
32    /// # Note
33    ///
34    /// This function is meant for implementers to _derive_ [`Self`] based on the properties of the
35    /// `reader` contents (e.g. a context-specific version identifier, etc.).
36    /// Instead of creating [`Self`] from considering all of `reader`, this function is only used to
37    /// introspect `reader` and to retrieve required information to _derive_ which variant of
38    /// [`Self`] to create or whether to fail.
39    fn derive_from_reader(reader: impl Read) -> Result<Self, Self::Err>
40    where
41        Self: Sized;
42
43    /// Derives [`Self`] from a string slice `s`.
44    ///
45    /// # Note
46    ///
47    /// This function is meant for implementers to _derive_ [`Self`] based on the
48    /// properties of `s` (e.g. a context-specific version identifier, etc.).
49    /// Instead of creating [`Self`] from considering all of `s`, this function is only used to
50    /// introspect `s` and to retrieve required information to _derive_ which variant of [`Self`] to
51    /// create or whether to fail.
52    ///
53    /// For _creation_ from the entire string slice the [`FromStr`] trait should be implemented
54    /// instead.
55    fn derive_from_str(s: &str) -> Result<Self, Self::Err>
56    where
57        Self: Sized;
58}