action_core/
utils.rs

1use std::ffi::OsStr;
2
3/// `toPosixPath` converts the given path to the posix form.
4///
5/// On Windows, \\ will be replaced with /.
6pub fn to_posix_path(path: impl AsRef<str>) -> String {
7    path.as_ref().replace('\\', "/")
8}
9
10/// `toWin32Path` converts the given path to the win32 form.
11///
12/// On Linux, / will be replaced with \\.
13pub fn to_win32_path(path: impl AsRef<str>) -> String {
14    path.as_ref().replace('/', "\\")
15}
16
17/// `toPlatformPath` converts the given path to a platform-specific path.
18///
19/// It does this by replacing instances of / and \ with
20/// the platform-specific path separator.
21pub fn to_platform_path(path: impl AsRef<str>) -> String {
22    path.as_ref()
23        .replace(['/', '\\'], std::path::MAIN_SEPARATOR_STR)
24}
25
26pub fn escape_data(data: impl AsRef<str>) -> String {
27    data.as_ref()
28        .replace('%', "%25")
29        .replace('\r', "%0D")
30        .replace('\n', "%0A")
31}
32
33pub fn escape_property(prop: impl AsRef<str>) -> String {
34    prop.as_ref()
35        .replace('%', "%25")
36        .replace('\r', "%0D")
37        .replace('\n', "%0A")
38        .replace(':', "%3A")
39        .replace(',', "%2C")
40}
41
42/// Filters empty values.
43///
44/// # Errors
45/// If the value is empty.
46pub fn not_empty<T>(value: T) -> Option<T>
47where
48    T: AsRef<OsStr>,
49{
50    if value.as_ref().is_empty() {
51        None
52    } else {
53        Some(value)
54    }
55}