Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ pub mod ser;

pub use de::from_str;
pub use error::Error;
pub use ser::to_string;
pub use ser::{to_string, to_string_with_config};

#[cfg(test)]
mod tests {
Expand Down Expand Up @@ -511,6 +511,37 @@ mod tests {
assert!(lines.next().is_none());
}

#[test]
fn test_serialize_no_comments() {
#[derive(Debug, Serialize)]
struct Config {
street_name: Option<String>,
house_number: Option<u16>,
zip_code: Option<u16>,
city: Option<String>,
}

let config = Config {
street_name: Some("Fakestreet".to_string()),
house_number: Some(123),
zip_code: None,
city: None,
};

let ini = to_string_with_config(
&config,
ser::Config {
none_as_comment: false,
},
)
.unwrap();
let mut lines = ini.lines();

assert_eq!(lines.next(), Some("street_name = Fakestreet"));
assert_eq!(lines.next(), Some("house_number = 123"));
assert!(lines.next().is_none());
}

#[test]
fn test_deserialize_nested() {
let ini_str = r#"
Expand Down
71 changes: 51 additions & 20 deletions src/ser.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,58 @@
use crate::{Error, error::Result};
use serde::{Serialize, ser};

#[derive(Default)]
pub struct Serializer {
output: String,
current_section: Option<String>,
section_names: Vec<String>,
config: Config,
}

pub fn to_string<T>(value: &T) -> Result<String>
where
T: Serialize,
{
let mut serializer = Serializer {
output: String::new(),
current_section: None,
section_names: Vec::new(),
};
impl Serializer {
fn with_config(config: Config) -> Self {
Self {
config,
..Default::default()
}
}

fn serialize<T: Serialize>(mut self, value: &T) -> Result<String> {
// First pass: collect all section names
let mut section_collector = SectionCollector {
sections: Vec::new(),
};
value.serialize(&mut section_collector)?;
self.section_names = section_collector.sections;

// Second pass: actual serialization
value.serialize(&mut self)?;
Ok(self.output)
}
}

// First pass: collect all section names
let mut section_collector = SectionCollector {
sections: Vec::new(),
};
value.serialize(&mut section_collector)?;
serializer.section_names = section_collector.sections;
#[derive(Copy, Clone)]
pub struct Config {
/// Serialize `None` values serialized as commented lines
pub none_as_comment: bool,
}

impl Default for Config {
fn default() -> Self {
Self {
none_as_comment: true,
}
}
}

pub fn to_string<T: Serialize>(value: &T) -> Result<String> {
let serializer = Serializer::default();
serializer.serialize(value)
}

// Second pass: actual serialization
value.serialize(&mut serializer)?;
Ok(serializer.output)
pub fn to_string_with_config<T: Serialize>(value: &T, config: Config) -> Result<String> {
let serializer = Serializer::with_config(config);
serializer.serialize(value)
}

// Helper to collect section names
Expand Down Expand Up @@ -844,6 +870,7 @@ impl ser::SerializeStruct for &mut Serializer {
output: String::new(),
current_section: Some(key.to_string()),
section_names: self.section_names.clone(),
config: self.config,
};
value.serialize(&mut nested_serializer)?;

Expand All @@ -855,14 +882,18 @@ impl ser::SerializeStruct for &mut Serializer {
output: String::new(),
current_section: self.current_section.clone(),
section_names: self.section_names.clone(),
config: self.config,
};

match value.serialize(&mut temp_serializer) {
Ok(_) => {
if temp_serializer.output.is_empty() {
// This was None
// Skip commented lines for fields that are section names
if !self.section_names.contains(&key.to_string()) {
// Write out a commented line if configured to do so,
// and the field is not a section name
if self.config.none_as_comment
&& !self.section_names.contains(&key.to_string())
{
self.write_commented_key(key);
}
} else {
Expand Down