chiark / gitweb /
parse library file
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 8 Sep 2020 23:34:54 +0000 (00:34 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 8 Sep 2020 23:34:54 +0000 (00:34 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/bin/otterlib.rs
src/shapelib.rs

index bd903b6f5032cd70cd4e631b781438176964a599..a8752b0b2fba40e78515585a19df6b4f04c0c72c 100644 (file)
@@ -2,8 +2,7 @@ pub use otter::imports::*;
 
 #[throws(anyhow::Error)]
 fn main(){
-  let mut s = String::new();
-  io::stdin().read_to_string(&mut s)?;
-  let l : shapelib::Library = toml::from_str(&s)?;
+  let f = env::args().nth(1).unwrap();
+  let l = shapelib::Library::load(&f)?;
   dbg!(l);
 }
index 37daf3e90494740834f516763aa1b5c878c1cf82..98502bc1509d8c60d57e8527faa99e521a4344c3 100644 (file)
@@ -12,15 +12,16 @@ pub struct Library {
 
 #[derive(Deserialize,Debug)]
 pub struct Section {
-  pub shape: Box<dyn OutlineSpec>,
+  pub outline: Box<dyn OutlineSpec>,
   pub size: Vec<Coord>,
   pub middle: Option<Vec<f64>>,
   pub category: String,
   pub files: FileList,
-  pub scraper: toml::Value,
+  pub scraper: Option<toml::Value>,
 }
 
 #[derive(Deserialize,Debug)]
+#[serde(try_from="&str")]
 pub struct FileList (Vec<FileEntry>);
 
 #[derive(Deserialize,Debug)]
@@ -29,11 +30,56 @@ pub struct FileEntry {
   pub desc: Html,
 }
 
-#[typetag::deserialize(tag="outline")]
+#[typetag::deserialize]
 pub trait OutlineSpec : Debug {
 }
 
+#[derive(Error,Debug)]
+pub enum LibraryLoadError{ 
+  #[error(transparent)]
+  TomlParseError(#[from] toml::de::Error),
+  #[error("error reading/opening library file: {0}")]
+  FileError(#[from] io::Error),
+  #[error("{:?}",&self)]
+  FilesListLineMissingWhitespace(usize),
+}
+
+impl Library {
+  #[throws(LibraryLoadError)]
+  pub fn load(path: &str) -> Library {
+    let f = File::open(path)?;
+    let mut f = BufReader::new(f);
+    let mut s = String::new();
+    f.read_to_string(&mut s).unwrap();
+    let l : shapelib::Library = toml::from_str(&s)?;
+    l
+  }
+}
+
+type LLE = LibraryLoadError;
+
 #[derive(Deserialize,Debug)]
-pub struct Circle { }
+struct Circle { }
 #[typetag::deserialize]
 impl OutlineSpec for Circle { }
+
+impl TryFrom<&str> for FileList {
+  type Error = LLE;
+  #[throws(LLE)]
+  fn try_from(s: &str) -> FileList {
+    let mut o = Vec::new();
+    for (lno,l) in s.lines().enumerate() {
+      let l = l.trim();
+      if l=="" || l.starts_with("#") { continue }
+      let sp = l.find(|c:char| c.is_ascii_whitespace())
+        .ok_or(LLE::FilesListLineMissingWhitespace(lno))?;
+      let (lhs, rhs) = l.split_at(sp);
+      let rhs = rhs.trim();
+      o.push(FileEntry{
+        filespec: lhs.to_owned(),
+        desc: Html(rhs.to_owned()),
+      });
+    }
+    FileList(o)
+  }
+}