1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#![cfg_attr(feature = "_doc", feature(doc_cfg, external_doc))]
#![cfg_attr(feature = "_doc", doc(include = "../README.md"))]
#![warn(clippy::all)]
#![recursion_limit = "256"]
#![allow(unused_variables, dead_code)]

#[macro_use]
extern crate thiserror;
#[cfg(feature = "obo")]
#[macro_use]
extern crate mashup;
#[cfg(feature = "obo")]
extern crate fastobo;
extern crate serde;
extern crate serde_json;
extern crate serde_yaml;

pub mod constants;
pub mod error;
#[cfg(feature = "obo")]
mod from_graph;
#[cfg(feature = "obo")]
mod into_graph;
pub mod model;
mod utils;

use std::fs::File;
use std::io::Read;
use std::io::Write;
use std::path::Path;

use self::error::Result;
#[cfg(feature = "obo")]
pub use self::from_graph::FromGraph;
#[cfg(feature = "obo")]
pub use self::into_graph::IntoGraph;
use self::model::GraphDocument;

// ---------------------------------------------------------------------------

/// Read an OBO graph from a string containing a JSON or YAML serialization.
#[inline]
pub fn from_str<S: AsRef<str>>(src: S) -> Result<GraphDocument> {
    serde_yaml::from_str::<model::GraphDocument>(src.as_ref()).map_err(From::from)
}

/// Read an OBO graph from a `Read` implementor.
#[inline]
pub fn from_reader<R: Read>(r: R) -> Result<GraphDocument> {
    serde_yaml::from_reader::<R, model::GraphDocument>(r).map_err(From::from)
}

/// Read an OBO graph from a file on the local filesystem.
#[inline]
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<GraphDocument> {
    File::open(path)
        .map_err(From::from)
        .and_then(|r| serde_yaml::from_reader(r).map_err(From::from))
}

// ---------------------------------------------------------------------------

/// Write an OBO graph to a string.
#[inline]
pub fn to_string(g: &GraphDocument) -> Result<String> {
    serde_json::to_string(g).map_err(From::from)
}

/// Write an OBO graph to a `Write` implementor.
#[inline]
pub fn to_writer<W: Write>(w: W, g: &GraphDocument) -> Result<()> {
    serde_json::to_writer(w, g).map_err(From::from)
}

/// Write an OBO graph to a file on the local filesystem.
#[inline]
pub fn to_file<P: AsRef<Path>>(path: P, g: &GraphDocument) -> Result<()> {
    File::create(path)
        .map_err(From::from)
        .and_then(|w| to_writer(w, g))
}