1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
use crate::req::RequestContext;
use anyhow::Result;
use console::{style, Style};
use reqwest::Response;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use similar::{ChangeTag, TextDiff};
use std::{collections::HashMap, fmt, io::Write, path::Path};
use tokio::fs;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct DiffConfig {
#[serde(flatten)]
ctxs: HashMap<String, DiffContext>,
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct DiffContext {
pub request1: RequestContext,
pub request2: RequestContext,
#[serde(skip_serializing_if = "is_default_response", default)]
pub response: ResponseContext,
}
fn is_default_response(r: &ResponseContext) -> bool {
r == &ResponseContext::default()
}
#[derive(Serialize, Deserialize, Debug, Clone, Default, PartialEq, Eq)]
pub struct ResponseContext {
#[serde(skip_serializing_if = "Vec::is_empty")]
pub skip_headers: Vec<String>,
}
#[derive(Debug, PartialEq, Eq)]
pub enum DiffResult {
Equal,
Diff(String),
}
struct Line(Option<usize>);
impl fmt::Display for Line {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
None => write!(f, " "),
Some(idx) => write!(f, "{:<4}", idx + 1),
}
}
}
impl DiffConfig {
pub async fn try_load(path: impl AsRef<Path>) -> Result<DiffConfig> {
let file = fs::read_to_string(path).await?;
let config: DiffConfig = serde_yaml::from_str(&file)?;
for (profile, ctx) in config.ctxs.iter() {
if !ctx.request1.params.is_object() || !ctx.request2.params.is_object() {
return Err(anyhow::anyhow!(
"params in request1 or request2 must be an object in profile: {}",
profile
));
}
}
Ok(config)
}
pub fn get(&self, profile: &str) -> Result<&DiffContext> {
self.ctxs
.get(profile)
.ok_or_else(|| anyhow::anyhow!("profile {} not found", profile))
}
pub async fn diff(&self, profile: &str) -> Result<DiffResult> {
let ctx = self
.ctxs
.get(profile)
.ok_or_else(|| anyhow::anyhow!("profile {} not found", profile))?;
ctx.diff().await
}
}
impl DiffContext {
pub async fn diff(&self) -> Result<DiffResult> {
let res1 = self.request1.send().await?;
let res2 = self.request2.send().await?;
self.diff_response(res1, res2).await
}
async fn diff_response(&self, res1: Response, res2: Response) -> Result<DiffResult> {
let url1 = res1.url().to_string();
let url2 = res2.url().to_string();
let text1 = self.request_to_string(res1).await?;
let text2 = self.request_to_string(res2).await?;
if text1 != text2 {
let headers = format!("--- a/{}\n+++ b/{}\n", url1, url2);
return Ok(DiffResult::Diff(build_diff(headers, text1, text2)?));
}
Ok(DiffResult::Equal)
}
async fn request_to_string(&self, res: Response) -> Result<String> {
let mut buf = Vec::new();
writeln!(&mut buf, "{}", res.status()).unwrap();
res.headers().iter().for_each(|(k, v)| {
if self.response.skip_headers.iter().any(|v| v == k.as_str()) {
return;
}
writeln!(&mut buf, "{}: {:?}", k, v).unwrap();
});
writeln!(&mut buf).unwrap();
let mut body = res.text().await?;
if let Ok(json) = serde_json::from_str::<Value>(&body) {
body = serde_json::to_string_pretty(&json)?;
}
writeln!(&mut buf, "{}", body).unwrap();
Ok(String::from_utf8(buf)?)
}
}
fn build_diff(headers: String, old: String, new: String) -> Result<String> {
let diff = TextDiff::from_lines(&old, &new);
let mut buf = Vec::with_capacity(4096);
writeln!(&mut buf, "{}", headers).unwrap();
for (idx, group) in diff.grouped_ops(3).iter().enumerate() {
if idx > 0 {
writeln!(&mut buf, "{:-^1$}", "-", 80)?;
}
for op in group {
for change in diff.iter_inline_changes(op) {
let (sign, s) = match change.tag() {
ChangeTag::Delete => ("-", Style::new().red()),
ChangeTag::Insert => ("+", Style::new().green()),
ChangeTag::Equal => (" ", Style::new().dim()),
};
write!(
&mut buf,
"{}{} |{}",
style(Line(change.old_index())).dim(),
style(Line(change.new_index())).dim(),
s.apply_to(sign).bold(),
)?;
for (emphasized, value) in change.iter_strings_lossy() {
if emphasized {
write!(&mut buf, "{}", s.apply_to(value).underlined().on_black())?;
} else {
write!(&mut buf, "{}", s.apply_to(value))?;
}
}
if change.missing_newline() {
writeln!(&mut buf)?;
}
}
}
}
Ok(String::from_utf8(buf)?)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn diff_request_should_work() {
let config = DiffConfig::try_load("fixtures/diff.yml").await.unwrap();
let result = config.diff("rust").await.unwrap();
assert_eq!(result, DiffResult::Equal);
}
}