Skip to main content

r_metadata/
scalar.rs

1//! Small finite metadata values.
2
3use std::str::FromStr;
4
5/// An R logical value accepted by DESCRIPTION fields.
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
7pub struct Logical(bool);
8impl Logical {
9    /// Returns the represented boolean.
10    pub const fn get(self) -> bool {
11        self.0
12    }
13}
14impl From<Logical> for bool {
15    fn from(value: Logical) -> Self {
16        value.0
17    }
18}
19impl From<bool> for Logical {
20    fn from(value: bool) -> Self {
21        Self(value)
22    }
23}
24impl std::fmt::Display for Logical {
25    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26        f.write_str(if self.0 { "yes" } else { "no" })
27    }
28}
29impl FromStr for Logical {
30    type Err = LogicalParseError;
31    fn from_str(value: &str) -> Result<Self, Self::Err> {
32        if value.eq_ignore_ascii_case("yes") || value.eq_ignore_ascii_case("true") {
33            Ok(Self(true))
34        } else if value.eq_ignore_ascii_case("no") || value.eq_ignore_ascii_case("false") {
35            Ok(Self(false))
36        } else {
37            Err(LogicalParseError)
38        }
39    }
40}
41/// Error parsing a logical value.
42#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
43#[error("expected yes, true, no, or false")]
44pub struct LogicalParseError;
45
46/// Operating-system restriction from `OS_type`.
47#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
48pub enum OsType {
49    /// Unix-like systems.
50    Unix,
51    /// Microsoft Windows.
52    Windows,
53}
54impl std::fmt::Display for OsType {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        f.write_str(match self {
57            Self::Unix => "unix",
58            Self::Windows => "windows",
59        })
60    }
61}
62impl FromStr for OsType {
63    type Err = OsTypeParseError;
64    fn from_str(value: &str) -> Result<Self, Self::Err> {
65        match value {
66            "unix" => Ok(Self::Unix),
67            "windows" => Ok(Self::Windows),
68            _ => Err(OsTypeParseError),
69        }
70    }
71}
72/// Error parsing `OS_type`.
73#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
74#[error("expected unix or windows")]
75pub struct OsTypeParseError;
76
77/// Standard R package priority.
78#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
79pub enum Priority {
80    /// Base R package.
81    Base,
82    /// Recommended package.
83    Recommended,
84    /// Ordinary optional package, used by historical repository metadata.
85    Optional,
86    /// Contributed package, used by historical repository metadata.
87    Contrib,
88}
89impl std::fmt::Display for Priority {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        f.write_str(match self {
92            Self::Base => "base",
93            Self::Recommended => "recommended",
94            Self::Optional => "optional",
95            Self::Contrib => "contrib",
96        })
97    }
98}
99impl FromStr for Priority {
100    type Err = PriorityParseError;
101    fn from_str(value: &str) -> Result<Self, Self::Err> {
102        match value {
103            "base" => Ok(Self::Base),
104            "recommended" => Ok(Self::Recommended),
105            "optional" => Ok(Self::Optional),
106            "contrib" => Ok(Self::Contrib),
107            _ => Err(PriorityParseError),
108        }
109    }
110}
111/// Error parsing package priority.
112#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
113#[error("expected base, recommended, optional, or contrib")]
114pub struct PriorityParseError;