Debian Codegraph @

tests/codegen.rs

raw symbols SCIP
1 use std::{fmt::Write, fs};
2
3 #[test]
4 fn generated_code_is_fresh() {
5 let mut fresh = generate_consts(
6 "Name",
7 "src/consts/names.txt",
8 "src/consts/names.rs",
9 &[
10 ("vnd.", "vnd", "Vendor subtypes starting with `vnd.`."),
11 ("x-", "x_", "Unregistered subtypes starting with `x-`."),
12 ("", "", ""),
13 ],
14 NAMES_HEADER,
15 );
16
17 fresh = fresh
18 && generate_consts(
19 "Value",
20 "src/consts/values.txt",
21 "src/consts/values.rs",
22 &[("", "", "")],
23 VALUES_HEADER,
24 );
25
26 if !fresh {
27 panic!("generated code is not fresh, please commit updates");
28 }
29 }
30
31 fn generate_consts(
32 ty: &str,
33 input: &str,
34 dst: &str,
35 prefixes: &[(&str, &str, &str)],
36 header: &str,
37 ) -> bool {
38 let mut prefixes = prefixes
39 .iter()
40 .map(|&(pf, name, comment)| (pf, name, comment, String::with_capacity(1024)))
41 .collect::<Vec<_>>();
42
43 let input = fs::read_to_string(input).expect("failed to read input file");
44 for line in input.lines() {
45 let (ident, name) = if let Some(pair) = line.split_once('=') {
46 pair
47 } else {
48 (line, line)
49 };
50
51 if let Some((pf, _, _, out)) = prefixes.iter_mut().find(|(pf, ..)| ident.starts_with(pf)) {
52 let ident = upper_snake_case(ident.trim_start_matches(*pf));
53 let indent = if pf.is_empty() { "" } else { " " };
54 writeln!(out, "{}/// `{}`", indent, name).unwrap();
55 writeln!(
56 out,
57 "{}pub const {}: crate::{} = crate::{}::new_unchecked(\"{}\");",
58 indent, ident, ty, ty, name
59 )
60 .unwrap();
61 }
62 }
63
64 let mut new = String::with_capacity(1024);
65 new.push_str(header);
66 new.push_str("\n\n");
67 for (pf, name, comment, out) in prefixes {
68 if !pf.is_empty() {
69 writeln!(new, "/// {}", comment).unwrap();
70 writeln!(new, "pub mod {} {{", name).unwrap();
71 }
72
73 new.push_str(&out);
74
75 if !pf.is_empty() {
76 new.push_str("}\n\n");
77 }
78 }
79
80 let old = fs::read_to_string(dst).expect("failed to read destination file");
81 if old != new {
82 fs::write(dst, &new).expect("failed to write destination file");
83 }
84
85 old == new
86 }
87
88 fn upper_snake_case(s: &str) -> String {
89 let s = s
90 .split_inclusive(char::is_uppercase)
91 .map(|chunk| {
92 if chunk.ends_with(char::is_uppercase) {
93 let prefix = chunk.trim_end_matches(char::is_uppercase);
94 if prefix.ends_with(char::is_lowercase) {
95 return format!("{}_{}", prefix, chunk.split_at(chunk.len() - 1).1);
96 }
97 }
98 chunk.to_string()
99 })
100 .collect::<String>()
101 .replace('+', "_plus")
102 .replace(|c| !char::is_ascii_alphanumeric(&c), "_")
103 .to_ascii_uppercase();
104
105 if s.starts_with(char::is_numeric) {
106 format!("_{}", s)
107 } else {
108 s
109 }
110 }
111
112 const NAMES_HEADER: &str = r#"//! Predefined names, @generated in tests/codegen.rs
113 //!
114 //! # Sources
115 //! - <https://en.wikipedia.org/wiki/Media_type>
116 //! - <https://www.iana.org/assignments/media-types/media-types.xhtml>
117 //! - <https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_type>
118 //! - <https://developer.mozilla.org/en-US/docs/Glossary/Quality_values>
119 //! - <https://datatracker.ietf.org/doc/html/rfc3676>
120 //! - <https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types>"#;
121
122 const VALUES_HEADER: &str = r#"//! Predefined parameter values, @generated in tests/codegen.rs
123 //!
124 //! # Sources
125 //! - <https://www.iana.org/assignments/character-sets/character-sets.xhtml>
126 //! - <https://datatracker.ietf.org/doc/html/rfc3676>"#;