@@ -19,43 +19,37 @@ pub enum PropertiesWriterError {
1919/// `property_1=value_1\nproperty_2=value_2\n`.
2020pub fn to_java_properties_string < ' a , T > ( properties : T ) -> Result < String , PropertiesWriterError >
2121where
22- T : Iterator < Item = ( & ' a String , & ' a Option < String > ) > ,
22+ T : Iterator < Item = ( & ' a String , & ' a String ) > ,
2323{
2424 let mut output = Vec :: new ( ) ;
2525 write_java_properties ( & mut output, properties) ?;
2626 String :: from_utf8 ( output) . context ( FromUtf8Snafu )
2727}
2828
29- /// Writes Java properties to the given writer. A `None` value is written as an
30- /// empty value (`key=`).
29+ /// Writes Java properties to the given writer.
3130fn write_java_properties < ' a , W , T > ( writer : W , properties : T ) -> Result < ( ) , PropertiesWriterError >
3231where
3332 W : Write ,
34- T : Iterator < Item = ( & ' a String , & ' a Option < String > ) > ,
33+ T : Iterator < Item = ( & ' a String , & ' a String ) > ,
3534{
3635 let mut writer = PropertiesWriter :: new ( writer) ;
3736 for ( k, v) in properties {
38- let property_value = v. as_deref ( ) . unwrap_or_default ( ) ;
39- writer. write ( k, property_value) . context ( PropertiesSnafu ) ?;
37+ writer. write ( k, v) . context ( PropertiesSnafu ) ?;
4038 }
4139 writer. flush ( ) . context ( PropertiesSnafu ) ?;
4240 Ok ( ( ) )
4341}
4442
4543/// Converts properties into a Hadoop configuration XML, including the wrapping
46- /// `<configuration>...</configuration>` elements. Properties with a `None` value
47- /// are skipped. Keys and values are XML-escaped.
44+ /// `<configuration>...</configuration>` elements.
4845pub fn to_hadoop_xml < ' a , T > ( properties : T ) -> String
4946where
50- T : Iterator < Item = ( & ' a String , & ' a Option < String > ) > ,
47+ T : Iterator < Item = ( & ' a String , & ' a String ) > ,
5148{
5249 let mut snippet = String :: new ( ) ;
5350 for ( k, v) in properties {
54- let escaped_value = match v {
55- Some ( value) => escape_str_attribute ( value) ,
56- None => continue ,
57- } ;
5851 let escaped_key = escape_str_attribute ( k) ;
52+ let escaped_value = escape_str_attribute ( v) ;
5953 write ! (
6054 snippet,
6155 " <property>\n <name>{escaped_key}</name>\n <value>{escaped_value}</value>\n </property>\n "
@@ -71,18 +65,18 @@ mod tests {
7165
7266 use super :: * ;
7367
74- fn xml ( pairs : & [ ( & str , Option < & str > ) ] ) -> String {
75- let map: BTreeMap < String , Option < String > > = pairs
68+ fn xml ( pairs : & [ ( & str , & str ) ] ) -> String {
69+ let map: BTreeMap < String , String > = pairs
7670 . iter ( )
77- . map ( |( k, v) | ( k. to_string ( ) , v. map ( str :: to_string) ) )
71+ . map ( |( k, v) | ( k. to_string ( ) , v. to_string ( ) ) )
7872 . collect ( ) ;
7973 to_hadoop_xml ( map. iter ( ) )
8074 }
8175
82- fn props ( pairs : & [ ( & str , Option < & str > ) ] ) -> String {
83- let map: BTreeMap < String , Option < String > > = pairs
76+ fn props ( pairs : & [ ( & str , & str ) ] ) -> String {
77+ let map: BTreeMap < String , String > = pairs
8478 . iter ( )
85- . map ( |( k, v) | ( k. to_string ( ) , v. map ( str :: to_string) ) )
79+ . map ( |( k, v) | ( k. to_string ( ) , v. to_string ( ) ) )
8680 . collect ( ) ;
8781 to_java_properties_string ( map. iter ( ) ) . unwrap ( )
8882 }
@@ -98,26 +92,16 @@ mod tests {
9892 #[ test]
9993 fn hadoop_xml_renders_single_property ( ) {
10094 assert_eq ! (
101- xml( & [ ( "fs.defaultFS" , Some ( "hdfs://hdfs/" ) ) ] ) ,
95+ xml( & [ ( "fs.defaultFS" , "hdfs://hdfs/" ) ] ) ,
10296 "<?xml version=\" 1.0\" ?>\n <configuration>\n \
10397 <property>\n <name>fs.defaultFS</name>\n \
10498 <value>hdfs://hdfs/</value>\n </property>\n </configuration>"
10599 ) ;
106100 }
107101
108- #[ test]
109- fn hadoop_xml_skips_none_values ( ) {
110- assert_eq ! (
111- xml( & [ ( "kept" , Some ( "1" ) ) , ( "dropped" , None ) ] ) ,
112- "<?xml version=\" 1.0\" ?>\n <configuration>\n \
113- <property>\n <name>kept</name>\n \
114- <value>1</value>\n </property>\n </configuration>"
115- ) ;
116- }
117-
118102 #[ test]
119103 fn hadoop_xml_escapes_special_characters ( ) {
120- let rendered = xml ( & [ ( "k" , Some ( "<a>&b" ) ) ] ) ;
104+ let rendered = xml ( & [ ( "k" , "<a>&b" ) ] ) ;
121105 assert ! (
122106 rendered. contains( "<value><a>&b</value>" ) ,
123107 "{rendered}"
@@ -126,18 +110,18 @@ mod tests {
126110
127111 #[ test]
128112 fn java_properties_renders_key_value ( ) {
129- assert_eq ! ( props( & [ ( "a" , Some ( "1" ) ) , ( "b" , Some ( "2" ) ) ] ) , "a=1\n b=2\n " ) ;
113+ assert_eq ! ( props( & [ ( "a" , "1" ) , ( "b" , "2" ) ] ) , "a=1\n b=2\n " ) ;
130114 }
131115
132116 #[ test]
133- fn java_properties_renders_none_as_empty ( ) {
134- assert_eq ! ( props( & [ ( "none " , None ) ] ) , "none =\n " ) ;
117+ fn java_properties_renders_empty ( ) {
118+ assert_eq ! ( props( & [ ( "empty " , "" ) ] ) , "empty =\n " ) ;
135119 }
136120
137121 #[ test]
138122 fn java_properties_escapes_colon_in_value ( ) {
139123 assert_eq ! (
140- props( & [ ( "url" , Some ( "file://this/location/file.abc" ) ) ] ) ,
124+ props( & [ ( "url" , "file://this/location/file.abc" ) ] ) ,
141125 "url=file\\ ://this/location/file.abc\n "
142126 ) ;
143127 }
0 commit comments