alpm_types/name.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
use std::{
fmt::{Display, Formatter},
str::FromStr,
string::ToString,
};
use lazy_regex::{lazy_regex, Lazy};
use regex::Regex;
use crate::Error;
pub(crate) static NAME_REGEX: Lazy<Regex> = lazy_regex!(r"^[a-z\d_@+]+[a-z\d\-._@+]*$");
/// A build tool name
///
/// The same character restrictions as with `Name` apply.
/// Further name restrictions may be enforced on an existing instances using
/// `matches_restriction()`.
///
/// ## Examples
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::{BuildTool, Error, Name};
///
/// // create BuildTool from &str
/// assert!(BuildTool::from_str("test-123@.foo_+").is_ok());
/// assert!(BuildTool::from_str(".test").is_err());
///
/// // format as String
/// assert_eq!("foo", format!("{}", BuildTool::from_str("foo").unwrap()));
///
/// // validate that BuildTool follows naming restrictions
/// let buildtool = BuildTool::from_str("foo").unwrap();
/// let restrictions = vec![
/// Name::from_str("foo").unwrap(),
/// Name::from_str("bar").unwrap(),
/// ];
/// assert!(buildtool.matches_restriction(&restrictions));
/// ```
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct BuildTool(Name);
impl BuildTool {
/// Create a new BuildTool
pub fn new(name: Name) -> Self {
BuildTool(name)
}
/// Create a new BuildTool in a Result, which matches one Name in a list of restrictions
///
/// ## Examples
/// ```
/// use alpm_types::{BuildTool, Error, Name};
///
/// assert!(
/// BuildTool::new_with_restriction("foo", &[Name::new("foo".to_string()).unwrap()]).is_ok()
/// );
/// assert!(
/// BuildTool::new_with_restriction("foo", &[Name::new("bar".to_string()).unwrap()]).is_err()
/// );
/// ```
pub fn new_with_restriction(name: &str, restrictions: &[Name]) -> Result<Self, Error> {
let buildtool = BuildTool::from_str(name)?;
if buildtool.matches_restriction(restrictions) {
Ok(buildtool)
} else {
Err(Error::ValueDoesNotMatchRestrictions {
restrictions: restrictions.iter().map(ToString::to_string).collect(),
})
}
}
/// Validate that the BuildTool has a name matching one Name in a list of restrictions
pub fn matches_restriction(&self, restrictions: &[Name]) -> bool {
restrictions
.iter()
.any(|restriction| restriction.eq(self.inner()))
}
/// Return a reference to the inner type
pub fn inner(&self) -> &Name {
&self.0
}
}
impl FromStr for BuildTool {
type Err = Error;
/// Create a BuildTool from a string
fn from_str(s: &str) -> Result<BuildTool, Self::Err> {
Name::new(s.to_string()).map(BuildTool)
}
}
impl Display for BuildTool {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(fmt, "{}", self.inner())
}
}
/// A package name
///
/// Package names may contain the characters `[a-z\d\-._@+]`, but must not
/// start with `[-.]`.
///
/// ## Examples
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::{Error, Name};
///
/// // create Name from &str
/// assert_eq!(
/// Name::from_str("test-123@.foo_+"),
/// Ok(Name::new("test-123@.foo_+".to_string()).unwrap())
/// );
/// assert!(Name::from_str(".test").is_err());
///
/// // format as String
/// assert_eq!("foo", format!("{}", Name::new("foo".to_string()).unwrap()));
/// ```
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Name(String);
impl Name {
/// Create a new `Name`
pub fn new(name: String) -> Result<Self, Error> {
Self::from_str(&name)
}
/// Return a reference to the inner type
pub fn inner(&self) -> &str {
&self.0
}
}
impl FromStr for Name {
type Err = Error;
/// Create a Name from a string
fn from_str(s: &str) -> Result<Name, Self::Err> {
if NAME_REGEX.is_match(s) {
Ok(Name(s.to_string()))
} else {
Err(Error::RegexDoesNotMatch {
value: s.to_string(),
regex_type: "pkgname".to_string(),
regex: NAME_REGEX.to_string(),
})
}
}
}
impl Display for Name {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(fmt, "{}", self.inner())
}
}
impl AsRef<str> for Name {
fn as_ref(&self) -> &str {
self.inner()
}
}
#[cfg(test)]
mod tests {
use proptest::prelude::*;
use rstest::rstest;
use super::*;
#[rstest]
#[case(
"bar",
["foo".parse(), "bar".parse()].into_iter().flatten().collect::<Vec<Name>>(),
Ok(BuildTool::from_str("bar").unwrap()),
)]
#[case(
"bar",
["foo".parse(), "foo".parse()].into_iter().flatten().collect::<Vec<Name>>(),
Err(Error::ValueDoesNotMatchRestrictions {
restrictions: vec!["foo".to_string(), "foo".to_string()],
}),
)]
fn buildtool_new_with_restriction(
#[case] buildtool: &str,
#[case] restrictions: Vec<Name>,
#[case] result: Result<BuildTool, Error>,
) {
assert_eq!(
BuildTool::new_with_restriction(buildtool, &restrictions),
result
);
}
#[rstest]
#[case("bar", ["foo".parse(), "bar".parse()].into_iter().flatten().collect::<Vec<Name>>(), true)]
#[case("bar", ["foo".parse(), "foo".parse()].into_iter().flatten().collect::<Vec<Name>>(), false)]
fn buildtool_matches_restriction(
#[case] buildtool: &str,
#[case] restrictions: Vec<Name>,
#[case] result: bool,
) {
let buildtool = BuildTool::from_str(buildtool).unwrap();
assert_eq!(buildtool.matches_restriction(&restrictions), result);
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(1000))]
#[test]
fn valid_name_from_string(name_str in r"[a-z\d_@+]+[a-z\d\-._@+]*") {
let name = Name::from_str(&name_str).unwrap();
prop_assert_eq!(name_str, format!("{}", name));
}
#[test]
fn invalid_name_from_string_start(name_str in r"[\-.]+[a-z\d\-._@+]*") {
let error = Name::from_str(&name_str).unwrap_err();
assert_eq!(error, Error::RegexDoesNotMatch {
value: name_str.to_string(),
regex_type: "pkgname".to_string(),
regex: NAME_REGEX.to_string(),
});
}
}
}