| 1 | use ffi; |
| 2 | use error::YamlError; |
| 3 | use event::{YamlVersionDirective, YamlTagDirective}; |
| 4 | |
| 5 | use std::str; |
| 6 | use std::slice; |
| 7 | use std::ptr; |
| 8 | use std::mem; |
| 9 | use std::ffi::{CStr, CString}; |
| 10 | use std::io; |
| 11 | use std::io::Write; |
| 12 | use libc; |
| 13 | |
| 14 | pub struct YamlBaseEmitter { |
| 15 | emitter_mem: ffi::yaml_emitter_t |
| 16 | } |
| 17 | |
| 18 | impl YamlBaseEmitter { |
| 19 | unsafe fn new() -> YamlBaseEmitter { |
| 20 | YamlBaseEmitter { |
| 21 | emitter_mem: mem::uninitialized() |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl Drop for YamlBaseEmitter { |
| 27 | fn drop(&mut self) { |
| 28 | unsafe { |
| 29 | ffi::yaml_emitter_delete(&mut self.emitter_mem); |
| 30 | } |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | pub struct YamlEmitter<'r> { |
| 35 | base_emitter: YamlBaseEmitter, |
| 36 | writer: &'r mut (Write+'r), |
| 37 | io_error: Option<io::Error>, |
| 38 | } |
| 39 | |
| 40 | fn to_c_str_opt(s: Option<&str>) -> Result<Option<CString>, YamlError> { |
| 41 | match s { |
| 42 | None => Ok(None), |
| 43 | Some(s) => match CString::new(s.as_bytes()) { |
| 44 | Ok(cstr) => Ok(Some(cstr)), |
| 45 | Err(_) => Err(YamlError::new( |
| 46 | ffi::YamlErrorType::YAML_EMITTER_ERROR, |
| 47 | Some("Nul bytes in string".to_string()) |
| 48 | )) |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | fn to_c_str(s: &str) -> Result<CString, YamlError> { |
| 54 | match CString::new(s.as_bytes()) { |
| 55 | Ok(cstr) => Ok(cstr), |
| 56 | Err(_) => Err(YamlError::new( |
| 57 | ffi::YamlErrorType::YAML_EMITTER_ERROR, |
| 58 | Some("Nul bytes in string".to_string()) |
| 59 | )) |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | impl<'r> YamlEmitter<'r> { |
| 64 | pub fn init<'a>(writer: &'a mut Write) -> Box<YamlEmitter<'a>> { |
| 65 | unsafe { |
| 66 | let mut emitter = Box::new(YamlEmitter { |
| 67 | base_emitter: YamlBaseEmitter::new(), |
| 68 | writer: writer, |
| 69 | io_error: None |
| 70 | }); |
| 71 | |
| 72 | if ffi::yaml_emitter_initialize(&mut emitter.base_emitter.emitter_mem) == 0 { |
| 73 | panic!("failed to initialize yaml_emitter_t"); |
| 74 | } |
| 75 | |
| 76 | ffi::yaml_emitter_set_output(&mut emitter.base_emitter.emitter_mem, handle_writer_cb, mem::transmute(&mut *emitter)); |
| 77 | |
| 78 | emitter |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn get_error(&mut self) -> YamlError { |
| 83 | let emitter_mem = &self.base_emitter.emitter_mem; |
| 84 | unsafe { |
| 85 | let c_problem = CStr::from_ptr(emitter_mem.problem); |
| 86 | let mut error = YamlError { |
| 87 | kind: emitter_mem.error, |
| 88 | problem: str::from_utf8(c_problem.to_bytes()).map(|s| s.to_string()).ok(), |
| 89 | io_error: None, |
| 90 | context: None |
| 91 | }; |
| 92 | |
| 93 | mem::swap(&mut self.io_error, &mut error.io_error); |
| 94 | |
| 95 | return error; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | pub fn emit_stream<F>(&mut self, encoding: ffi::YamlEncoding, f: F) -> Result<(), YamlError> |
| 100 | where F: Fn(&mut YamlEmitter) -> Result<(), YamlError> |
| 101 | { |
| 102 | try!(self.emit_stream_start_event(encoding)); |
| 103 | try!(f(self)); |
| 104 | try!(self.emit_stream_end_event()); |
| 105 | self.flush() |
| 106 | } |
| 107 | |
| 108 | fn emit_stream_start_event(&mut self, encoding: ffi::YamlEncoding) -> Result<(), YamlError> { |
| 109 | unsafe { |
| 110 | let mut event = mem::uninitialized(); |
| 111 | |
| 112 | if ffi::yaml_stream_start_event_initialize(&mut event, encoding) == 0 { |
| 113 | panic!("yaml_stream_start_event_initialize failed!"); |
| 114 | } |
| 115 | |
| 116 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 117 | Ok(()) |
| 118 | } else { |
| 119 | Err(self.get_error()) |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn emit_stream_end_event(&mut self) -> Result<(), YamlError> { |
| 125 | unsafe { |
| 126 | let mut event = mem::uninitialized(); |
| 127 | |
| 128 | if ffi::yaml_stream_end_event_initialize(&mut event) == 0 { |
| 129 | panic!("yaml_stream_end_event_initialize failed!"); |
| 130 | } |
| 131 | |
| 132 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 133 | Ok(()) |
| 134 | } else { |
| 135 | Err(self.get_error()) |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | pub fn emit_document<F>(&mut self, |
| 141 | version_directive: Option<YamlVersionDirective>, |
| 142 | tag_directives: &[YamlTagDirective], |
| 143 | implicit: bool, |
| 144 | f: F) -> Result<(), YamlError> where |
| 145 | F: Fn(&mut YamlEmitter) -> Result<(), YamlError> |
| 146 | { |
| 147 | try!(self.emit_document_start_event(version_directive, tag_directives, implicit)); |
| 148 | try!(f(self)); |
| 149 | self.emit_document_end_event(implicit) |
| 150 | } |
| 151 | |
| 152 | fn emit_document_start_event(&mut self, |
| 153 | version_directive: Option<YamlVersionDirective>, |
| 154 | tag_directives: &[YamlTagDirective], |
| 155 | implicit: bool) |
| 156 | -> Result<(), YamlError> |
| 157 | { |
| 158 | let mut vsn_dir = ffi::yaml_version_directive_t { major: 0, minor: 0 }; |
| 159 | let c_vsn_dir = match version_directive { |
| 160 | Some(directive) => { |
| 161 | vsn_dir.major = directive.major as libc::c_int; |
| 162 | vsn_dir.minor = directive.minor as libc::c_int; |
| 163 | &vsn_dir as *const ffi::yaml_version_directive_t |
| 164 | }, |
| 165 | None => ptr::null() |
| 166 | }; |
| 167 | |
| 168 | let c_tag_dirs: Vec<ffi::yaml_tag_directive_t> = |
| 169 | match tag_directives.iter().map(|tag| tag.to_tag_directive_t()).collect() { |
| 170 | Ok(dirs) => dirs, |
| 171 | Err(_) => return Err(YamlError::new( |
| 172 | ffi::YamlErrorType::YAML_EMITTER_ERROR, |
| 173 | Some("Nul bytes in tag directives".to_string()) |
| 174 | )) |
| 175 | }; |
| 176 | let tag_dir_start = c_tag_dirs.as_ptr(); |
| 177 | unsafe { |
| 178 | let mut event = mem::uninitialized(); |
| 179 | let tag_dir_end = tag_dir_start.offset(c_tag_dirs.len() as isize); |
| 180 | let c_implicit = if implicit { 1 } else { 0 }; |
| 181 | |
| 182 | if ffi::yaml_document_start_event_initialize(&mut event, c_vsn_dir, tag_dir_start, tag_dir_end, c_implicit) == 0 { |
| 183 | panic!("yaml_document_start_event_initialize failed!"); |
| 184 | } |
| 185 | |
| 186 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 187 | Ok(()) |
| 188 | } else { |
| 189 | Err(self.get_error()) |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | fn emit_document_end_event(&mut self, implicit: bool) -> Result<(), YamlError> { |
| 195 | let c_implicit = if implicit { 1 } else { 0 }; |
| 196 | unsafe { |
| 197 | let mut event = mem::uninitialized(); |
| 198 | |
| 199 | if ffi::yaml_document_end_event_initialize(&mut event, c_implicit) == 0 { |
| 200 | panic!("yaml_stream_end_event_initialize failed!"); |
| 201 | } |
| 202 | |
| 203 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 204 | Ok(()) |
| 205 | } else { |
| 206 | Err(self.get_error()) |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | pub fn emit_alias_event(&mut self, anchor: &str) -> Result<(), YamlError> { |
| 212 | let c_anchor = try!(to_c_str(anchor)); |
| 213 | |
| 214 | unsafe { |
| 215 | let mut event = mem::uninitialized(); |
| 216 | |
| 217 | let ptr = c_anchor.as_ptr(); |
| 218 | if ffi::yaml_alias_event_initialize(&mut event, ptr as *const ffi::yaml_char_t) != 0 { |
| 219 | panic!("yaml_stream_end_event_initialize failed!") |
| 220 | } |
| 221 | |
| 222 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 223 | Ok(()) |
| 224 | } else { |
| 225 | Err(self.get_error()) |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | pub fn emit_scalar_event(&mut self, anchor: Option<&str>, tag: Option<&str>, |
| 231 | value: &str, plain_implicit: bool, quoted_implicit: bool, |
| 232 | style: ffi::YamlScalarStyle) -> Result<(), YamlError> |
| 233 | { |
| 234 | let c_anchor = try!(to_c_str_opt(anchor)); |
| 235 | let anchor_ptr = match c_anchor { |
| 236 | Some(s) => s.as_ptr(), |
| 237 | None => ptr::null() |
| 238 | }; |
| 239 | let c_tag = try!(to_c_str_opt(tag)); |
| 240 | let tag_ptr = match c_tag { |
| 241 | Some(s) => s.as_ptr(), |
| 242 | None => ptr::null() |
| 243 | }; |
| 244 | let c_plain_implicit = if plain_implicit { 1 } else { 0 }; |
| 245 | let c_quoted_implicit = if quoted_implicit { 1 } else { 0 }; |
| 246 | |
| 247 | unsafe { |
| 248 | let mut event = mem::uninitialized(); |
| 249 | |
| 250 | if ffi::yaml_scalar_event_initialize(&mut event, |
| 251 | anchor_ptr as *const ffi::yaml_char_t, tag_ptr as *const ffi::yaml_char_t, |
| 252 | value.as_ptr(), value.len() as libc::c_int, |
| 253 | c_plain_implicit, c_quoted_implicit, |
| 254 | style) == 0 |
| 255 | { |
| 256 | panic!("yaml_scalar_event_initialize failed!"); |
| 257 | } |
| 258 | |
| 259 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 260 | Ok(()) |
| 261 | } else { |
| 262 | Err(self.get_error()) |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | pub fn emit_sequence<F>(&mut self, anchor: Option<&str>, tag: Option<&str>, implicit: bool, |
| 268 | style: ffi::YamlSequenceStyle, |
| 269 | f: F) -> Result<(), YamlError> where |
| 270 | F: Fn(&mut YamlEmitter) -> Result<(), YamlError> |
| 271 | { |
| 272 | try!(self.emit_sequence_start_event(anchor, tag, implicit, style)); |
| 273 | try!(f(self)); |
| 274 | self.emit_sequence_end_event() |
| 275 | } |
| 276 | |
| 277 | fn emit_sequence_start_event(&mut self, anchor: Option<&str>, tag: Option<&str>, implicit: bool, |
| 278 | style: ffi::YamlSequenceStyle) -> Result<(), YamlError> |
| 279 | { |
| 280 | let c_anchor = try!(to_c_str_opt(anchor)); |
| 281 | let anchor_ptr = match c_anchor { |
| 282 | Some(s) => s.as_ptr(), |
| 283 | None => ptr::null() |
| 284 | }; |
| 285 | let c_tag = try!(to_c_str_opt(tag)); |
| 286 | let tag_ptr = match c_tag { |
| 287 | Some(s) => s.as_ptr(), |
| 288 | None => ptr::null() |
| 289 | }; |
| 290 | let c_implicit = if implicit { 1 } else { 0 }; |
| 291 | |
| 292 | unsafe { |
| 293 | let mut event = mem::uninitialized(); |
| 294 | |
| 295 | if ffi::yaml_sequence_start_event_initialize(&mut event, |
| 296 | anchor_ptr as *const ffi::yaml_char_t, tag_ptr as *const ffi::yaml_char_t, |
| 297 | c_implicit, style) == 0 |
| 298 | { |
| 299 | panic!("yaml_sequence_start_event_initialize failed!"); |
| 300 | } |
| 301 | |
| 302 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 303 | Ok(()) |
| 304 | } else { |
| 305 | Err(self.get_error()) |
| 306 | } |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | fn emit_sequence_end_event(&mut self) -> Result<(), YamlError> { |
| 311 | unsafe { |
| 312 | let mut event = mem::uninitialized(); |
| 313 | |
| 314 | if ffi::yaml_sequence_end_event_initialize(&mut event) == 0 { |
| 315 | panic!("yaml_sequence_end_event_initialize failed!"); |
| 316 | } |
| 317 | |
| 318 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 319 | Ok(()) |
| 320 | } else { |
| 321 | Err(self.get_error()) |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | pub fn emit_mapping<F>(&mut self, anchor: Option<&str>, tag: Option<&str>, implicit: bool, |
| 327 | style: ffi::YamlSequenceStyle, |
| 328 | f: F) -> Result<(), YamlError> where |
| 329 | F: Fn(&mut YamlEmitter) -> Result<(), YamlError> |
| 330 | { |
| 331 | try!(self.emit_mapping_start_event(anchor, tag, implicit, style)); |
| 332 | try!(f(self)); |
| 333 | self.emit_mapping_end_event() |
| 334 | } |
| 335 | |
| 336 | fn emit_mapping_start_event(&mut self, anchor: Option<&str>, tag: Option<&str>, implicit: bool, |
| 337 | style: ffi::YamlSequenceStyle) -> Result<(), YamlError> |
| 338 | { |
| 339 | let c_anchor = try!(to_c_str_opt(anchor)); |
| 340 | let anchor_ptr = match c_anchor { |
| 341 | Some(s) => s.as_ptr(), |
| 342 | None => ptr::null() |
| 343 | }; |
| 344 | let c_tag = try!(to_c_str_opt(tag)); |
| 345 | let tag_ptr = match c_tag { |
| 346 | Some(s) => s.as_ptr(), |
| 347 | None => ptr::null() |
| 348 | }; |
| 349 | let c_implicit = if implicit { 1 } else { 0 }; |
| 350 | |
| 351 | unsafe { |
| 352 | let mut event = mem::uninitialized(); |
| 353 | |
| 354 | if ffi::yaml_mapping_start_event_initialize(&mut event, |
| 355 | anchor_ptr as *const ffi::yaml_char_t, tag_ptr as *const ffi::yaml_char_t, |
| 356 | c_implicit, style) == 0 |
| 357 | { |
| 358 | panic!("yaml_mapping_start_event_initialize failed!"); |
| 359 | } |
| 360 | |
| 361 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 362 | Ok(()) |
| 363 | } else { |
| 364 | Err(self.get_error()) |
| 365 | } |
| 366 | } |
| 367 | } |
| 368 | |
| 369 | fn emit_mapping_end_event(&mut self) -> Result<(), YamlError> { |
| 370 | unsafe { |
| 371 | let mut event = mem::uninitialized(); |
| 372 | |
| 373 | if ffi::yaml_mapping_end_event_initialize(&mut event) == 0 { |
| 374 | panic!("yaml_mapping_end_event_initialize failed!"); |
| 375 | } |
| 376 | |
| 377 | if ffi::yaml_emitter_emit(&mut self.base_emitter.emitter_mem, &mut event) != 0 { |
| 378 | Ok(()) |
| 379 | } else { |
| 380 | Err(self.get_error()) |
| 381 | } |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | pub fn flush(&mut self) -> Result<(), YamlError> { |
| 386 | unsafe { |
| 387 | if ffi::yaml_emitter_flush(&mut self.base_emitter.emitter_mem) != 0 { |
| 388 | Ok(()) |
| 389 | } else { |
| 390 | Err(self.get_error()) |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | extern fn handle_writer_cb(data: *mut YamlEmitter, buffer: *const u8, size: libc::size_t) -> libc::c_int { |
| 397 | unsafe { |
| 398 | let buf = slice::from_raw_parts(buffer, size as usize); |
| 399 | let emitter = &mut *data; |
| 400 | match emitter.writer.write_all(buf) { |
| 401 | Ok(()) => 1, |
| 402 | Err(err) => { |
| 403 | emitter.io_error = Some(err); |
| 404 | 0 |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | #[cfg(test)] |
| 411 | mod test { |
| 412 | use emitter::YamlEmitter; |
| 413 | use ffi::YamlEncoding::YamlUtf8Encoding; |
| 414 | use ffi::YamlScalarStyle::*; |
| 415 | use ffi::YamlSequenceStyle::*; |
| 416 | |
| 417 | #[test] |
| 418 | #[allow(unused_must_use)] |
| 419 | fn event_emitter_sequence_test() { |
| 420 | let mut writer = Vec::new(); |
| 421 | { |
| 422 | let mut emitter = YamlEmitter::init(&mut writer); |
| 423 | emitter.emit_stream(YamlUtf8Encoding, |e| { |
| 424 | e.emit_document(None, &[], true, |e| { |
| 425 | e.emit_sequence(None, None, true, YamlFlowSequenceStyle, |e| { |
| 426 | try!(e.emit_scalar_event(None, None, "1", true, false, YamlPlainScalarStyle)); |
| 427 | e.emit_scalar_event(None, None, "2", true, false, YamlPlainScalarStyle) |
| 428 | }) |
| 429 | }) |
| 430 | }); |
| 431 | emitter.flush(); |
| 432 | } |
| 433 | assert_eq!(&writer[..], b"[1, 2]\n"); |
| 434 | } |
| 435 | |
| 436 | #[test] |
| 437 | #[allow(unused_must_use)] |
| 438 | fn event_emitter_mapping_test() { |
| 439 | let mut writer = Vec::new(); |
| 440 | { |
| 441 | let mut emitter = YamlEmitter::init(&mut writer); |
| 442 | emitter.emit_stream(YamlUtf8Encoding, |e| { |
| 443 | e.emit_document(None, &[], true, |e| { |
| 444 | e.emit_mapping(None, None, true, YamlFlowSequenceStyle, |e| { |
| 445 | try!(e.emit_scalar_event(None, None, "a", true, false, YamlPlainScalarStyle)); |
| 446 | try!(e.emit_scalar_event(None, None, "1", true, false, YamlPlainScalarStyle)); |
| 447 | try!(e.emit_scalar_event(None, None, "b", true, false, YamlPlainScalarStyle)); |
| 448 | e.emit_scalar_event(None, None, "2", true, false, YamlPlainScalarStyle) |
| 449 | }) |
| 450 | }) |
| 451 | }); |
| 452 | emitter.flush(); |
| 453 | } |
| 454 | assert_eq!(&writer[..], b"{a: 1, b: 2}\n"); |
| 455 | } |
| 456 | } |