Skip to content

Commit

Permalink
feat: add new() and new_with_params() impl
Browse files Browse the repository at this point in the history
  • Loading branch information
QEDK committed Nov 20, 2023
1 parent 67a596f commit 4658659
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@ test2.ini
.vscode
output_async.ini
output_sync.ini
pretty_output.ini
pretty_output_async.ini
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,9 @@ fn main() -> Result<(), Box<dyn Error>> {
// You can easily write the currently stored configuration to a file with the `write` method. This creates a compact format with as little spacing as possible:
config.write("output.ini");

// You can write the currently stored configuration with more spacing to a file with the `pretty_write` method. You must supply the method with a configuratio specification:
let mut write_options = WriteOptions::default(); // The defaults match the formatting used in the `write` method
write_options.space_around_delimiters = true;
write_options.multiline_line_indentation = 2;
write_options.blank_lines_between_sections = 1;
// You can write the currently stored configuration with different spacing to a file with the `pretty_write` method:
let write_options = WriteOptions::new_with_params(true, 2, 1);
// or you can use the default configuration as `WriteOptions::new()`
config.pretty_write("pretty_output.ini", &write_options);

// If you want to simply mutate the stored hashmap, you can use get_mut_map()
Expand Down
40 changes: 40 additions & 0 deletions src/ini.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,46 @@ impl Default for WriteOptions {
}
}

impl WriteOptions {
///Creates a new `WriteOptions` object with the default values.
///## Example
///```rust
///use configparser::ini::WriteOptions;
///
///let write_options = WriteOptions::new();
///assert_eq!(write_options.space_around_delimiters, false);
///assert_eq!(write_options.multiline_line_indentation, 4);
///assert_eq!(write_options.blank_lines_between_sections, 0);
///```
///Returns the struct and stores it in the calling variable.
pub fn new() -> WriteOptions {
WriteOptions::default()
}

///Creates a new `WriteOptions` object with the given parameters.
///## Example
///```rust
///use configparser::ini::WriteOptions;
///
///let write_options = WriteOptions::new_with_params(true, 2, 1);
///assert_eq!(write_options.space_around_delimiters, true);
///assert_eq!(write_options.multiline_line_indentation, 2);
///assert_eq!(write_options.blank_lines_between_sections, 1);
///```
///Returns the struct and stores it in the calling variable.
pub fn new_with_params(
space_around_delimiters: bool,
multiline_line_indentation: usize,
blank_lines_between_sections: usize,
) -> WriteOptions {
Self {
space_around_delimiters,
multiline_line_indentation,
blank_lines_between_sections,
}
}
}

#[cfg(windows)]
const LINE_ENDING: &str = "\r\n";
#[cfg(not(windows))]
Expand Down
8 changes: 3 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,9 @@ fn main() -> Result<(), Box<dyn Error>> {
// You can easily write the currently stored configuration to a file with the `write` method. This creates a compact format with as little spacing as possible:
config.write("output.ini");
// You can write the currently stored configuration with more spacing to a file with the `pretty_write` method. You must supply the method with a configuratio specification:
let mut write_options = WriteOptions::default(); // The defaults match the formatting used in the `write` method
write_options.space_around_delimiters = true;
write_options.multiline_line_indentation = 2;
write_options.blank_lines_between_sections = 1;
// You can write the currently stored configuration with different spacing to a file with the `pretty_write` method:
let write_options = WriteOptions::new_with_params(true, 2, 1);
// or you can use the default configuration as `WriteOptions::new()`
config.pretty_write("pretty_output.ini", &write_options);
// If you want to simply mutate the stored hashmap, you can use get_mut_map()
Expand Down

0 comments on commit 4658659

Please sign in to comment.