quickdash/utilities.rs
1/* Copyright [2021] [Cerda]
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16//! Module containing various utility functions
17
18use std::path::Path;
19
20/// Merges two `Vec`s.
21///
22/// # Examples
23///
24/// ```
25/// let vec1 = vec![0];
26/// let vec2 = vec![1];
27///
28/// assert_eq!(quickdash::utilities::vec_merge(vec1, vec2), vec![0, 1]);
29/// ```
30pub fn vec_merge<T>(mut lhs: Vec<T>, rhs: Vec<T>) -> Vec<T> {
31 lhs.extend(rhs);
32 lhs
33}
34
35/// Create a string consisting of `n` repetitions of `what`.
36///
37/// # Examples
38///
39/// ```
40/// assert_eq!(
41/// quickdash::utilities::mul_str("LOL! ", 3),
42/// "LOL! LOL! LOL! ".to_string()
43/// );
44/// ```
45pub fn mul_str(what: &str, n: usize) -> String {
46 what.repeat(n)
47}
48
49/// Create a user-usable path to `what` from `prefix`.
50///
51/// # Examples
52///
53/// ```
54/// # use std::path::Path;
55/// assert_eq!(
56/// quickdash::utilities::relative_name(Path::new("/usr"), Path::new("/usr/bin/quickdash")),
57/// "bin/quickdash".to_string()
58/// );
59/// ```
60pub fn relative_name(prefix: &Path, what: &Path) -> String {
61 what.strip_prefix(prefix)
62 .unwrap()
63 .to_str()
64 .unwrap()
65 .replace('\\', "/")
66}