|
| 1 | +//! Writer for Java `.properties` files (e.g. ZooKeeper's `zoo.cfg` and |
| 2 | +//! `security.properties`). |
| 3 | +//! |
| 4 | +//! Vendored from the `product-config` crate's `writer` module so the operator no |
| 5 | +//! longer depends on `product-config` for rendering. Backed by the same |
| 6 | +//! `java-properties` crate, so the rendered output is byte-identical to the |
| 7 | +//! previous `product_config::writer::to_java_properties_string`. |
| 8 | +
|
| 9 | +use std::io::Write; |
| 10 | + |
| 11 | +use java_properties::{PropertiesError, PropertiesWriter}; |
| 12 | +use snafu::{ResultExt, Snafu}; |
| 13 | + |
| 14 | +#[derive(Debug, Snafu)] |
| 15 | +pub enum PropertiesWriterError { |
| 16 | + #[snafu(display("failed to create properties file"))] |
| 17 | + Properties { source: PropertiesError }, |
| 18 | + |
| 19 | + #[snafu(display("failed to convert properties file byte array to UTF-8"))] |
| 20 | + FromUtf8 { source: std::string::FromUtf8Error }, |
| 21 | +} |
| 22 | + |
| 23 | +/// Creates a common Java properties file string in the format: |
| 24 | +/// `property_1=value_1\nproperty_2=value_2\n`. |
| 25 | +pub fn to_java_properties_string<'a, T>(properties: T) -> Result<String, PropertiesWriterError> |
| 26 | +where |
| 27 | + T: Iterator<Item = (&'a String, &'a Option<String>)>, |
| 28 | +{ |
| 29 | + let mut output = Vec::new(); |
| 30 | + write_java_properties(&mut output, properties)?; |
| 31 | + String::from_utf8(output).context(FromUtf8Snafu) |
| 32 | +} |
| 33 | + |
| 34 | +/// Writes Java properties to the given writer. A `None` value is written as an |
| 35 | +/// empty value (`key=`). |
| 36 | +fn write_java_properties<'a, W, T>(writer: W, properties: T) -> Result<(), PropertiesWriterError> |
| 37 | +where |
| 38 | + W: Write, |
| 39 | + T: Iterator<Item = (&'a String, &'a Option<String>)>, |
| 40 | +{ |
| 41 | + let mut writer = PropertiesWriter::new(writer); |
| 42 | + for (k, v) in properties { |
| 43 | + let property_value = v.as_deref().unwrap_or_default(); |
| 44 | + writer.write(k, property_value).context(PropertiesSnafu)?; |
| 45 | + } |
| 46 | + writer.flush().context(PropertiesSnafu)?; |
| 47 | + Ok(()) |
| 48 | +} |
| 49 | + |
| 50 | +#[cfg(test)] |
| 51 | +mod tests { |
| 52 | + use std::collections::BTreeMap; |
| 53 | + |
| 54 | + use super::*; |
| 55 | + |
| 56 | + fn props(pairs: &[(&str, Option<&str>)]) -> String { |
| 57 | + let map: BTreeMap<String, Option<String>> = pairs |
| 58 | + .iter() |
| 59 | + .map(|(k, v)| (k.to_string(), v.map(str::to_string))) |
| 60 | + .collect(); |
| 61 | + to_java_properties_string(map.iter()).unwrap() |
| 62 | + } |
| 63 | + |
| 64 | + #[test] |
| 65 | + fn java_properties_renders_key_value() { |
| 66 | + assert_eq!(props(&[("a", Some("1")), ("b", Some("2"))]), "a=1\nb=2\n"); |
| 67 | + } |
| 68 | + |
| 69 | + #[test] |
| 70 | + fn java_properties_renders_none_as_empty() { |
| 71 | + assert_eq!(props(&[("none", None)]), "none=\n"); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn java_properties_escapes_colon_in_value() { |
| 76 | + assert_eq!( |
| 77 | + props(&[("url", Some("file://this/location/file.abc"))]), |
| 78 | + "url=file\\://this/location/file.abc\n" |
| 79 | + ); |
| 80 | + } |
| 81 | +} |
0 commit comments