api_testing_core/
report.rs1use crate::markdown;
2
3pub struct ReportHeader<'a> {
4 pub report_date: &'a str,
5 pub case_name: &'a str,
6 pub generated_at: &'a str,
7 pub endpoint_note: &'a str,
8 pub result_note: &'a str,
9 pub command_snippet: Option<&'a str>,
10}
11
12pub struct ReportBuilder {
13 out: String,
14}
15
16impl ReportBuilder {
17 pub fn new(header: ReportHeader<'_>) -> Self {
18 let mut out = String::new();
19
20 out.push_str(&markdown::heading(
21 1,
22 &format!("API Test Report ({})", header.report_date),
23 ));
24 out.push('\n');
25 out.push_str(&markdown::heading(
26 2,
27 &format!("Test Case: {}", header.case_name),
28 ));
29 out.push('\n');
30
31 if let Some(cmd) = header.command_snippet {
32 out.push_str(&markdown::heading(2, "Command"));
33 out.push('\n');
34 out.push_str(&markdown::code_block("bash", cmd));
35 out.push('\n');
36 }
37
38 out.push_str(&format!("Generated at: {}\n\n", header.generated_at));
39 out.push_str(&format!("{}\n\n", header.endpoint_note));
40 out.push_str(&format!("{}\n\n", header.result_note));
41
42 Self { out }
43 }
44
45 pub fn push_section_heading(&mut self, title: &str) {
46 self.out.push_str(&format!("### {title}\n\n"));
47 }
48
49 pub fn push_list_item(&mut self, item: &str) {
50 self.out.push_str(&format!("- {item}\n"));
51 }
52
53 pub fn push_blank_line(&mut self) {
54 self.out.push('\n');
55 }
56
57 pub fn push_code_section(
58 &mut self,
59 title: &str,
60 lang: &str,
61 body: &str,
62 note: Option<&str>,
63 trailing_blank: bool,
64 ) {
65 self.push_section_heading(title);
66 if let Some(note) = note {
67 let note = note.trim();
68 if !note.is_empty() {
69 self.out.push_str(note);
70 self.out.push_str("\n\n");
71 }
72 }
73 self.out.push_str(&markdown::code_block(lang, body));
74 if trailing_blank {
75 self.out.push('\n');
76 }
77 }
78
79 pub fn finish(self) -> String {
80 self.out
81 }
82}