1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#![doc(html_root_url = "https://docs.rs/fixed_len_str/0.1.2/")]
#![no_std]

//! This crate provides a procedural macro for declare a wrapper struct for an array with the size
//! given by the tokens which dereferences to [`str`] and a macro for create they from [`str`].
//! 
//! [`str`]: https://doc.rust-lang.org/std/primitive.str.html

extern crate proc_macro;
extern crate alloc;

use proc_macro::TokenStream;

use alloc::string::ToString;

/// A macro for declare an FixedStr**input** struct.
/// 
/// # Examples
/// 
/// ```
/// use fixed_len_str::fixed_len_str;
/// 
/// fixed_len_str!(3);
/// 
/// fn main() {
///     let string = new_str3!("abc");
/// 
///     assert_eq!(string.as_ref(), "abc");
/// 
///     let string = FixedStr3::new(*b"abc");
/// 
///     assert_eq!(string.as_ref(), "abc");
/// 
///     let mut string = FixedStr3::default(); // equivalent to mem::zeroed but safe
///     string.fill_zeroes_str("abc");
/// 
///     assert_eq!(string.as_ref(), "abc");
/// 
///     let mut string = FixedStr3::new([b'a', b'b', 0]);
///     string.fill_zeroes_char('c');
/// 
///     assert_eq!(string.as_ref(), "abc");
/// }
/// ```
#[proc_macro]
pub fn fixed_len_str(input: TokenStream) -> TokenStream {
    return include_str!("code.in").to_string()
    .replace("$len", input.to_string().trim())
    .parse()
    .unwrap()
}