verilization_compiler/util.rs
1#![macro_use]
2
3//! Utility functions that can be useful for generators.
4
5
6
7/// Creates a loop with code executed in between each iteration.
8///
9/// ```
10/// use verilization_compiler::for_sep;
11/// let mut str = String::from("");
12/// for_sep!(x, &["a", "b"], { str += ", "; }, {
13/// str += x;
14/// });
15/// assert_eq!(str, "a, b");
16/// ```
17///
18/// Loops over the second parameter with the fourth parameter as the body.
19/// The third parameter block is executed in between each loop iteration.
20/// The first parameter is a pattern for each value.
21#[macro_export]
22macro_rules! for_sep {
23 ($var : pat, $iterator : expr, $sep : block, $body : block) => {
24 {
25 let mut iter = std::iter::IntoIterator::into_iter($iterator);
26 if let Some(item) = iter.next() {
27 {
28 let $var = item;
29 $body;
30 }
31
32 while let Some(item) = iter.next() {
33 $sep;
34 {
35 let $var = item;
36 $body;
37 }
38 }
39 }
40 }
41 };
42}
43
44/// Capitalizes the first character of the string.
45///
46/// ```
47/// use verilization_compiler::util::capitalize_identifier;
48/// let mut word = String::from("hello");
49/// capitalize_identifier(&mut word);
50/// assert_eq!(word, "Hello");
51/// ```
52pub fn capitalize_identifier(word: &mut str) {
53 if let Some(start) = word.get_mut(0..1) {
54 start.make_ascii_uppercase()
55 }
56}
57
58/// Uncapitalizes the first character of the string.
59///
60/// ```
61/// use verilization_compiler::util::uncapitalize_identifier;
62/// let mut word = String::from("HELLO");
63/// uncapitalize_identifier(&mut word);
64/// assert_eq!(word, "hELLO");
65/// ```
66pub fn uncapitalize_identifier(word: &mut str) {
67 if let Some(start) = word.get_mut(0..1) {
68 start.make_ascii_lowercase()
69 }
70}
71
72