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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#![cfg_attr(feature = "_doc", feature(doc_cfg, external_doc))]
#![cfg_attr(feature = "_doc", doc(include = "../README.md"))]
#![warn(clippy::all)]
#![allow(dead_code, unused_imports)]
#[macro_use]
extern crate err_derive;
#[macro_use]
extern crate opaque_typedef_macros;
#[macro_use]
extern crate fastobo_derive_internal;
extern crate fastobo_syntax;
#[cfg(feature = "memchr")]
extern crate memchr;
extern crate opaque_typedef;
extern crate ordered_float;
extern crate pest;
#[cfg(test)]
extern crate textwrap_macros;
extern crate url;
#[cfg(feature = "threading")]
extern crate crossbeam_channel;
#[macro_use]
#[cfg(feature = "threading")]
extern crate lazy_static;
#[cfg(feature = "threading")]
extern crate num_cpus;
#[macro_use]
pub mod parser;
pub mod ast;
pub mod error;
pub mod semantics;
pub mod visit;
use std::convert::TryFrom;
use std::fs::File;
use std::io::BufRead;
use std::io::BufReader;
use std::io::Write;
use std::path::Path;
use std::str::FromStr;
use self::ast::OboDoc;
use self::error::Error;
use self::error::Result;
use self::parser::FrameReader;
#[inline]
pub fn from_str<S: AsRef<str>>(src: S) -> Result<OboDoc> {
OboDoc::from_str(src.as_ref()).map_err(Error::from)
}
#[inline]
pub fn from_reader<B: BufRead>(r: B) -> Result<OboDoc> {
OboDoc::try_from(FrameReader::new(r))
}
#[inline]
pub fn from_file<P: AsRef<Path>>(path: P) -> Result<OboDoc> {
let pathref = path.as_ref();
File::open(pathref)
.map(BufReader::new)
.map_err(From::from)
.and_then(from_reader)
.map_err(|e| {
if let Error::SyntaxError { error } = e {
error.with_path(&pathref.to_string_lossy()).into()
} else {
e
}
})
}
#[inline]
pub fn to_writer<W: Write>(mut writer: W, doc: &OboDoc) -> Result<()> {
write!(writer, "{}", doc.header())?;
if !doc.header().is_empty() && !doc.entities().is_empty() {
writeln!(writer)?;
}
for entity in doc.entities() {
write!(writer, "{}", entity)?;
}
Ok(())
}
#[inline]
pub fn to_file<P: AsRef<Path>>(path: P, doc: &OboDoc) -> Result<()> {
File::create(path)
.map_err(From::from)
.and_then(|r| to_writer(r, doc).map_err(From::from))
}