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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#![doc(html_root_url = "https://docs.rs/fixed_len_str/0.3.0/")]
#![no_std]

//! This crate provides a procedural macro for declare a wrapper struct for an array with the size
//! given by the tokens which derefs to [`str`].
//! 
//! For a proper API documentation of one of length 12 see [fixed_len_str_example].
//!
//! If you want to use [serde] to serialize and deserialize the fixed_len_str use
//! `features = ["serde_support"]`,if you want to match a pattern of type
//! `FnMut(FixedStr$len) -> bool` on a [`str`] use `features = ["pattern_pred_support"]`
//! and if you want the documentation visible at the expansion use `default-features = false`.
//! 
//! [`str`]: https://doc.rust-lang.org/std/primitive.str.html
//! [fixed_len_str_example]: https://docs.rs/fixed_len_str_example/
//! [serde]: https://crates.io/crates/serde

extern crate proc_macro;
extern crate alloc;

use proc_macro::TokenStream;

use alloc::string::ToString;

/// A macro for declare an FixedStr**input** struct.
/// 
/// # Examples
/// 
/// ```
/// #![feature(proc_macro_hygiene)] 
/// // this is only needed here as I expanding as statements the respective items
/// #![feature(pattern)] 
/// // needed for use the unstable API pattern 
/// 
/// use fixed_len_str::fixed_len_str;
/// 
/// fixed_len_str!(3);
/// 
/// let string = FixedStr3::from("abc");
/// 
/// assert_eq!(string, "abc");
/// 
/// let string = FixedStr3::new(*b"abc").unwrap();
/// 
/// assert_eq!(string, "abc");
/// 
/// let mut string = FixedStr3::default(); // equivalent to mem::zeroed but safe
/// string.fill_str("abc");
/// 
/// assert_eq!(string, "abc");
/// 
/// let mut string = FixedStr3::new([b'a', b'b', 0]).unwrap();
/// assert_eq!(string, "ab");
/// string.fill_char('c');
/// 
/// assert_eq!(string, "abc");
/// assert_eq!(string.as_bytes(), (&string[..]).as_bytes()); // this is only certain with non-zero bytes
/// assert_eq!(string.into_string(), String::from(&string[..])); // clone or consume at your option
/// assert_eq!(FixedStr3::from(&[][..]).as_ref(), "");
/// 
/// if cfg!(feature = "pattern_pred_support") {
///     use fixed_str3::Closure; // needed due to the orphan rule
/// 
///     assert_eq!("aaabb".matches(Closure::from(|s: FixedStr3| s == "aaa" || s == "bb"))
///                       .collect::<Vec<&str>>(), ["aaa", "bb"]);
/// }
/// ```
#[proc_macro]
pub fn fixed_len_str(input: TokenStream) -> TokenStream {
    let mut string = include_str!("code.in").to_string();

    if cfg!(feature = "serde_support") {
        string.push_str(include_str!("serde_code.in"))
    }

    if cfg!(feature = "pattern_pred_support") {
        string.push_str(include_str!("pattern_pred_code.in"))
    }

    string.push_str("
}

$doc_hide
pub use fixed_str$len::FixedStr$len;");

    if cfg!(feature = "docs_hidden") {
        string = string.replace("$doc_hide", "#[doc(hidden)]");
    } else {
        string = string.replace("$doc_hide", "");
    }

    string.replace("$len", input.to_string().trim()).parse().unwrap()
}

/// A macro for declare an FixedStr**input** struct with NonZeroU8 as utf8 encoded bytes.
/// 
/// # Examples
/// 
/// ```
/// #![feature(proc_macro_hygiene)] 
/// // this is only needed here as I expanding as statements the respective items
/// #![feature(pattern)] 
/// // needed for use the unstable API pattern 
/// 
/// use fixed_len_str::fixed_len_str_nz;
/// use core::mem::transmute;
/// 
/// fixed_len_str_nz!(3);
/// 
/// let string = FixedStrNZ3::from("abc");
/// 
/// assert_eq!(string, "abc");
/// 
/// let string = FixedStrNZ3::new(unsafe { transmute(*b"abc") }).unwrap();
/// 
/// assert_eq!(string, "abc");
/// assert_eq!(string.as_bytes(), (&string[..]).as_bytes()); 
/// assert_eq!(string.into_string(), String::from(&string[..])); // clone or consume at your option
/// 
/// assert_eq!(FixedStrNZ3::from(unsafe { transmute(&[b'a', b'b', b'c', b'd']) }).as_ref(), "abc");
/// 
/// if cfg!(feature = "pattern_pred_support") {
///     use fixed_str_nz3::Closure; // needed due to the orphan rule
/// 
///     assert_eq!("aaabb".matches(Closure::from(|s: FixedStrNZ3| s == "aaa" || s == "bb"))
///                       .collect::<Vec<&str>>(), ["aaa"]);
/// }
/// ```
#[proc_macro]
pub fn fixed_len_str_nz(input: TokenStream) -> TokenStream {
    let mut string = include_str!("code_nz.in").to_string();

    if cfg!(feature = "serde_support") {
        string.push_str(include_str!("serde_code_nz.in"))
    }

    if cfg!(feature = "pattern_pred_support") {
        string.push_str(include_str!("pattern_pred_code_nz.in"))
    }

    string.push_str("
}

$doc_hide
pub use fixed_str_nz$len::FixedStrNZ$len;");

    if cfg!(feature = "docs_hidden") {
        string = string.replace("$doc_hide", "#[doc(hidden)]");
    } else {
        string = string.replace("$doc_hide", "");
    }

    string.replace("$len", input.to_string().trim()).parse().unwrap()
}