commit a969fc4a9bf3b2a27fce0cf5e019aeeacdeef68f
parent 486942efb807b7f7d5b1e637f1ecd1d9a2843325
Author: lash <dev@holbrook.no>
Date:   Sat, 25 Jun 2022 11:09:46 +0000
Force valid digest size in metadata init
Diffstat:
4 files changed, 49 insertions(+), 26 deletions(-)
diff --git a/Cargo.toml b/Cargo.toml
@@ -15,16 +15,14 @@ rio_api = "~0.7.1"
 hex = "^0.4"
 mime = "^0.3.13"
 unic-langid-impl = "^0.9.0"
-
-[dependencies.biblatex]
-biblatex = "0.5.0"
-optional = true
+biblatex = "^0.6.2"
+sha2 = "^0.10.2"
 
 #[dependencies.rdf]
 #rio_turtle = "~0.7.1"
 #rio_api = "~0.7.1"
 #optional = true
 
-[features]
+#[features]
 #dump_rdf = ["rdf"]
 #dump_bibtex = ["biblatex"]
diff --git a/src/dc/mod.rs b/src/dc/mod.rs
@@ -1,20 +1,24 @@
 use mime::Mime;
 use unic_langid_impl::LanguageIdentifier;
+use biblatex::EntryType;
+use std::str::FromStr;
 
 
 pub struct DCMetaData {
     pub title: String,
     pub author: String,
+    pub typ: EntryType,
     pub subject: Option<String>,
     pub mime: Option<Mime>,
     pub language: Option<LanguageIdentifier>,
 }
 
 impl DCMetaData {
-    pub fn new(title: &str, author: &str) -> DCMetaData {
+    pub fn new(title: &str, author: &str, typ: EntryType) -> DCMetaData {
         DCMetaData{
             title: String::from(title),
             author: String::from(author),
+            typ: typ,
             subject: None,
             mime: None,
             language: None,
diff --git a/src/meta.rs b/src/meta.rs
@@ -5,12 +5,17 @@ use hex;
 use mime::{
     Mime
 };
+use sha2::{
+    Sha512,
+    Digest,
+};
 use unic_langid_impl::LanguageIdentifier;
+use biblatex::EntryType;
 use std::str::FromStr;
 
 use crate::dc::DCMetaData;
 
-pub type Digest = Vec<u8>;
+//pub type Digest = Vec<u8>;
 
 pub type PublishDate = (u8, u8, u32);
 
@@ -18,18 +23,9 @@ pub type FileName = String;
 
 pub type FilePath = String;
 
-enum ResourceType {
-    Unknown,
-    Article,
-    Whitepaper,
-    Book,
-    Report,
-}
-
 pub struct MetaData {
     dc: DCMetaData,
-    typ: ResourceType,
-    digest: Digest,
+    digest: Vec<u8>,
     local_name: Option<FileName>,
     comment: String,
     publish_date: PublishDate,
@@ -37,14 +33,17 @@ pub struct MetaData {
 }
 
 impl MetaData {
+    pub fn new(title: &str, author: &str, typ: EntryType, digest: Vec<u8>, filename: Option<FileName>) -> MetaData {
+        let dc = DCMetaData::new(title, author, typ);
+
+        let sz = Sha512::output_size();
+        if digest.len() != sz {
+            panic!("wrong digest size, must be {}", sz);
+        }
 
-    pub fn new(title: &str, author: &str, digest: Vec<u8>, filename: Option<FileName>) -> MetaData {
-        let dc = DCMetaData::new(title, author);
- 
         MetaData{
                 dc: dc,
-                typ: ResourceType::Unknown,
-                digest: vec!(),
+                digest: digest,
                 comment: String::new(),
                 //local_name: filepath.to_str().unwrap().to_string(),
                 local_name: filename,
@@ -61,6 +60,10 @@ impl MetaData {
         self.dc.author.clone()
     }
 
+    pub fn typ(&self) -> EntryType {
+        self.dc.typ.clone()
+    }
+
     pub fn set_subject(&mut self, v: &str) {
         self.dc.subject = Some(String::from(v));
     }
@@ -105,7 +108,7 @@ impl MetaData {
 
         let mut title: String = String::new();
         let mut author: String = String::new();
-        //let mut subject: String = String::new();
+        let mut typ: EntryType = EntryType::Unknown(String::new());
         let filename: FileName; 
 
         let title_src = xattr::get(filepath, "user.dcterms:title").unwrap();
@@ -132,7 +135,16 @@ impl MetaData {
             .into_string()
             .unwrap();
 
-        let mut metadata = MetaData::new(title.as_str(), author.as_str(), vec!(), Some(filename));
+        let typ_src = xattr::get(filepath, "user.dcterms:type").unwrap();
+        match typ_src {
+            Some(v) => {
+                let s = std::str::from_utf8(&v).unwrap();
+                typ = EntryType::new(s);
+            },
+            None => {},
+        }
+
+        let mut metadata = MetaData::new(title.as_str(), author.as_str(), typ, vec!(), Some(filename));
 
         match xattr::get(filepath, "user.dcterms:subject") {
             Ok(v) => {
diff --git a/src/rdf.rs b/src/rdf.rs
@@ -36,6 +36,12 @@ pub fn write_rdf(entry: &MetaData, w: impl Write) -> Result<usize, std::io::Erro
         predicate: NamedNode { iri: "https://purl.org/dc/terms/creator" }.into(),
         object: Literal::Simple { value: entry.author().as_str() }.into(),
     });
+    let typ = entry.typ().to_string();
+    tfmt.format(&Triple{
+        subject: urn,
+        predicate: NamedNode { iri: "https://purl.org/dc/terms/type" }.into(),
+        object: Literal::Simple { value: typ.as_str() }.into(),
+    });
     match entry.subject() {
         Some(v) => {
             tfmt.format(&Triple{
@@ -80,16 +86,19 @@ mod tests {
     use super::write_rdf;
     use super::MetaData;
     use std::io::stdout;
+    use std::default::Default;
+    use biblatex::EntryType;
 
     #[test]
     fn test_write() {
-        let mut m = MetaData::new("foo", "bar", vec!(0x2a), None);
+        let mut digest = Vec::with_capacity(64);
+        digest.resize(64, 0x2a);
+        let mut m = MetaData::new("foo", "bar", EntryType::Article, Vec::from(digest), None);
         m.set_subject("baz");
         m.set_mime_str("foo/bar");
         m.set_language("en-US");
         //let v =  Vec::default();
         let v = stdout();
         let r = write_rdf(&m, v);
-        println!("");
     }
 }