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 fn format_drift(&self, report: &crate::drift::DriftReport) -> String {
107 self.to_json(report)
108 }
109 fn format_posture(&self, posture: &crate::posture::EmailPosture) -> String {
110 self.to_json(posture)
111 }
112 fn format_caa(&self, policy: &crate::caa::CaaPolicy) -> String {
113 self.to_json(policy)
114 }
115 fn format_confusables(&self, report: &crate::confusables::ConfusableReport) -> String {
116 self.to_json(report)
117 }
118 fn format_subdomain_classification(
119 &self,
120 result: &crate::subdomains::SubdomainClassification,
121 ) -> String {
122 self.to_json(result)
123 }
124
125 fn format_subdomain_baseline_diff(
126 &self,
127 report: &crate::subdomains::SubdomainBaselineDiff,
128 ) -> String {
129 self.to_json(report)
130 }
131}
132
133#[cfg(test)]
134mod tests {
135 use super::*;
136 use crate::dns::RecordType;
137 use crate::status::StatusResponse;
138
139 #[test]
140 fn test_json_format_status() {
141 let response = StatusResponse::new("example.com".to_string());
142 let formatter = JsonFormatter::new();
143 let output = formatter.format_status(&response);
144 assert!(output.contains("example.com"));
145 assert!(output.contains("domain"));
146 }
147
148 #[test]
149 fn test_json_compact() {
150 let response = StatusResponse::new("example.com".to_string());
151 let formatter = JsonFormatter::new().compact();
152 let output = formatter.format_status(&response);
153 assert!(!output.contains("\n "));
155 }
156
157 #[test]
158 fn test_json_format_dns_records() {
159 let records = vec![DnsRecord {
160 name: "example.com".to_string(),
161 record_type: RecordType::A,
162 ttl: 300,
163 data: crate::dns::RecordData::A {
164 address: "93.184.216.34".to_string(),
165 },
166 }];
167 let formatter = JsonFormatter::new();
168 let output = formatter.format_dns(&records);
169 assert!(output.contains("93.184.216.34"));
170 assert!(output.contains("\"A\""));
171 }
172}