|
1 | 1 | use serde::{ser, Deserialize, Serialize}; |
2 | 2 | use std::io; |
3 | 3 |
|
| 4 | +use crate::Value; |
4 | 5 | use crate::{ |
5 | 6 | error::{Error, Result}, |
6 | 7 | extensions::Extensions, |
|
56 | 57 | Ok(String::from_utf8(s.output).expect("Ron should be utf-8")) |
57 | 58 | } |
58 | 59 |
|
| 60 | +impl Value { |
| 61 | + pub fn to_string(&self) -> Result<String> { |
| 62 | + let buf = Vec::new(); |
| 63 | + let mut s = Serializer::new(buf, None)?; |
| 64 | + self.enhanced_serialize(&mut s)?; |
| 65 | + Ok(String::from_utf8(s.output).expect("Ron should be utf-8")) |
| 66 | + } |
| 67 | + |
| 68 | + pub fn to_string_pretty(&self, config: PrettyConfig) -> Result<String> { |
| 69 | + let buf = Vec::new(); |
| 70 | + let mut s = Serializer::new(buf, Some(config))?; |
| 71 | + self.enhanced_serialize(&mut s)?; |
| 72 | + Ok(String::from_utf8(s.output).expect("Ron should be utf-8")) |
| 73 | + } |
| 74 | +} |
| 75 | + |
59 | 76 | /// Pretty serializer state |
60 | 77 | struct Pretty { |
61 | 78 | indent: usize, |
@@ -86,7 +103,7 @@ pub struct PrettyConfig { |
86 | 103 | /// Indentation string |
87 | 104 | #[serde(default = "default_indentor")] |
88 | 105 | pub indentor: String, |
89 | | - // Whether to emit struct names |
| 106 | + /// Whether to emit struct names |
90 | 107 | #[serde(default = "default_struct_names")] |
91 | 108 | pub struct_names: bool, |
92 | 109 | /// Separate tuple members with indentation |
@@ -368,6 +385,21 @@ impl<W: io::Write> Serializer<W> { |
368 | 385 | .map(|(pc, _)| pc.struct_names) |
369 | 386 | .unwrap_or(false) |
370 | 387 | } |
| 388 | + |
| 389 | + fn serialize_struct_dyn<'b>(&mut self, name: &'b str, len: usize) -> Result<Compound<W>> { |
| 390 | + if self.struct_names() { |
| 391 | + self.write_identifier(name)?; |
| 392 | + } |
| 393 | + self.output.write_all(b"(")?; |
| 394 | + |
| 395 | + self.is_empty = Some(len == 0); |
| 396 | + self.start_indent()?; |
| 397 | + |
| 398 | + Ok(Compound { |
| 399 | + ser: self, |
| 400 | + state: State::First, |
| 401 | + }) |
| 402 | + } |
371 | 403 | } |
372 | 404 |
|
373 | 405 | impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> { |
@@ -631,18 +663,7 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> { |
631 | 663 | } |
632 | 664 |
|
633 | 665 | fn serialize_struct(self, name: &'static str, len: usize) -> Result<Self::SerializeStruct> { |
634 | | - if self.struct_names() { |
635 | | - self.write_identifier(name)?; |
636 | | - } |
637 | | - self.output.write_all(b"(")?; |
638 | | - |
639 | | - self.is_empty = Some(len == 0); |
640 | | - self.start_indent()?; |
641 | | - |
642 | | - Ok(Compound { |
643 | | - ser: self, |
644 | | - state: State::First, |
645 | | - }) |
| 666 | + self.serialize_struct_dyn(name, len) |
646 | 667 | } |
647 | 668 |
|
648 | 669 | fn serialize_struct_variant( |
@@ -866,6 +887,33 @@ impl<'a, W: io::Write> ser::SerializeMap for Compound<'a, W> { |
866 | 887 | } |
867 | 888 | } |
868 | 889 |
|
| 890 | +impl<'a, W: io::Write> Compound<'a, W> { |
| 891 | + fn serialize_field_dyn(&mut self, key: &str, value: &Value) -> Result<()> { |
| 892 | + if let State::First = self.state { |
| 893 | + self.state = State::Rest; |
| 894 | + } else { |
| 895 | + self.ser.output.write_all(b",")?; |
| 896 | + |
| 897 | + if let Some((ref config, ref pretty)) = self.ser.pretty { |
| 898 | + if pretty.indent <= config.depth_limit { |
| 899 | + self.ser.output.write_all(config.new_line.as_bytes())?; |
| 900 | + } |
| 901 | + } |
| 902 | + } |
| 903 | + self.ser.indent()?; |
| 904 | + self.ser.write_identifier(key)?; |
| 905 | + self.ser.output.write_all(b":")?; |
| 906 | + |
| 907 | + if self.ser.is_pretty() { |
| 908 | + self.ser.output.write_all(b" ")?; |
| 909 | + } |
| 910 | + |
| 911 | + value.serialize(&mut *self.ser)?; |
| 912 | + |
| 913 | + Ok(()) |
| 914 | + } |
| 915 | +} |
| 916 | + |
869 | 917 | impl<'a, W: io::Write> ser::SerializeStruct for Compound<'a, W> { |
870 | 918 | type Error = Error; |
871 | 919 | type Ok = (); |
|
0 commit comments