@@ -574,6 +574,14 @@ impl<'w, 'r, W: Write> Serializer<'w, 'r, W> {
574574 self
575575 }
576576
577+ /// Set the level of quoting used when writing texts
578+ ///
579+ /// Default: [`QuoteLevel::Minimal`]
580+ pub fn set_quote_level ( & mut self , level : QuoteLevel ) -> & mut Self {
581+ self . ser . level = level;
582+ self
583+ }
584+
577585 /// Set the indent object for a serializer
578586 pub ( crate ) fn set_indent ( & mut self , indent : Indent < ' r > ) -> & mut Self {
579587 self . ser . indent = indent;
@@ -779,3 +787,99 @@ impl<'w, 'r, W: Write> ser::Serializer for Serializer<'w, 'r, W> {
779787 }
780788 }
781789}
790+
791+ #[ cfg( test) ]
792+ mod quote_level {
793+ use super :: * ;
794+ use pretty_assertions:: assert_eq;
795+ use serde:: Serialize ;
796+
797+ #[ derive( Debug , PartialEq , Serialize ) ]
798+ struct Element ( & ' static str ) ;
799+
800+ #[ derive( Debug , PartialEq , Serialize ) ]
801+ struct Example {
802+ #[ serde( rename = "@attribute" ) ]
803+ attribute : & ' static str ,
804+ element : Element ,
805+ }
806+
807+ #[ test]
808+ fn default_ ( ) {
809+ let example = Example {
810+ attribute : "special chars: &, <, >, \" , '" ,
811+ element : Element ( "special chars: &, <, >, \" , '" ) ,
812+ } ;
813+
814+ let mut buffer = String :: new ( ) ;
815+ let ser = Serializer :: new ( & mut buffer) ;
816+
817+ example. serialize ( ser) . unwrap ( ) ;
818+ assert_eq ! (
819+ buffer,
820+ "<Example attribute=\" special chars: &, <, >, ", '\" >\
821+ <element>special chars: &, <, >, \" , '</element>\
822+ </Example>"
823+ ) ;
824+ }
825+
826+ #[ test]
827+ fn minimal ( ) {
828+ let example = Example {
829+ attribute : "special chars: &, <, >, \" , '" ,
830+ element : Element ( "special chars: &, <, >, \" , '" ) ,
831+ } ;
832+
833+ let mut buffer = String :: new ( ) ;
834+ let mut ser = Serializer :: new ( & mut buffer) ;
835+ ser. set_quote_level ( QuoteLevel :: Minimal ) ;
836+
837+ example. serialize ( ser) . unwrap ( ) ;
838+ assert_eq ! (
839+ buffer,
840+ "<Example attribute=\" special chars: &, <, >, ", '\" >\
841+ <element>special chars: &, <, >, \" , '</element>\
842+ </Example>"
843+ ) ;
844+ }
845+
846+ #[ test]
847+ fn partial ( ) {
848+ let example = Example {
849+ attribute : "special chars: &, <, >, \" , '" ,
850+ element : Element ( "special chars: &, <, >, \" , '" ) ,
851+ } ;
852+
853+ let mut buffer = String :: new ( ) ;
854+ let mut ser = Serializer :: new ( & mut buffer) ;
855+ ser. set_quote_level ( QuoteLevel :: Partial ) ;
856+
857+ example. serialize ( ser) . unwrap ( ) ;
858+ assert_eq ! (
859+ buffer,
860+ "<Example attribute=\" special chars: &, <, >, ", '\" >\
861+ <element>special chars: &, <, >, \" , '</element>\
862+ </Example>"
863+ ) ;
864+ }
865+
866+ #[ test]
867+ fn full ( ) {
868+ let example = Example {
869+ attribute : "special chars: &, <, >, \" , '" ,
870+ element : Element ( "special chars: &, <, >, \" , '" ) ,
871+ } ;
872+
873+ let mut buffer = String :: new ( ) ;
874+ let mut ser = Serializer :: new ( & mut buffer) ;
875+ ser. set_quote_level ( QuoteLevel :: Full ) ;
876+
877+ example. serialize ( ser) . unwrap ( ) ;
878+ assert_eq ! (
879+ buffer,
880+ "<Example attribute=\" special chars: &, <, >, ", '\" >\
881+ <element>special chars: &, <, >, ", '</element>\
882+ </Example>"
883+ ) ;
884+ }
885+ }
0 commit comments