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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
use crate::json_utils::split_key_val_pair; use smallvec::SmallVec; use std::sync::Mutex; use crate::misc_utils::to_camelcase; use std::fmt; use crate::output_channel::OutputChannel; #[derive(Copy, Clone, PartialEq, Debug)] pub enum BracketType { Curly, Square, } #[derive(Clone, Debug)] struct JSONContext { first_item: bool, bracket_type: BracketType, } impl JSONContext { pub fn new(bracket_type: BracketType) -> JSONContext { JSONContext { first_item: true, bracket_type, } } } #[derive(Debug)] pub struct JSONPrinter { json_enabled: bool, output_channel: OutputChannel, contexts: Mutex<SmallVec<[JSONContext; 8]>>, } impl Clone for JSONPrinter { fn clone(&self) -> Self { JSONPrinter { json_enabled: self.json_enabled, output_channel: self.output_channel, contexts: Mutex::new(self.contexts.lock().unwrap().clone()), } } } fn write_comma_if_not_first(f: &mut fmt::Formatter, context: &mut JSONContext) -> fmt::Result { if !context.first_item { write!(f, ",")?; } context.first_item = false; Ok(()) } fn bracket_type_to_str_open(bracket_type: BracketType) -> &'static str { match bracket_type { BracketType::Curly => "{", BracketType::Square => "[", } } fn bracket_type_to_str_close(bracket_type: BracketType) -> &'static str { match bracket_type { BracketType::Curly => "}", BracketType::Square => "]", } } impl JSONPrinter { pub fn new(json_enabled: bool, output_channel: OutputChannel) -> JSONPrinter { JSONPrinter { json_enabled, output_channel, contexts: Mutex::new(SmallVec::new()), } } pub fn json_enabled(&self) -> bool { self.json_enabled } pub fn output_channel(&self) -> OutputChannel { self.output_channel } pub fn set_output_channel(&mut self, output_channel: OutputChannel) { self.output_channel = output_channel } pub fn first_item(&self) -> bool { self.contexts.lock().unwrap().last().unwrap().first_item } fn print_comma_if_not_first(&self, context: &mut JSONContext) { if !context.first_item { print_at_output_channel!(self.output_channel => ","); } context.first_item = false; } pub fn print_open_bracket(&self, name: Option<&str>, bracket_type: BracketType) { if !self.json_enabled { return; } match self.contexts.lock().unwrap().last_mut() { None => {} Some(x) => self.print_comma_if_not_first(x), } match name { None => {} Some(n) => print_at_output_channel!(self.output_channel => "\"{}\": ", to_camelcase(n)), } println_at_output_channel!(self.output_channel => "{}", bracket_type_to_str_open(bracket_type)); self.contexts .lock() .unwrap() .push(JSONContext::new(bracket_type)); } pub fn write_open_bracket( &self, f: &mut fmt::Formatter, name: Option<&str>, bracket_type: BracketType, ) -> fmt::Result { if !self.json_enabled { return Ok(()); } match self.contexts.lock().unwrap().last_mut() { None => {} Some(x) => write_comma_if_not_first(f, x)?, } match name { None => {} Some(n) => write!(f, "\"{}\": ", to_camelcase(n))?, } writeln!(f, "{}", bracket_type_to_str_open(bracket_type))?; self.contexts .lock() .unwrap() .push(JSONContext::new(bracket_type)); Ok(()) } pub fn print_close_bracket(&self) { if !self.json_enabled { return; } let context = self.contexts.lock().unwrap().pop().unwrap(); println_at_output_channel!(self.output_channel => "{}", bracket_type_to_str_close(context.bracket_type)); } pub fn write_close_bracket(&self, f: &mut fmt::Formatter) -> fmt::Result { if !self.json_enabled { return Ok(()); } let context = self.contexts.lock().unwrap().pop().unwrap(); writeln!(f, "{}", bracket_type_to_str_close(context.bracket_type)) } pub fn print_maybe_json(&self, force_quotes: bool, msg: &str) { if self.json_enabled { let mut contexts = self.contexts.lock().unwrap(); let context = contexts.last_mut().unwrap(); let (l, r): (&str, &str) = split_key_val_pair(&msg); print_json_field!(self.output_channel => l, r, force_quotes, context.first_item); context.first_item = false; } else { println_at_output_channel!(self.output_channel => "{}", msg); } } pub fn write_maybe_json( &self, f: &mut fmt::Formatter, force_quotes: bool, msg: &str, ) -> fmt::Result { if self.json_enabled { let mut contexts = self.contexts.lock().unwrap(); let context = contexts.last_mut().unwrap(); let (l, r): (&str, &str) = split_key_val_pair(&msg); write_json_field!(f, l, r, force_quotes, context.first_item)?; context.first_item = false; Ok(()) } else { writeln!(f, "{}", msg) } } }