1use super::OutputFormatter;
2use crate::dns::{DnsRecord, FollowIteration, FollowResult, PropagationResult};
3use crate::lookup::LookupResult;
4use crate::rdap::RdapResponse;
5use crate::status::StatusResponse;
6use crate::whois::WhoisResponse;
7
8pub struct JsonFormatter {
9 pretty: bool,
10}
11
12impl Default for JsonFormatter {
13 fn default() -> Self {
14 Self::new()
15 }
16}
17
18impl JsonFormatter {
19 pub fn new() -> Self {
20 Self { pretty: true }
21 }
22
23 pub fn compact(mut self) -> Self {
24 self.pretty = false;
25 self
26 }
27
28 fn to_json<T: serde::Serialize + ?Sized>(&self, value: &T) -> String {
29 if self.pretty {
30 serde_json::to_string_pretty(value)
31 .unwrap_or_else(|e| format!("{{\"error\": \"{}\"}}", e))
32 } else {
33 serde_json::to_string(value).unwrap_or_else(|e| format!("{{\"error\": \"{}\"}}", e))
34 }
35 }
36}
37
38impl OutputFormatter for JsonFormatter {
39 fn format_whois(&self, response: &WhoisResponse) -> String {
40 self.to_json(response)
41 }
42
43 fn format_rdap(&self, response: &RdapResponse) -> String {
44 self.to_json(response)
45 }
46
47 fn format_dns(&self, records: &[DnsRecord]) -> String {
48 self.to_json(records)
49 }
50
51 fn format_propagation(&self, result: &PropagationResult) -> String {
52 self.to_json(result)
53 }
54
55 fn format_lookup(&self, result: &LookupResult) -> String {
56 self.to_json(result)
57 }
58
59 fn format_status(&self, response: &StatusResponse) -> String {
60 self.to_json(response)
61 }
62
63 fn format_follow_iteration(&self, iteration: &FollowIteration) -> String {
64 self.to_json(iteration)
65 }
66
67 fn format_follow(&self, result: &FollowResult) -> String {
68 self.to_json(result)
69 }
70
71 fn format_availability(&self, result: &crate::availability::AvailabilityResult) -> String {
72 self.to_json(result)
73 }
74
75 fn format_dnssec(&self, report: &crate::dns::DnssecReport) -> String {
76 self.to_json(report)
77 }
78
79 fn format_tld(&self, info: &crate::tld::TldInfo) -> String {
80 self.to_json(info)
81 }
82
83 fn format_dns_comparison(&self, comparison: &crate::dns::DnsComparison) -> String {
84 self.to_json(comparison)
85 }
86
87 fn format_subdomains(&self, result: &crate::subdomains::SubdomainResult) -> String {
88 self.to_json(result)
89 }
90
91 fn format_diff(&self, diff: &crate::diff::DomainDiff) -> String {
92 self.to_json(diff)
93 }
94
95 fn format_ssl(&self, report: &crate::ssl::SslReport) -> String {
96 self.to_json(report)
97 }
98
99 fn format_watch(&self, report: &crate::watchlist::WatchReport) -> String {
100 self.to_json(report)
101 }
102
103 fn format_domain_info(&self, info: &crate::domain_info::DomainInfo) -> String {
104 self.to_json(info)
105 }
106}
107
108#[cfg(test)]
109mod tests {
110 use super::*;
111 use crate::dns::RecordType;
112 use crate::status::StatusResponse;
113
114 #[test]
115 fn test_json_format_status() {
116 let response = StatusResponse::new("example.com".to_string());
117 let formatter = JsonFormatter::new();
118 let output = formatter.format_status(&response);
119 assert!(output.contains("example.com"));
120 assert!(output.contains("domain"));
121 }
122
123 #[test]
124 fn test_json_compact() {
125 let response = StatusResponse::new("example.com".to_string());
126 let formatter = JsonFormatter::new().compact();
127 let output = formatter.format_status(&response);
128 assert!(!output.contains("\n "));
130 }
131
132 #[test]
133 fn test_json_format_dns_records() {
134 let records = vec![crate::dns::DnsRecord {
135 name: "example.com".to_string(),
136 record_type: RecordType::A,
137 ttl: 300,
138 data: crate::dns::RecordData::A {
139 address: "93.184.216.34".to_string(),
140 },
141 }];
142 let formatter = JsonFormatter::new();
143 let output = formatter.format_dns(&records);
144 assert!(output.contains("93.184.216.34"));
145 assert!(output.contains("\"A\""));
146 }
147}