pub struct FilesV1(Vec<PathBuf>);Expand description
The representation of alpm-files data (version 1).
Tuple Fields§
§0: Vec<PathBuf>Trait Implementations§
Source§impl FilesStyleToString for FilesV1
impl FilesStyleToString for FilesV1
Source§fn to_string(&self, style: FilesStyle) -> String
fn to_string(&self, style: FilesStyle) -> String
Returns the String representation of the FilesV1.
The formatting of the returned string depends on the provided FilesStyle.
§Examples
use std::path::PathBuf;
use alpm_files::{FilesStyle, FilesStyleToString, FilesV1};
// An empty alpm-db-files.
let expected = "";
let files = FilesV1::try_from(Vec::new())?;
assert_eq!(files.to_string(FilesStyle::Db), expected);
// An alpm-db-files with entries.
let expected = r#"%FILES%
usr/
usr/bin/
usr/bin/foo
"#;
let files = FilesV1::try_from(vec![
PathBuf::from("usr/"),
PathBuf::from("usr/bin/"),
PathBuf::from("usr/bin/foo"),
])?;
assert_eq!(files.to_string(FilesStyle::Db), expected);
// An empty alpm-repo-files.
let expected = "%FILES%\n";
let files = FilesV1::try_from(Vec::new())?;
assert_eq!(files.to_string(FilesStyle::Repo), expected);
// An alpm-repo-files with entries.
let expected = r#"%FILES%
usr/
usr/bin/
usr/bin/foo
"#;
let files = FilesV1::try_from(vec![
PathBuf::from("usr/"),
PathBuf::from("usr/bin/"),
PathBuf::from("usr/bin/foo"),
])?;
assert_eq!(files.to_string(FilesStyle::Repo), expected);Source§impl FromStr for FilesV1
impl FromStr for FilesV1
Source§fn from_str(s: &str) -> Result<Self, Self::Err>
fn from_str(s: &str) -> Result<Self, Self::Err>
Creates a new FilesV1 from a string slice.
§Note
Delegates to the TryFrom Vec of PathBuf implementation, after the string slice
has been parsed as a Vec of PathBuf.
§Errors
Returns an error, if
valueis not empty and the first line does not contain the section header (“%FILES%”),- there are lines following the section header, but they cannot be parsed as a
VecofPathBuf, - or
Self::try_fromVecofPathBuffails.
§Examples
use std::{path::PathBuf, str::FromStr};
use alpm_files::FilesV1;
use winnow::Parser;
// No files according to alpm-db-files.
let data = "";
let files = FilesV1::from_str(data)?;
// No files according to alpm-repo-files.
let data = "%FILES%";
let files = FilesV1::from_str(data)?;
let data = "%FILES%\n";
let files = FilesV1::from_str(data)?;
// Files according to alpm-repo-files.
let data = r#"%FILES%
usr/
usr/bin/
usr/bin/foo"#;
let files = FilesV1::from_str(data)?;
// Files according to alpm-db-files.
let data = r#"%FILES%
usr/
usr/bin/
usr/bin/foo
"#;
let files = FilesV1::from_str(data)?;Source§impl TryFrom<PathBuf> for FilesV1
impl TryFrom<PathBuf> for FilesV1
Source§fn try_from(value: PathBuf) -> Result<Self, Self::Error>
fn try_from(value: PathBuf) -> Result<Self, Self::Error>
Creates a new FilesV1 from all files and directories in a directory.
§Note
Delegates to alpm_common::relative_files to get a sorted list of all files and
directories in the directory value (relative to value).
Afterwards, tries to construct a FilesV1 from this list.
§Errors
Returns an error if
alpm_common::relative_filesfails,- or
TryFromVecofPathBufforFilesV1fails.
§Examples
use std::{
fs::{File, create_dir_all},
path::PathBuf,
};
use alpm_files::FilesV1;
use tempfile::tempdir;
let temp_dir = tempdir()?;
let path = temp_dir.path();
create_dir_all(path.join("usr/bin/"))?;
File::create(path.join("usr/bin/foo"))?;
let files = FilesV1::try_from(path.to_path_buf())?;
assert_eq!(
files.as_ref(),
vec![
PathBuf::from("usr/"),
PathBuf::from("usr/bin/"),
PathBuf::from("usr/bin/foo")
]
);Source§impl TryFrom<Vec<PathBuf>> for FilesV1
impl TryFrom<Vec<PathBuf>> for FilesV1
Source§fn try_from(value: Vec<PathBuf>) -> Result<Self, Self::Error>
fn try_from(value: Vec<PathBuf>) -> Result<Self, Self::Error>
Creates a new FilesV1 from a Vec of PathBuf.
The provided value is sorted and checked for non top-level paths without a parent, as well
as any duplicate paths.
§Errors
Returns an error if
valuecontains absolute paths,valuecontains (non top-level) paths without a parent directory present invalue,- or
valuecontains duplicate paths.
§Examples
use std::path::PathBuf;
use alpm_files::FilesV1;
let paths: Vec<PathBuf> = vec![
PathBuf::from("usr/"),
PathBuf::from("usr/bin/"),
PathBuf::from("usr/bin/foo"),
];
let files = FilesV1::try_from(paths)?;
// Absolute paths are not allowed.
let paths: Vec<PathBuf> = vec![
PathBuf::from("/usr/"),
PathBuf::from("/usr/bin/"),
PathBuf::from("/usr/bin/foo"),
];
assert!(FilesV1::try_from(paths).is_err());
// Every path (excluding top-level paths) must have a parent.
let paths: Vec<PathBuf> = vec![PathBuf::from("usr/bin/"), PathBuf::from("usr/bin/foo")];
assert!(FilesV1::try_from(paths).is_err());
// Every path must be unique.
let paths: Vec<PathBuf> = vec![
PathBuf::from("usr/"),
PathBuf::from("usr/"),
PathBuf::from("usr/bin/"),
PathBuf::from("usr/bin/foo"),
];
assert!(FilesV1::try_from(paths).is_err());