From 598d9956c9117a28983887973a57a099a843f48c Mon Sep 17 00:00:00 2001 From: "radoslaw.kumorek" Date: Thu, 4 Sep 2025 16:38:47 +0200 Subject: [PATCH 1/2] support load_from_stream --- src/ini.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/ini.rs b/src/ini.rs index 4b2afbd..7ab8223 100644 --- a/src/ini.rs +++ b/src/ini.rs @@ -528,6 +528,30 @@ impl Ini { Ok(self.map.clone()) } + pub fn load_from_stream( + &mut self, + mut reader: R, + ) -> Result>>, String> { + let mut buf = String::new(); + if let Err(err) = reader.read_to_string(&mut buf) { + return Err(format!( + "couldn't read from stream: {}", + err + )); + } + + self.map = match self.parse(buf) { + Err(why) => { + return Err(format!( + "couldn't read from stream: {}", + why + )); + } + Ok(map) => map, + }; + Ok(self.map.clone()) + } + ///Loads a file from a defined path, parses it and applies it to the existing hashmap in our struct. ///While `load()` will clear the existing `Map`, `load_and_append()` applies the new values on top of ///the existing hashmap, preserving previous values. From 64e13a2042fe87253d74836a7d41ae4e5cee5910 Mon Sep 17 00:00:00 2001 From: "radoslaw.kumorek" Date: Sun, 7 Sep 2025 15:16:50 +0200 Subject: [PATCH 2/2] documentation --- src/ini.rs | 31 +++++++++++++++++++++++-------- 1 file changed, 23 insertions(+), 8 deletions(-) diff --git a/src/ini.rs b/src/ini.rs index 7ab8223..28c374d 100644 --- a/src/ini.rs +++ b/src/ini.rs @@ -528,24 +528,39 @@ impl Ini { Ok(self.map.clone()) } + /// Loads configuration data from any stream implementing `std::io::Read`, parses it, and applies it to the internal map. + /// + /// This function reads the entire stream into a string, parses it as an INI file, and updates the internal map. + /// On error, returns a descriptive message. The previous map is replaced with the new one. + /// + /// # Arguments + /// * `reader` - Any type that implements `std::io::Read`, such as a file, buffer, or network stream. + /// + /// # Returns + /// * `Result>>, String>` - The parsed map on success, or an error message on failure. + /// + /// # Example + /// ```rust + /// use std::fs::File; + /// let mut config = ConfigParser::new(); + /// let file = File::open("settings.ini").unwrap(); + /// config.load_from_stream(file).unwrap(); + /// + /// let input = "[section]\nkey=value"; + /// config.load_from_stream(input.as_bytes()).unwrap(); + /// ``` pub fn load_from_stream( &mut self, mut reader: R, ) -> Result>>, String> { let mut buf = String::new(); if let Err(err) = reader.read_to_string(&mut buf) { - return Err(format!( - "couldn't read from stream: {}", - err - )); + return Err(format!("couldn't read from stream: {}", err)); } self.map = match self.parse(buf) { Err(why) => { - return Err(format!( - "couldn't read from stream: {}", - why - )); + return Err(format!("couldn't read from stream: {}", why)); } Ok(map) => map, };