lit_mod/
lib.rs

1//! lit-mod is a collection of procedural macros for working with string literals.
2//!
3//! ## Usage
4//!
5//! Add this to your `Cargo.toml`:
6//!
7//! ```toml
8//! [dependencies]
9//! lit-mod = "0.1"
10//! ```
11
12mod lines;
13mod remove_lines;
14mod replace;
15mod slice;
16mod util;
17
18use proc_macro::TokenStream;
19
20/// Replaces all matches of a pattern with another string in a string literal.
21///
22/// Alternatively the third argument can be ignored and the macro will remove all matches of the
23/// pattern instead.
24///
25/// # Examples
26///
27/// Basic usage:
28///
29/// ```
30/// use lit_mod::replace;
31///
32/// assert_eq!("this is new", replace!("this is old", "old", "new"));
33/// assert_eq!("than an old", replace!("this is old", "is", "an"));
34/// assert_eq!("this is", replace!("this is old", " old"));
35/// ```
36///
37/// When the pattern doesn't match:
38///
39/// ```
40/// use lit_mod::replace;
41///
42/// assert_eq!(
43///     "this is old",
44///     replace!("this is old", "cookie monster", "little lamb")
45/// );
46/// ```
47#[proc_macro]
48pub fn replace(input: TokenStream) -> TokenStream {
49    replace::replace(input)
50}
51
52/// Replaces the literal with a slice of the literal.
53///
54/// *Note:* This macro doesn't slice by bytes, but by characters.
55///
56/// # Examples
57///
58/// Basic usage:
59///
60/// ```
61/// use lit_mod::slice;
62///
63/// assert_eq!("world!", slice!("Hello, world!", 7..));
64/// assert_eq!("Hello", slice!("Hello, world!", ..-8));
65/// ```
66#[proc_macro]
67pub fn slice(input: TokenStream) -> TokenStream {
68    slice::slice(input)
69}
70
71/// Replaces the literal with a literal of the lines.
72///
73/// # Examples
74///
75/// Basic usage:
76///
77/// ```
78/// use lit_mod::lines;
79///
80/// assert_eq!(
81///     "Hello, world!",
82///     lines!("Hello, world!\nThis is lines", 0..1)
83/// );
84/// assert_eq!(
85///     "This is lines",
86///     lines!("Hello, world!\nThis is lines", -1..)
87/// );
88/// ```
89#[proc_macro]
90pub fn lines(input: TokenStream) -> TokenStream {
91    lines::lines(input)
92}
93
94/// Replaces the literal with a literal without the lines.
95///
96/// # Examples
97///
98/// Basic usage:
99///
100/// ```
101/// use lit_mod::remove_lines;
102///
103/// assert_eq!(
104///     "Hello, world!",
105///     remove_lines!("Hello, world!\nThis is lines", -1..)
106/// );
107/// assert_eq!(
108///     "This is lines",
109///     remove_lines!("Hello, world!\nThis is lines", ..1)
110/// );
111/// ```
112#[proc_macro]
113pub fn remove_lines(input: TokenStream) -> TokenStream {
114    remove_lines::remove_lines(input)
115}