chiark / gitweb /
ini: new module compiles
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Aug 2021 20:03:44 +0000 (21:03 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 7 Aug 2021 20:03:44 +0000 (21:03 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/ini.rs
src/lib.rs

index 107f4aa125ae8e7c1ea9c7487aebc23e4b231153..0d60b79ad59eccd9362122683c8e2a634ddd3b52 100644 (file)
@@ -4,7 +4,9 @@
 
 use crate::prelude::*;
 
+use std::cell::{RefCell, RefMut};
 use std::io::BufRead;
+use std::rc::Rc;
 
 #[derive(Debug,Clone)]
 #[derive(Hash,Eq,PartialEq,Ord,PartialOrd)]
@@ -47,10 +49,14 @@ impl Display for Loc {
   }
 }
 
+
 #[throws(AE)]
-pub fn read(parsed: &mut Parsed, file: &mut dyn BufRead, path_for_loc: &Path) {
+pub fn read(parsed: &mut Parsed, file: &mut dyn BufRead, path_for_loc: &Path)
+//->Result<(), AE>
+{
+  let parsed = Rc::new(RefCell::new(parsed));
   let path: Arc<PathBuf> = path_for_loc.to_owned().into();
-  let mut section: Option<&mut Section> = None;
+  let mut section: Option<RefMut<Section>> = None;
   for (lno, line) in file.lines().enumerate() {
     let line = line.context("read")?;
     let line = line.trim();
@@ -69,15 +75,22 @@ pub fn read(parsed: &mut Parsed, file: &mut dyn BufRead, path_for_loc: &Path) {
         regex_captures!(r#"^ \[ \s* (.+?) \s* \] $"#x, line)
       {
         let new: Arc<String> = new.to_owned().into();
-        section = Some(
-          parsed.entry(new.clone())
+
+        section.take(); // drops previous RefCell borrow of parsed
+
+        let new_section = RefMut::map(parsed.borrow_mut(), |p| {
+
+          p.entry(new.clone())
             .or_insert_with(|| {
               Section {
                 loc: Loc { section: Some(new), file: path.clone(), lno },
                 values: default(),
-              }
+                }
             })
-        );
+
+        });
+
+        section = Some(new_section);
 
       } else if let Some((_, key, val)) =
         regex_captures!(r#"^ ( [^\[] .*? ) \s* = \s* (.*) $"#, line)
index 1327974c7ae46f96d9257597ce949c92a392cd6a..298c1b9689084ec17c97552c7630b09c1fce059d 100644 (file)
@@ -13,4 +13,4 @@ pub mod queue;
 pub mod types;
 pub mod utils;
 
-//mod ini;
+pub mod ini;