Mercurial > core / rust/lib/sxp/src/fmt.rs
changeset 698: |
96958d3eb5b0 |
parent: |
0ccbbd142694
|
author: |
Richard Westhaver <ellis@rwest.io> |
date: |
Fri, 04 Oct 2024 22:04:59 -0400 |
permissions: |
-rw-r--r-- |
description: |
fixes |
1 //! fmt.rs --- SXP Formatters 9 pub fn indent<W: ?Sized + io::Write>( 20 pub trait WriteFormatter { 21 /// Write a 'nil' value to the specified writer. 23 fn write_nil<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> { 24 writer.write_all(b"nil") 26 /// Write a bool value to the specified writer. 28 fn write_bool<W: ?Sized + Write>( 34 writer.write_all(b"t") 36 writer.write_all(b"nil") 39 /// Write an i8 value like '-123' to the specified writer. 41 fn write_i8<W: ?Sized + Write>( 46 let mut buffer = itoa::Buffer::new(); 47 let s = buffer.format(value); 48 writer.write_all(s.as_bytes()) 50 /// Write an i16 value like '-123' to the specified writer. 52 fn write_i16<W: ?Sized + Write>( 57 let mut buffer = itoa::Buffer::new(); 58 let s = buffer.format(value); 59 writer.write_all(s.as_bytes()) 61 /// Write an i32 value like `-123` to the specified writer. 63 fn write_i32<W: ?Sized + Write>( 68 let mut buffer = itoa::Buffer::new(); 69 let s = buffer.format(value); 70 writer.write_all(s.as_bytes()) 72 /// Write an i64 value like `-123` to the specified writer. 74 fn write_i64<W: ?Sized + Write>( 79 let mut buffer = itoa::Buffer::new(); 80 let s = buffer.format(value); 81 writer.write_all(s.as_bytes()) 83 /// Write an i128 value like `-123` to the specified writer. 85 fn write_i128<W: ?Sized + Write>( 90 let mut buffer = itoa::Buffer::new(); 91 let s = buffer.format(value); 92 writer.write_all(s.as_bytes()) 94 /// Write a u8 value like `123` to the specified writer. 96 fn write_u8<W: ?Sized + Write>( 100 ) -> io::Result<()> { 101 let mut buffer = itoa::Buffer::new(); 102 let s = buffer.format(value); 103 writer.write_all(s.as_bytes()) 105 /// Write a u16 value like `123` to the specified writer. 107 fn write_u16<W: ?Sized + Write>( 111 ) -> io::Result<()> { 112 let mut buffer = itoa::Buffer::new(); 113 let s = buffer.format(value); 114 writer.write_all(s.as_bytes()) 116 /// Write a u32 value like `123` to the specified writer. 118 fn write_u32<W: ?Sized + Write>( 122 ) -> io::Result<()> { 123 let mut buffer = itoa::Buffer::new(); 124 let s = buffer.format(value); 125 writer.write_all(s.as_bytes()) 127 /// Write a u64 value like `123` to the specified writer. 129 fn write_u64<W: ?Sized + Write>( 133 ) -> io::Result<()> { 134 let mut buffer = itoa::Buffer::new(); 135 let s = buffer.format(value); 136 writer.write_all(s.as_bytes()) 138 /// Write a u128 value like `123` to the specified writer. 140 fn write_u128<W: ?Sized + Write>( 144 ) -> io::Result<()> { 145 let mut buffer = itoa::Buffer::new(); 146 let s = buffer.format(value); 147 writer.write_all(s.as_bytes()) 149 /// Write a f32 value like `-31.26e+12` to the specified writer. 151 fn write_f32<W: ?Sized + Write>( 155 ) -> io::Result<()> { 156 let mut buffer = ryu::Buffer::new(); 157 let s = buffer.format_finite(value); 158 writer.write_all(s.as_bytes()) 160 /// Write a f64 value like `-31.26e+12` to the specified writer. 162 fn write_f64<W: ?Sized + Write>( 166 ) -> io::Result<()> { 167 let mut buffer = ryu::Buffer::new(); 168 let s = buffer.format_finite(value); 169 writer.write_all(s.as_bytes()) 171 /// Write a number that has already been rendered to a string. 173 fn write_number_str<W: ?Sized + Write>( 177 ) -> io::Result<()> { 178 writer.write_all(value.as_bytes()) 180 /// Called before each series of `write_string_fragment` and 181 /// `write_char_escape`. Writes a `"` to the specified writer. 183 fn begin_string<W: ?Sized + Write>( 186 ) -> io::Result<()> { 187 writer.write_all(b"\"") 189 /// Called after each series of `write_string_fragment` and 190 /// `write_char_escape`. Writes a `"` to the specified writer. 192 fn end_string<W: ?Sized + Write>( 195 ) -> io::Result<()> { 196 writer.write_all(b"\"") 198 /// Writes a string fragment that doesn't need any escaping to the 199 /// specified writer. 201 fn write_string_fragment<W: ?Sized + Write>( 205 ) -> io::Result<()> { 206 writer.write_all(fragment.as_bytes()) 208 /// Writes a character escape code to the specified writer. 210 fn write_char_escape<W: ?Sized + Write>( 213 char_escape: CharEscape, 214 ) -> io::Result<()> { 215 use self::CharEscape::*; 216 let s = match char_escape { 218 ReverseSolidus => b"\\\\", 223 CarriageReturn => b"\\r", 225 AsciiControl(byte) => { 226 static HEX_DIGITS: [u8; 16] = *b"0123456789abcdef"; 232 HEX_DIGITS[(byte >> 4) as usize], 233 HEX_DIGITS[(byte & 0xF) as usize], 235 return writer.write_all(bytes); 241 fn write_symbol<W: ?Sized + Write>( 245 ) -> io::Result<()> { 246 writer.write_all(sym.as_bytes()) 248 /// Called before every list. Writes a `(` to the specified 251 fn begin_list<W: ?Sized + Write>( 254 ) -> io::Result<()> { 255 writer.write_all(b"(") 257 /// Called after every list. Writes a `)` to the specified 260 fn end_list<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> { 261 writer.write_all(b")") 263 /// Called before every list element. Writes a ` ` if needed to the 264 /// specified writer. 266 fn begin_list_element<W: ?Sized + Write>( 270 ) -> io::Result<()> { 274 writer.write_all(b" ") 277 /// Called after every list element. 279 fn end_list_element<W: ?Sized + Write>( 282 ) -> io::Result<()> { 285 /// Called before every slot identifier. Writes a `:` to the 286 /// specified writer. 288 fn begin_key<W: ?Sized + Write>(&mut self, writer: &mut W) -> io::Result<()> { 289 writer.write_all(b":") 292 /// Writes a raw JSON fragment that doesn't need any escaping to the 293 /// specified writer. 295 fn write_raw_fragment<W: ?Sized + Write>( 299 ) -> io::Result<()> { 300 writer.write_all(fragment.as_bytes()) 304 /// The ReadFormatter is the sister interface to WriteFormatter. 305 pub trait ReadFormatter<'r>: Read<'r> { 307 fn peek<R: ?Sized + Read<'r>>( 310 ) -> Result<Option<u8>> { 314 // /// Read a 'nil' value from the specified reader. 316 // fn read_nil<R:?Sized+Read<'r>>(&mut self, reader: &'r R) -> io::Result<()> 321 pub trait Formatter<'r>: ReadFormatter<'r> + WriteFormatter {} 322 impl<'r, T: ReadFormatter<'r> + WriteFormatter> Formatter<'r> for T {} 324 pub struct DefaultFormatter; 325 // impl<'r> ReadFormatter<'r> for DefaultFormatter {} 326 impl WriteFormatter for DefaultFormatter {} 328 pub struct BinaryFormatter; 329 // impl<'r> ReadFormatter<'r> for BinaryFormatter {} 330 impl WriteFormatter for BinaryFormatter {} 332 pub struct PrettyFormatter; 333 // impl<'r> ReadFormatter<'r> for PrettyFormatter {} 334 impl WriteFormatter for PrettyFormatter {}