fastobo_graphs/from_graph/
header.rs1use std::str::FromStr;
2
3use fastobo::ast::HeaderClause;
4use fastobo::ast::HeaderFrame;
5use fastobo::ast::Ident;
6use fastobo::ast::LiteralPropertyValue;
7use fastobo::ast::NaiveDateTime;
8use fastobo::ast::NamespaceIdent;
9use fastobo::ast::PrefixedIdent;
10use fastobo::ast::PropertyValue;
11use fastobo::ast::QuotedString;
12use fastobo::ast::RelationIdent;
13use fastobo::ast::ResourcePropertyValue;
14use fastobo::ast::UnquotedString;
15
16use super::FromGraph;
17use crate::constants::property::dc;
18use crate::constants::property::obo_in_owl;
19use crate::constants::property::rdfs;
20use crate::error::Result;
21use crate::model::BasicPropertyValue;
22use crate::model::Meta;
23
24impl FromGraph<Meta> for HeaderFrame {
25 fn from_graph(meta: Meta) -> Result<Self> {
26 let mut frame = Self::new();
27
28 if let Some(ref def) = meta.definition {}
29
30 if let Some(ref iri) = meta.version {}
31
32 for comment in meta.comments {
34 frame.push(HeaderClause::Remark(Box::new(UnquotedString::new(comment))));
35 }
36 for pv in meta.basic_property_values {
37 frame.push(HeaderClause::from_graph(pv)?);
38 }
39
40 Ok(frame)
43 }
44}
45
46impl FromGraph<BasicPropertyValue> for HeaderClause {
47 fn from_graph(pv: BasicPropertyValue) -> Result<Self> {
48 match pv.pred.as_str() {
49 obo_in_owl::AUTO_GENERATED_BY => Ok(HeaderClause::AutoGeneratedBy(Box::new(
50 UnquotedString::new(pv.val),
51 ))),
52 obo_in_owl::CREATION_DATE | dc::DATE | obo_in_owl::HAS_DATE => {
53 let dt = NaiveDateTime::from_str(&pv.val)?;
54 Ok(HeaderClause::Date(Box::new(dt)))
55 }
56 obo_in_owl::HAS_DEFAULT_NAMESPACE => {
57 let ns_id = NamespaceIdent::from_str(&pv.val)?;
58 Ok(HeaderClause::DefaultNamespace(Box::new(ns_id)))
59 }
60 obo_in_owl::HAS_OBO_FORMAT_VERSION => Ok(HeaderClause::FormatVersion(Box::new(
61 UnquotedString::new(pv.val),
62 ))),
63 obo_in_owl::NAMESPACE_ID_RULE => Ok(HeaderClause::NamespaceIdRule(Box::new(
64 UnquotedString::new(pv.val),
65 ))),
66 obo_in_owl::SAVED_BY => {
67 Ok(HeaderClause::SavedBy(Box::new(UnquotedString::new(pv.val))))
68 }
69 rdfs::COMMENT => Ok(HeaderClause::Remark(Box::new(UnquotedString::new(pv.val)))),
70 other => {
71 let rel = RelationIdent::from_str(&other)?;
72 let pv = match Ident::from_str(&pv.val) {
73 Ok(id) => PropertyValue::from(ResourcePropertyValue::new(rel, id)),
74 Err(_) => PropertyValue::from(LiteralPropertyValue::new(
75 rel,
76 QuotedString::new(pv.val),
77 Ident::from(PrefixedIdent::new("xsd", "string")),
78 )),
79 };
80 Ok(HeaderClause::PropertyValue(Box::new(pv)))
81 }
82 }
83 }
84}