Skip to content

Commit 7eec940

Browse files
authored
ref(conventions): Clean up build.rs (#5254)
1. Renames the `raw` module to `attributes` 2. Moves some attribute-related definitions into the module 3. Extracts attribute-map generation code into its own function
1 parent 734f6a0 commit 7eec940

File tree

2 files changed

+49
-42
lines changed

2 files changed

+49
-42
lines changed

relay-conventions/build/raw.rs renamed to relay-conventions/build/attributes.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Contains type definitions for deserializing attribute definitions from JSON files in `sentry-conventions/model`.
22
3+
use std::collections::BTreeMap;
4+
35
use serde::Deserialize;
46

57
/// Whether an attribute can contain PII.
@@ -89,3 +91,35 @@ pub fn format_attribute_info(attr: Attribute) -> (String, String) {
8991
);
9092
(key, value)
9193
}
94+
95+
/// Parse a path-like attribute key into individual segments.
96+
///
97+
/// NOTE: This does not yet support escaped segments, e.g. `"foo.'my.thing'.bar"` will split into
98+
/// `["foo.'my", "thing'.bar"]`.
99+
pub fn parse_segments(key: &str) -> impl Iterator<Item = &str> {
100+
key.split('.')
101+
}
102+
103+
#[derive(Default)]
104+
pub struct RawNode {
105+
pub children: BTreeMap<String, RawNode>,
106+
pub info: Option<String>,
107+
}
108+
109+
impl RawNode {
110+
pub fn build(&self, w: &mut impl std::io::Write) -> Result<(), std::io::Error> {
111+
let Self { children, info } = self;
112+
write!(w, "Node {{ info: ")?;
113+
match info {
114+
Some(info) => write!(w, "Some({})", info)?,
115+
None => write!(w, "None")?,
116+
};
117+
write!(w, ", children: ::phf::phf_map!{{",)?;
118+
for (segment, child) in children {
119+
write!(w, "\"{segment}\" => ")?;
120+
child.build(w)?;
121+
write!(w, ",")?;
122+
}
123+
write!(w, "}} }}")
124+
}
125+
}

relay-conventions/build/build.rs

Lines changed: 15 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1+
mod attributes;
12
mod name;
2-
mod raw;
33

4-
use std::collections::BTreeMap;
54
use std::env;
65
use std::fs::File;
76
use std::io::{BufWriter, Write};
@@ -12,40 +11,22 @@ use walkdir::WalkDir;
1211
const ATTRIBUTE_DIR: &str = "sentry-conventions/model/attributes";
1312
const NAME_DIR: &str = "sentry-conventions/model/name";
1413

15-
#[derive(Default)]
16-
struct RawNode {
17-
children: BTreeMap<String, RawNode>,
18-
info: Option<String>,
19-
}
14+
fn main() {
15+
let crate_dir: PathBuf = env::var("CARGO_MANIFEST_DIR").unwrap().into();
2016

21-
impl RawNode {
22-
fn build(&self, w: &mut impl std::io::Write) -> Result<(), std::io::Error> {
23-
let Self { children, info } = self;
24-
write!(w, "Node {{ info: ")?;
25-
match info {
26-
Some(info) => write!(w, "Some({})", info)?,
27-
None => write!(w, "None")?,
28-
};
29-
write!(w, ", children: ::phf::phf_map!{{",)?;
30-
for (segment, child) in children {
31-
write!(w, "\"{segment}\" => ")?;
32-
child.build(w)?;
33-
write!(w, ",")?;
34-
}
35-
write!(w, "}} }}")
36-
}
37-
}
17+
write_attribute_rs(&crate_dir);
18+
write_name_rs(&crate_dir);
19+
20+
// Ideally this would only run when compiling for tests, but #[cfg(test)] doesn't seem to work
21+
// here.
22+
write_test_name_rs();
3823

39-
/// Parse a path-like attribute key into individual segments.
40-
///
41-
/// NOTE: This does not yet support escaped segments, e.g. `"foo.'my.thing'.bar"` will split into
42-
/// `["foo.'my", "thing'.bar"]`.
43-
fn parse_segments(key: &str) -> impl Iterator<Item = &str> {
44-
key.split('.')
24+
println!("cargo::rerun-if-changed=.");
4525
}
4626

47-
fn main() {
48-
let crate_dir: PathBuf = env::var("CARGO_MANIFEST_DIR").unwrap().into();
27+
fn write_attribute_rs(crate_dir: &Path) {
28+
use attributes::{Attribute, RawNode, format_attribute_info, parse_segments};
29+
4930
let mut root = RawNode::default();
5031

5132
for file in WalkDir::new(crate_dir.join(ATTRIBUTE_DIR)) {
@@ -55,8 +36,8 @@ fn main() {
5536
&& ext.to_str() == Some("json")
5637
{
5738
let contents = std::fs::read_to_string(file.path()).unwrap();
58-
let attr: raw::Attribute = serde_json::from_str(&contents).unwrap();
59-
let (key, value) = raw::format_attribute_info(attr);
39+
let attr: Attribute = serde_json::from_str(&contents).unwrap();
40+
let (key, value) = format_attribute_info(attr);
6041

6142
let mut node = &mut root;
6243
let mut parts = parse_segments(&key).peekable();
@@ -79,14 +60,6 @@ fn main() {
7960
write!(&mut out_file, "static ATTRIBUTES: Node<AttributeInfo> = ",).unwrap();
8061
root.build(&mut out_file).unwrap();
8162
write!(&mut out_file, ";").unwrap();
82-
83-
write_name_rs(&crate_dir);
84-
85-
// Ideally this would only run when compiling for tests, but #[cfg(test)] doesn't seem to work
86-
// here.
87-
write_test_name_rs();
88-
89-
println!("cargo::rerun-if-changed=.");
9063
}
9164

9265
fn write_name_rs(crate_dir: &Path) {

0 commit comments

Comments
 (0)