1#![cfg_attr(not(target_os = "windows"), allow(unused))]
2
3use proc_macro::TokenStream;
4use quote::quote;
5use syn::{LitStr, parse_macro_input};
6
7/// This macro replaces the path separator `/` with `\` for Windows.
8/// But if the target OS is not Windows, the path is returned as is.
9///
10/// # Example
11/// ```rust
12/// # use util_macros::separator;
13/// let path = separator!("path/to/file");
14/// #[cfg(target_os = "windows")]
15/// assert_eq!(path, "path\\to\\file");
16/// #[cfg(not(target_os = "windows"))]
17/// assert_eq!(path, "path/to/file");
18/// ```
19#[proc_macro]
20pub fn separator(input: TokenStream) -> TokenStream {
21 let path = parse_macro_input!(input as LitStr);
22 let path = path.value();
23
24 #[cfg(target_os = "windows")]
25 let path = path.replace("/", "\\");
26
27 TokenStream::from(quote! {
28 #path
29 })
30}
31
32/// This macro replaces the path prefix `file:///` with `file:///C:/` for Windows.
33/// But if the target OS is not Windows, the URI is returned as is.
34///
35/// # Example
36/// ```rust
37/// use util_macros::uri;
38///
39/// let uri = uri!("file:///path/to/file");
40/// #[cfg(target_os = "windows")]
41/// assert_eq!(uri, "file:///C:/path/to/file");
42/// #[cfg(not(target_os = "windows"))]
43/// assert_eq!(uri, "file:///path/to/file");
44/// ```
45#[proc_macro]
46pub fn uri(input: TokenStream) -> TokenStream {
47 let uri = parse_macro_input!(input as LitStr);
48 let uri = uri.value();
49
50 #[cfg(target_os = "windows")]
51 let uri = uri.replace("file:///", "file:///C:/");
52
53 TokenStream::from(quote! {
54 #uri
55 })
56}
57
58/// This macro replaces the line endings `\n` with `\r\n` for Windows.
59/// But if the target OS is not Windows, the line endings are returned as is.
60///
61/// # Example
62/// ```rust
63/// use util_macros::line_endings;
64///
65/// let text = line_endings!("Hello\nWorld");
66/// #[cfg(target_os = "windows")]
67/// assert_eq!(text, "Hello\r\nWorld");
68/// #[cfg(not(target_os = "windows"))]
69/// assert_eq!(text, "Hello\nWorld");
70/// ```
71#[proc_macro]
72pub fn line_endings(input: TokenStream) -> TokenStream {
73 let text = parse_macro_input!(input as LitStr);
74 let text = text.value();
75
76 #[cfg(target_os = "windows")]
77 let text = text.replace("\n", "\r\n");
78
79 TokenStream::from(quote! {
80 #text
81 })
82}