| 1 |
#![crate_name = "yaml"] |
| 2 |
#![crate_type = "lib"] |
| 3 |
|
| 4 |
extern crate libc; |
| 5 |
|
| 6 |
extern crate regex; |
| 7 |
|
| 8 |
use std::str; |
| 9 |
use std::ffi::CStr; |
| 10 |
use std::io::Read; |
| 11 |
|
| 12 |
use parser::YamlParser; |
| 13 |
use constructor::{YamlStandardData, YamlStandardConstructor, YamlConstructor}; |
| 14 |
use error::YamlError; |
| 15 |
|
| 16 |
pub mod ffi; |
| 17 |
pub mod error; |
| 18 |
pub mod event; |
| 19 |
pub mod parser; |
| 20 |
pub mod emitter; |
| 21 |
pub mod document; |
| 22 |
pub mod codecs; |
| 23 |
pub mod constructor; |
| 24 |
|
| 25 |
mod type_size; |
| 26 |
|
| 27 |
pub fn version_string() -> String { |
| 28 |
unsafe { |
| 29 |
str::from_utf8(CStr::from_ptr(ffi::yaml_get_version_string()).to_bytes()).unwrap().to_string() |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
pub fn version() -> (isize, isize, isize) { |
| 34 |
let mut c_major: libc::c_int = 0; |
| 35 |
let mut c_minor: libc::c_int = 0; |
| 36 |
let mut c_patch: libc::c_int = 0; |
| 37 |
|
| 38 |
unsafe { |
| 39 |
ffi::yaml_get_version( |
| 40 |
&mut c_major as *mut libc::c_int, |
| 41 |
&mut c_minor as *mut libc::c_int, |
| 42 |
&mut c_patch as *mut libc::c_int |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
(c_major as isize, c_minor as isize, c_patch as isize) |
| 47 |
} |
| 48 |
|
| 49 |
pub fn parse_bytes_utf8(bytes: &[u8]) -> Result<Vec<YamlStandardData>, YamlError> { |
| 50 |
parse_bytes(bytes, ffi::YamlEncoding::YamlUtf8Encoding) |
| 51 |
} |
| 52 |
|
| 53 |
pub fn parse_bytes(bytes: &[u8], encoding: ffi::YamlEncoding) -> Result<Vec<YamlStandardData>, YamlError> { |
| 54 |
let parser = parser::YamlByteParser::init(bytes, encoding); |
| 55 |
let ctor = YamlStandardConstructor::new(); |
| 56 |
|
| 57 |
parser.load().map(|doc_res| |
| 58 |
doc_res.and_then(|doc| ctor.construct(doc.root().unwrap())) |
| 59 |
).collect() |
| 60 |
} |
| 61 |
|
| 62 |
pub fn parse_io_utf8(reader: &mut Read) -> Result<Vec<YamlStandardData>, YamlError> { |
| 63 |
parse_io(reader, ffi::YamlEncoding::YamlUtf8Encoding) |
| 64 |
} |
| 65 |
|
| 66 |
pub fn parse_io(reader: &mut Read, encoding: ffi::YamlEncoding) -> Result<Vec<YamlStandardData>, YamlError> { |
| 67 |
let parser = parser::YamlIoParser::init(reader, encoding); |
| 68 |
let ctor = YamlStandardConstructor::new(); |
| 69 |
|
| 70 |
parser.load().map(|doc_res| |
| 71 |
doc_res.and_then(|doc| ctor.construct(doc.root().unwrap())) |
| 72 |
).collect() |
| 73 |
} |
| 74 |
|
| 75 |
#[cfg(test)] |
| 76 |
mod test { |
| 77 |
use std::mem; |
| 78 |
use std::io::BufReader; |
| 79 |
use constructor::YamlStandardData::*; |
| 80 |
|
| 81 |
#[test] |
| 82 |
fn test_version_string() { |
| 83 |
let vsn = super::version_string(); |
| •84 |
assert!("0.1.4".to_string() <= vsn && vsn < "0.3".to_string()) |
| 85 |
} |
| 86 |
|
| 87 |
#[test] |
| 88 |
fn test_version() { |
| 89 |
let vsn = super::version(); |
| •90 |
assert!((0, 1, 4) <= vsn && vsn < (0, 3, 0)) |
| 91 |
} |
| 92 |
|
| 93 |
#[test] |
| 94 |
fn test_event_size() { |
| 95 |
assert_eq!(super::type_size::YAML_EVENT_T_SIZE, mem::size_of::<super::ffi::yaml_event_t>()) |
| 96 |
} |
| 97 |
|
| 98 |
#[test] |
| 99 |
fn test_parser_size() { |
| 100 |
assert_eq!(super::type_size::YAML_PARSER_T_SIZE, mem::size_of::<super::ffi::yaml_parser_t>()) |
| 101 |
} |
| 102 |
|
| 103 |
#[test] |
| 104 |
fn test_emitter_size() { |
| 105 |
assert_eq!(super::type_size::YAML_EMITTER_T_SIZE, mem::size_of::<super::ffi::yaml_emitter_t>()) |
| 106 |
} |
| 107 |
|
| 108 |
#[test] |
| 109 |
fn test_document_size() { |
| 110 |
assert_eq!(super::type_size::YAML_DOCUMENT_T_SIZE, mem::size_of::<super::ffi::yaml_document_t>()) |
| 111 |
} |
| 112 |
|
| 113 |
#[test] |
| 114 |
fn test_node_size() { |
| 115 |
assert_eq!(super::type_size::YAML_NODE_T_SIZE, mem::size_of::<super::ffi::yaml_node_t>()) |
| 116 |
} |
| 117 |
|
| 118 |
#[test] |
| 119 |
fn test_parse_bytes() { |
| 120 |
let data = "[1, 2, 3]"; |
| 121 |
assert_eq!(Ok(vec![YamlSequence(vec![YamlInteger(1), YamlInteger(2), YamlInteger(3)])]), super::parse_bytes_utf8(data.as_bytes())) |
| 122 |
} |
| 123 |
|
| 124 |
#[test] |
| 125 |
fn test_parse_io() { |
| 126 |
let data = "[1, 2, 3]"; |
| 127 |
let mut reader = BufReader::new(data.as_bytes()); |
| 128 |
assert_eq!(Ok(vec![YamlSequence(vec![YamlInteger(1), YamlInteger(2), YamlInteger(3)])]), super::parse_io_utf8(&mut reader)) |
| 129 |
} |
| 130 |
} |