html5_picture/path.rs
1use std::path::{Path, PathBuf};
2
3/// Generates an output file name that is stored in the output working directory.
4/// ## Example
5///
6/// ```
7/// use {
8/// html5_picture::path::create_output_file_name,
9/// std::path::PathBuf,
10/// };
11///
12/// let base_dir = PathBuf::from("../assets");
13/// let input_file = PathBuf::from("../assets/some/picture.png");
14/// let output_file = create_output_file_name(&base_dir, &input_file).unwrap();
15/// assert_eq!(output_file.to_str().unwrap(), "../assets-html5picture/some/picture.png");
16/// ```
17pub fn create_output_file_name(
18 base_dir: &PathBuf,
19 input_file: &PathBuf,
20) -> Result<PathBuf, String> {
21 let output_base_dir = get_output_working_dir(&base_dir)?;
22 let relative_file_name = remove_base_dir(&base_dir, &input_file)?;
23 Ok(output_base_dir.join(relative_file_name))
24}
25
26/// Calculates the relative filename according to `base_dir` and `input_file`
27/// and joins it with the `output_dir`.
28/// ## Example
29///
30/// ```
31/// use {
32/// html5_picture::path::create_output_file_name_with_output_dir,
33/// std::path::PathBuf,
34/// };
35///
36/// let output_dir = PathBuf::from("../new-assets");
37/// let base_dir = PathBuf::from("../assets");
38/// let input_file = PathBuf::from("../assets/some/picture.png");
39/// let output_file = create_output_file_name_with_output_dir(&output_dir, &base_dir, &input_file).unwrap();
40/// assert_eq!(output_file.to_str().unwrap(), "../new-assets/some/picture.png");
41/// ```
42pub fn create_output_file_name_with_output_dir(
43 output_dir: &PathBuf,
44 base_dir: &PathBuf,
45 input_file: &PathBuf,
46) -> Result<PathBuf, String> {
47 let relative_file_name = remove_base_dir(&base_dir, &input_file)?;
48 Ok(output_dir.join(relative_file_name))
49}
50
51/// Generates the output directory name by attaching a fixed string to it.
52/// ## Example
53///
54/// ```
55/// use {
56/// html5_picture::path::get_output_working_dir,
57/// std::path::PathBuf,
58/// };
59///
60/// let input = PathBuf::from("../assets");
61/// let input = get_output_working_dir(&input).unwrap();
62/// assert_eq!(input.to_str().unwrap(), "../assets-html5picture");
63/// ```
64pub fn get_output_working_dir(input_dir: &PathBuf) -> Result<PathBuf, String> {
65 if let None = &input_dir.file_name() {
66 return Err(String::from(
67 "The last segment of the input path is not valid!",
68 ));
69 };
70 // get input directory name
71 let input_dir_name = input_dir.file_name().unwrap();
72 // get parent directiory
73 let parent = match input_dir.parent() {
74 Some(p) => p,
75 None => Path::new(""),
76 };
77 // generate output directory
78 Ok(parent
79 .join(format!("{}-html5picture", input_dir_name.to_str().unwrap())))
80}
81
82/// Removes the given base directory from the given input file.
83/// ## Example
84///
85/// ```
86/// use {
87/// html5_picture::path::remove_base_dir,
88/// std::path::PathBuf,
89/// };
90///
91/// let base_dir = PathBuf::from("../assets");
92/// let input_file = PathBuf::from("../assets/some_other/directory/picture.png");
93/// let output = remove_base_dir(&base_dir, &input_file).unwrap();
94/// assert_eq!(output.to_str().unwrap(), "some_other/directory/picture.png");
95/// ```
96pub fn remove_base_dir(
97 base_dir: &PathBuf,
98 input_file: &PathBuf,
99) -> Result<PathBuf, String> {
100 match pathdiff::diff_paths(input_file, base_dir) {
101 Some(p) => Ok(p),
102 None => match input_file.to_str() {
103 Some(v) => {
104 Err(String::from(format!("Could not remove base dir of {}", v)))
105 }
106 None => Err(String::from("Could not remove base dir!")),
107 },
108 }
109}