alpm_types/path.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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
use std::{
fmt::{Display, Formatter},
path::{Path, PathBuf},
str::FromStr,
};
use crate::Error;
/// A representation of an absolute path
///
/// AbsolutePath wraps a `PathBuf`, that is guaranteed to be absolute.
///
/// ## Examples
/// ```
/// use std::path::PathBuf;
/// use std::str::FromStr;
///
/// use alpm_types::{AbsolutePath, Error};
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// // Create AbsolutePath from &str
/// assert_eq!(
/// AbsolutePath::from_str("/"),
/// AbsolutePath::new(PathBuf::from("/"))
/// );
/// assert_eq!(
/// AbsolutePath::from_str("./"),
/// Err(Error::PathNotAbsolute(PathBuf::from("./")))
/// );
///
/// // Format as String
/// assert_eq!("/", format!("{}", AbsolutePath::from_str("/")?));
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AbsolutePath(PathBuf);
impl AbsolutePath {
/// Create a new `AbsolutePath`
pub fn new(path: PathBuf) -> Result<AbsolutePath, Error> {
match path.is_absolute() {
true => Ok(AbsolutePath(path)),
false => Err(Error::PathNotAbsolute(path)),
}
}
/// Return a reference to the inner type
pub fn inner(&self) -> &Path {
&self.0
}
}
impl FromStr for AbsolutePath {
type Err = Error;
/// Parses an absolute path from a string
///
/// ## Errors
///
/// Returns an error if the path is not absolute
fn from_str(s: &str) -> Result<AbsolutePath, Self::Err> {
match Path::new(s).is_absolute() {
true => Ok(AbsolutePath(PathBuf::from(s))),
false => Err(Error::PathNotAbsolute(PathBuf::from(s))),
}
}
}
impl Display for AbsolutePath {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(fmt, "{}", self.inner().display())
}
}
/// An absolute path used as build directory
///
/// This is a type alias for [`AbsolutePath`]
///
/// ## Examples
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::{Error, BuildDir};
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// // Create BuildDir from &str and format it
/// assert_eq!(
/// "/etc",
/// BuildDir::from_str("/etc")?.to_string()
/// );
/// # Ok(())
/// # }
pub type BuildDir = AbsolutePath;
/// An absolute path used as start directory in a package build environment
///
/// This is a type alias for [`AbsolutePath`]
///
/// ## Examples
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::{Error, StartDir};
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// // Create StartDir from &str and format it
/// assert_eq!(
/// "/etc",
/// StartDir::from_str("/etc")?.to_string()
/// );
/// # Ok(())
/// # }
pub type StartDir = AbsolutePath;
/// A representation of a relative file path
///
/// `RelativePath` wraps a `PathBuf` that is guaranteed to represent a
/// relative file path (i.e. it does not end with a `/`).
///
/// ## Examples
///
/// ```
/// use std::path::PathBuf;
/// use std::str::FromStr;
///
/// use alpm_types::{Error, RelativePath};
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// // Create RelativePath from &str
/// assert_eq!(
/// RelativePath::from_str("etc/test.conf"),
/// RelativePath::new(PathBuf::from("etc/test.conf"))
/// );
/// assert_eq!(
/// RelativePath::from_str("/etc/test.conf"),
/// Err(Error::PathNotRelative(PathBuf::from("/etc/test.conf")))
/// );
///
/// // Format as String
/// assert_eq!(
/// "test/test.txt",
/// RelativePath::from_str("test/test.txt")?.to_string()
/// );
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RelativePath(PathBuf);
impl RelativePath {
/// Create a new `RelativePath`
pub fn new(path: PathBuf) -> Result<RelativePath, Error> {
match path.is_relative()
&& !path
.to_string_lossy()
.ends_with(std::path::MAIN_SEPARATOR_STR)
{
true => Ok(RelativePath(path)),
false => Err(Error::PathNotRelative(path)),
}
}
/// Return a reference to the inner type
pub fn inner(&self) -> &Path {
&self.0
}
}
impl FromStr for RelativePath {
type Err = Error;
/// Parses a relative path from a string
///
/// ## Errors
///
/// Returns an error if the path is not relative
fn from_str(s: &str) -> Result<RelativePath, Self::Err> {
Self::new(PathBuf::from(s))
}
}
impl Display for RelativePath {
fn fmt(&self, fmt: &mut Formatter) -> std::fmt::Result {
write!(fmt, "{}", self.inner().display())
}
}
/// The path of a packaged file that should be preserved during package operations
///
/// This is a type alias for [`RelativePath`]
///
/// ## Examples
/// ```
/// use std::str::FromStr;
///
/// use alpm_types::Backup;
///
/// # fn main() -> Result<(), alpm_types::Error> {
/// // Create Backup from &str and format it
/// assert_eq!(
/// "etc/test.conf",
/// Backup::from_str("etc/test.conf")?.to_string()
/// );
/// # Ok(())
/// # }
pub type Backup = RelativePath;
#[cfg(test)]
mod tests {
use rstest::rstest;
use super::*;
#[rstest]
#[case("/home", BuildDir::new(PathBuf::from("/home")))]
#[case("./", Err(Error::PathNotAbsolute(PathBuf::from("./"))))]
#[case("~/", Err(Error::PathNotAbsolute(PathBuf::from("~/"))))]
#[case("foo.txt", Err(Error::PathNotAbsolute(PathBuf::from("foo.txt"))))]
fn build_dir_from_string(#[case] s: &str, #[case] result: Result<BuildDir, Error>) {
assert_eq!(BuildDir::from_str(s), result);
}
#[rstest]
#[case("/start", StartDir::new(PathBuf::from("/start")))]
#[case("./", Err(Error::PathNotAbsolute(PathBuf::from("./"))))]
#[case("~/", Err(Error::PathNotAbsolute(PathBuf::from("~/"))))]
#[case("foo.txt", Err(Error::PathNotAbsolute(PathBuf::from("foo.txt"))))]
fn startdir_from_str(#[case] s: &str, #[case] result: Result<StartDir, Error>) {
assert_eq!(StartDir::from_str(s), result);
}
#[rstest]
#[case("etc/test.conf", RelativePath::new(PathBuf::from("etc/test.conf")))]
#[case(
"/etc/test.conf",
Err(Error::PathNotRelative(PathBuf::from("/etc/test.conf")))
)]
#[case("etc/", Err(Error::PathNotRelative(PathBuf::from("etc/"))))]
#[case("etc", RelativePath::new(PathBuf::from("etc")))]
#[case(
"../etc/test.conf",
RelativePath::new(PathBuf::from("../etc/test.conf"))
)]
fn relative_path_from_str(#[case] s: &str, #[case] result: Result<RelativePath, Error>) {
assert_eq!(RelativePath::from_str(s), result);
}
}