Skip to main content

ab_riscv_macros_common/
code_utils.rs

1//! This module contains hacks for Rust nightly syntax to make it compatible with `syn`
2// TODO: Remove this module once `syn` supports used Rust nightly syntax
3
4const FROM_IMPL_CONST_1: &str = " const ";
5const TO_IMPL_CONST_1: &str = " cnst::";
6const FROM_IMPL_CONST_2: &str = " const  ";
7const TO_IMPL_CONST_2: &str = " cnst ::";
8const FROM_IMPL_CONST_REVERT_1: &str = "pub cnst::";
9const TO_IMPL_CONST_REVERT_1: &str = "pub const ";
10const FROM_IMPL_CONST_REVERT_2: &str = "pub cnst ::";
11const TO_IMPL_CONST_REVERT_2: &str = "pub const  ";
12const FROM_IMPL_CONST_REVERT_3: &str = ") cnst::";
13const TO_IMPL_CONST_REVERT_3: &str = ") const ";
14const FROM_IMPL_CONST_REVERT_4: &str = ") cnst ::";
15const TO_IMPL_CONST_REVERT_4: &str = ") const  ";
16
17const FROM_BRACKETS_CONST_1: &str = "[const] ";
18const TO_BRACKETS_CONST_1: &str = "BRCONST+";
19const FROM_BRACKETS_CONST_2: &str = " [const] ";
20const TO_BRACKETS_CONST_2: &str = "BRCONST +";
21
22/// Replace bits of Rust nightly syntax with something that is technically valid in stable Rust, so
23/// `syn` can parse it
24pub fn pre_process_rust_code(s: &mut str) {
25    replace_inplace(s, FROM_IMPL_CONST_1, TO_IMPL_CONST_1);
26    replace_inplace(s, FROM_IMPL_CONST_2, TO_IMPL_CONST_2);
27    replace_inplace(s, FROM_IMPL_CONST_REVERT_1, TO_IMPL_CONST_REVERT_1);
28    replace_inplace(s, FROM_IMPL_CONST_REVERT_2, TO_IMPL_CONST_REVERT_2);
29    replace_inplace(s, FROM_IMPL_CONST_REVERT_3, TO_IMPL_CONST_REVERT_3);
30    replace_inplace(s, FROM_IMPL_CONST_REVERT_4, TO_IMPL_CONST_REVERT_4);
31    replace_inplace(s, FROM_BRACKETS_CONST_1, TO_BRACKETS_CONST_1);
32    replace_inplace(s, FROM_BRACKETS_CONST_2, TO_BRACKETS_CONST_2);
33}
34
35/// The inverse of [`pre_process_rust_code()`]
36pub fn post_process_rust_code(s: &mut str) {
37    replace_inplace(s, TO_IMPL_CONST_1, FROM_IMPL_CONST_1);
38    replace_inplace(s, TO_IMPL_CONST_2, FROM_IMPL_CONST_2);
39    replace_inplace(s, TO_BRACKETS_CONST_1, FROM_BRACKETS_CONST_1);
40    replace_inplace(s, TO_BRACKETS_CONST_2, FROM_BRACKETS_CONST_2);
41}
42
43fn replace_inplace(mut s: &mut str, from: &str, to: &str) {
44    assert_eq!(from.len(), to.len(), "`{from}` != `{to}`");
45
46    if from.is_empty() {
47        return;
48    }
49
50    while let Some(found) = s.find(from) {
51        let start = found;
52        let end = found + from.len();
53
54        // SAFETY: Replacing a valid string with a valid string of the same length
55        unsafe { s.as_bytes_mut() }
56            .get_mut(start..end)
57            .expect("Just found a string of the desired length; qed")
58            .copy_from_slice(to.as_bytes());
59
60        s = &mut s[end..];
61    }
62}