Debian Codegraph @

tests/codegen.rs

raw symbols SCIP
1 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Write#, fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/};
2
3 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
4 fn generated_code_is_freshrust-analyzer cargo mediatype 0.23.0 generated_code_is_fresh().() {
5 let mut fresh = generate_constsrust-analyzer cargo mediatype 0.23.0 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_HEADERrust-analyzer cargo mediatype 0.23.0 NAMES_HEADER.,
15 );
16
17 fresh = fresh
18 && generate_constsrust-analyzer cargo mediatype 0.23.0 generate_consts().(
19 "Value",
20 "src/consts/values.txt",
21 "src/consts/values.rs",
22 &[("", "", "")],
23 VALUES_HEADERrust-analyzer cargo mediatype 0.23.0 VALUES_HEADER.,
24 );
25
26 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().fresh {
27 panicrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/panic!!("generated code is not fresh, please commit updates");
28 }
29 }
30
31 fn generate_constsrust-analyzer cargo mediatype 0.23.0 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 .iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().()
40 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|&(pf, name, comment)| (pf, name, comment, Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(1024)))
41 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<_>>();
42
43 let input = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().(input).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to read input file");
44 for line in input.linesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]lines().() {
45 let (ident, name) = if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(pair) = line.split_oncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_once().('=') {
46 pair
47 } else {
48 (line, line)
49 };
50
51 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((pf, _, _, out)) = prefixes.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().().findrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`IterMut<'a, T>`][Iterator]find().(|(pf, ..)| ident.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(pf)) {
52 let ident = upper_snake_caserust-analyzer cargo mediatype 0.23.0 upper_snake_case().(ident.trim_start_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_start_matches().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().pf));
53 let indent = if pf.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() { "" } else { " " };
54 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(out, "{}/// `{}`", indent, name).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
55 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(
56 out,
57 "{}pub const {}: crate::{} = crate::{}::new_unchecked(\"{}\");",
58 indent, ident, ty, ty, name
59 )
60 .unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
61 }
62 }
63
64 let mut new = Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#::with_capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]with_capacity().(1024);
65 new.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(header);
66 new.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("\n\n");
67 for (pf, name, comment, out) in prefixes {
68 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pf.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
69 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(new, "/// {}", comment).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
70 writelnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/writeln!!(new, "pub mod {} {{", name).unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().();
71 }
72
73 new.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().(&out);
74
75 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pf.is_emptyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]is_empty().() {
76 new.push_strrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String]push_str().("}\n\n");
77 }
78 }
79
80 let old = fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::read_to_stringrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/read_to_string().(dst).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to read destination file");
81 if old rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().new {
82 fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::writerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/write().(dst, &new).expectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]expect().("failed to write destination file");
83 }
84
85 old rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<Self>`]eq().==rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<Self>`]eq(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`PartialEq<Self>`]eq().new
86 }
87
88 fn upper_snake_caserust-analyzer cargo mediatype 0.23.0 upper_snake_case().(s: &str) -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String# {
89 let s = s
90 .split_inclusiverust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_inclusive().(char::is_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_uppercase().)
91 .maprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#map().(|chunk| {
92 if chunk.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(char::is_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_uppercase().) {
93 let prefix = chunk.trim_end_matchesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]trim_end_matches().(char::is_uppercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_uppercase().);
94 if prefix.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(char::is_lowercaserust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_lowercase().) {
95 return formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("{}_{}", prefix, chunk.split_atrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split_at().(chunk.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() -rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). 1).1);
96 }
97 }
98 chunk.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().()
99 })
100 .collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().::<Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#>()
101 .replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().('+', "_plus")
102 .replacerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]replace().(|c| !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().char::is_ascii_alphanumericrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_ascii_alphanumeric().(&c), "_")
103 .to_ascii_uppercaserust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str]to_ascii_uppercase().();
104
105 if s.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(char::is_numericrust-analyzer cargo core https://github.com/rust-lang/rust/library/core char/methods/impl#[char]is_numeric().) {
106 formatrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/format!!("_{}", s)
107 } else {
108 s
109 }
110 }
111
112 const NAMES_HEADERrust-analyzer cargo mediatype 0.23.0 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_HEADERrust-analyzer cargo mediatype 0.23.0 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>"#;