Skip to content

Commit

Permalink
&[Variable] -> Context
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Sep 30, 2019
1 parent 60f442e commit 69a0d0d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 25 deletions.
24 changes: 14 additions & 10 deletions src/normalize.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
pub struct Variable {
pub name: &'static str,
pub value: String,
use std::path::Path;

#[derive(Copy, Clone)]
pub struct Context<'a> {
pub krate: &'a str,
pub source_dir: &'a Path,
pub workspace: &'a Path,
}

pub fn trim<S: AsRef<[u8]>>(output: S) -> String {
Expand Down Expand Up @@ -28,7 +32,7 @@ pub fn trim<S: AsRef<[u8]>>(output: S) -> String {
///
/// There is one "preferred" variation which is what we print when the stderr
/// file is absent or not a match.
pub fn diagnostics(output: Vec<u8>, context: &[Variable]) -> Variations {
pub fn diagnostics(output: Vec<u8>, context: Context) -> Variations {
let mut from_bytes = String::from_utf8_lossy(&output).to_string();
from_bytes = from_bytes.replace("\r\n", "\n");

Expand Down Expand Up @@ -71,7 +75,7 @@ enum Normalization {

use self::Normalization::*;

fn apply(original: &str, normalization: Normalization, context: &[Variable]) -> String {
fn apply(original: &str, normalization: Normalization, context: Context) -> String {
let mut normalized = String::new();

for line in original.lines() {
Expand All @@ -86,7 +90,7 @@ fn apply(original: &str, normalization: Normalization, context: &[Variable]) ->
trim(normalized)
}

fn filter(line: &str, normalization: Normalization, context: &[Variable]) -> Option<String> {
fn filter(line: &str, normalization: Normalization, context: Context) -> Option<String> {
if line.trim_start().starts_with("--> ") {
if let Some(cut_end) = line.rfind(&['/', '\\'][..]) {
let cut_start = line.find('>').unwrap() + 2;
Expand Down Expand Up @@ -129,10 +133,10 @@ fn filter(line: &str, normalization: Normalization, context: &[Variable]) -> Opt
}
}

let mut line = line.to_owned();
for var in context {
line = line.replace(var.name, &var.value);
}
let line = line
.replace("$CRATE", context.krate)
.replace("$DIR", context.source_dir.to_string_lossy().as_ref())
.replace("$WORKSPACE", context.workspace.to_string_lossy().as_ref());

Some(line)
}
21 changes: 6 additions & 15 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::error::{Error, Result};
use crate::features;
use crate::manifest::{Bin, Build, Config, Manifest, Name, Package, Workspace};
use crate::message::{self, Fail, Warn};
use crate::normalize::{self, Variable, Variations};
use crate::normalize::{self, Context, Variations};
use crate::rustflags;

#[derive(Debug)]
Expand Down Expand Up @@ -202,20 +202,11 @@ impl Test {
let stdout = output.stdout;
let stderr = normalize::diagnostics(
output.stderr,
&[
Variable {
name: "$CRATE",
value: name.0.clone(),
},
Variable {
name: "$DIR",
value: project.source_dir.to_string_lossy().into_owned(),
},
Variable {
name: "WORKSPACE",
value: project.workspace.to_string_lossy().into_owned(),
},
],
Context {
krate: &name.0,
source_dir: &project.source_dir,
workspace: &project.workspace,
},
);

let check = match self.expected {
Expand Down

0 comments on commit 69a0d0d

Please sign in to comment.