rewrit_core/normalize/
path.rs1use crate::normalize::{NormalizeContext, NormalizeError, Normalizer};
2use rewrit_model::{CanonicalError, Observation, StackFrame};
3
4#[derive(Debug, Clone)]
5pub struct PathNormalizer {
6 pub replacement: String,
7}
8
9impl Default for PathNormalizer {
10 fn default() -> Self {
11 Self {
12 replacement: "<PROJECT_ROOT>".to_string(),
13 }
14 }
15}
16
17impl Normalizer for PathNormalizer {
18 fn name(&self) -> &'static str {
19 "path"
20 }
21
22 fn normalize(
23 &self,
24 mut observation: Observation,
25 ctx: &NormalizeContext,
26 ) -> Result<Observation, NormalizeError> {
27 let Some(root) = &ctx.project_root else {
28 return Ok(observation);
29 };
30
31 observation.stdout.text = observation.stdout.text.replace(root, &self.replacement);
32 observation.stderr.text = observation.stderr.text.replace(root, &self.replacement);
33
34 if let Some(error) = &mut observation.error {
35 normalize_error(error, root, &self.replacement);
36 }
37
38 for artifact in &mut observation.artifacts {
39 artifact.path = artifact.path.replace(root, &self.replacement);
40 }
41
42 Ok(observation)
43 }
44}
45
46fn normalize_error(error: &mut CanonicalError, root: &str, replacement: &str) {
47 for StackFrame { file, .. } in &mut error.frames {
48 if let Some(file) = file {
49 *file = file.replace(root, replacement);
50 }
51 }
52}