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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909
#![warn(missing_docs)] //! # Serde Any //! //! Dynamic serialization and deserialization with the format chosen at runtime //! //! ## Deserialization with a known format //! //! If the deserialization format is known in advance, `serde_any` mirrors the API of `serde_json` and `serde_yaml`. //! Namely, functions [`from_reader`](fn.from_reader.html), [`from_slice`](fn.from_slice.html) and //! [`from_str`](fn.from_str.html) work in the same way as those in format-specific crates, except that they take an //! additional [`Format`](enum.Format.html) paramater specifying the deserialization format. The //! [`from_file`](fn.from_file.html) function is provided as a convenience wrapper around //! [`from_reader`](fn.from_reader.html) for the common case of reading from a file. //! //! ## Deserialization by guessing //! //! This crate also supports deserialization where the data format is not known in advance. //! There are three different ways of inferring the data format: //! * with [`from_file`](fn.from_file.html), the format is deduced from the file extension. //! This is useful if a user can load a data file of any format. //! * with [`from_file_stem`](fn.from_file_stem.html), each filename with the given stem and a supported extension //! is checked. If any such file exists, its data is deserialized and returned. //! This is useful for configuration files with a known set of filenames. //! * with [`from_slice_any`](fn.from_slice_any.html) and [`from_str_any`](fn.from_slice_any.html), deserialization //! using each supported format is tried until one succeeds. //! This is useful when you receive data from an unknown source and don't know what format it is in. //! //! Note there is no corresponding `from_reader_any` function, as attempting to deserialize from a reader would //! consume its data. In order to deserialize from a `std::io::Read`, read the data into a `Vec<u8>` or `String` and //! call [`from_slice_any`](fn.from_slice_any) or [`from_str_any`](fn.from_str_any.html). //! //! ## Serialization //! //! For serialization, the data format must always be provided. //! Consistent with the format-specific crates, data may be serialized to a `String` with //! [`to_string`](fn.to_string.html), to a `Vec<u8>` with [`to_vec`](fn.to_string.html), or to a `std::io::Write` with //! [`to_writer`](fn.to_string.html). //! //! Alternatively, when writing to a file, the format can be inferred from the file name by the //! [`to_file`](fn.to_file.html) function. Similarly to [`from_file`](fn.from_file.html), this is most useful when //! saving to a user-selected file. //! //! There is no support for pretty-printing yet. //! #[macro_use] extern crate failure; extern crate serde; #[cfg(feature = "toml")] extern crate toml; #[cfg(feature = "json")] extern crate serde_json; #[cfg(feature = "yaml")] extern crate serde_yaml; #[cfg(feature = "ron")] extern crate ron; use std::path::Path; use std::ffi::OsStr; use std::io::{Read, Write}; use std::fs::File; use std::fmt; use serde::de::{Deserialize, DeserializeOwned}; use serde::ser::Serialize; /// The common error type #[derive(Debug, Fail)] pub enum Error { /// Error serializing or deserializing with JSON #[cfg(feature = "json")] #[fail(display = "JSON error: {}", _0)] Json(#[fail(cause)] serde_json::Error), /// Error serializing or deserializing with YAML #[cfg(feature = "yaml")] #[fail(display = "YAML error: {}", _0)] Yaml(#[fail(cause)] serde_yaml::Error), /// Error deserializing with TOML #[cfg(feature = "toml")] #[fail(display = "TOML deserialize error: {}", _0)] TomlDeserialize(#[fail(cause)] toml::de::Error), /// Error serializing with TOML #[cfg(feature = "toml")] #[fail(display = "TOML serialize error: {}", _0)] TomlSerialize(#[fail(cause)] toml::ser::Error), /// Error deserializing with RON #[cfg(feature = "ron")] #[fail(display = "RON deserialize error: {}", _0)] RonDeserialize(#[fail(cause)] ron::de::Error), /// Error serializing with RON #[cfg(feature = "ron")] #[fail(display = "RON serialize error: {}", _0)] RonSerialize(#[fail(cause)] ron::ser::Error), /// IO error #[fail(display = "IO error: {}", _0)] Io(#[fail(cause)] std::io::Error), /// The specified format is not supported #[fail(display = "Format {} not supported", _0)] UnsupportedFormat(Format), /// The specified file extension is not supported #[fail(display = "File extension {} not supported", _0)] UnsupportedFileExtension(String), /// None of the supported formats was able to deserialize successfully #[fail(display = "No format was able to parse the source")] NoSuccessfulParse, } macro_rules! impl_error_from { ($error_type:ty => $variant:expr) => ( impl From<$error_type> for Error { fn from(e: $error_type) -> Error { $variant(e) } } ); } impl_error_from!(std::io::Error => Error::Io); #[cfg(feature = "json")] impl_error_from!(serde_json::Error => Error::Json); #[cfg(feature = "yaml")] impl_error_from!(serde_yaml::Error => Error::Yaml); #[cfg(feature = "toml")] impl_error_from!(toml::ser::Error => Error::TomlSerialize); #[cfg(feature = "toml")] impl_error_from!(toml::de::Error => Error::TomlDeserialize); #[cfg(feature = "ron")] impl_error_from!(ron::ser::Error => Error::RonSerialize); #[cfg(feature = "ron")] impl_error_from!(ron::de::Error => Error::RonDeserialize); /// Serialization or deserialization formats #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Format { /// TOML (Tom's Obvious, Minimal Language), enabled by the `toml` feature. Toml, /// JSON (JavaScript Object Notation), enabled by the `json` feature. Json, /// YAML (YAML Ain't Markup Language), enabled by the `yaml` feature. Yaml, /// RON (Rusty Object Notation), enabled by the `ron` feature. Ron, } impl fmt::Display for Format { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{:?}", self) } } impl Format { /// Checks whether this format is supported /// /// Support for different formats is controlled by the features used /// when building `serde_any`. pub fn is_supported(&self) -> bool { #[allow(unreachable_patterns)] match self { #[cfg(feature = "toml")] Format::Toml => true, #[cfg(feature = "json")] Format::Json => true, #[cfg(feature = "yaml")] Format::Yaml => true, #[cfg(feature = "ron")] Format::Ron => true, _ => false, } } } /// Return a list of supported formats /// /// Support for different formats is controlled by the features used /// when building `serde_any`. pub fn supported_formats() -> Vec<Format> { let mut f = Vec::new(); #[cfg(feature = "toml")] f.push(Format::Toml); #[cfg(feature = "json")] f.push(Format::Json); #[cfg(feature = "yaml")] f.push(Format::Yaml); #[cfg(feature = "ron")] f.push(Format::Ron); f } /// Return a list of recognized file extensions /// /// The return value depends on the features used when building serde_any. /// Only file extensions corresponding to supported formats will be returned. pub fn supported_extensions() -> Vec<&'static str> { let mut e = Vec::new(); #[cfg(feature = "toml")] e.push("toml"); #[cfg(feature = "json")] e.push("json"); #[cfg(feature = "yaml")] { e.push("yml"); e.push("yaml"); } #[cfg(feature = "ron")] { e.push("ron"); } e } /// Attempt to guess the serialization/deserialization format from a file name /// /// This function may recognize and return a format even if it's not supported due to feature flags. pub fn guess_format<P>(path: P) -> Option<Format> where P: AsRef<Path>, { path.as_ref() .extension() .and_then(OsStr::to_str) .and_then(guess_format_from_extension) } /// Attempt to guess the serialization/deserialization format from a file extension /// /// This function may recognize and return a format even if it's not supported due to feature flags. pub fn guess_format_from_extension(ext: &str) -> Option<Format> { match ext { "yml" | "yaml" => Some(Format::Yaml), "json" => Some(Format::Json), "toml" => Some(Format::Toml), "ron" => Some(Format::Ron), _ => None, } } /// Deserialize from an IO stream using a specified format /// /// # Errors /// /// If the specified format is not supported, this function returns /// `Error::UnsupportedFormat`. /// /// If the conversion itself fails, the format-specific variant of `Error` /// will be returned, with the underlying error as its cause. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use failure::Error; /// use std::fs::File; /// use std::path::Path; /// /// use serde_any::Format; /// /// #[derive(Deserialize, Debug)] /// struct User { /// fingerprint: String, /// location: String, /// } /// /// fn read_user_from_file<P: AsRef<Path>>(path: P, format: Format) -> Result<User, Error> { /// // Open the file in read-only mode. /// let file = File::open(path)?; /// /// // Read the contents of the file as an instance of `User`. /// let u = serde_any::from_reader(file, format)?; /// /// // Return the `User`. /// Ok(u) /// } /// /// fn main() { /// match read_user_from_file("test.json", Format::Json) { /// Ok(u) => println!("{:#?}", u), /// Err(e) => println!("Error deserializing user: {}", e), /// }; /// } /// ``` #[allow(unreachable_patterns, unused_mut)] pub fn from_reader<T, R>(mut reader: R, format: Format) -> Result<T, Error> where T: DeserializeOwned, R: Read, { match format { #[cfg(feature = "yaml")] Format::Yaml => Ok(serde_yaml::from_reader::<_, T>(reader)?), #[cfg(feature = "json")] Format::Json => Ok(serde_json::from_reader::<_, T>(reader)?), #[cfg(feature = "toml")] Format::Toml => { let mut s = Vec::new(); reader.read_to_end(&mut s)?; Ok(toml::from_slice::<T>(&s)?) } #[cfg(feature = "ron")] Format::Ron => Ok(ron::de::from_reader::<_, T>(reader)?), _ => Err(Error::UnsupportedFormat(format)), } } /// Deserialize from a string using a specified format /// /// # Errors /// /// If the specified format is not supported, this function returns /// `Error::UnsupportedFormat`. /// /// If the conversion itself fails, the format-specific variant of `Error` /// will be returned, with the underlying error as its cause. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use failure::Error; /// /// use serde_any::Format; /// /// #[derive(Deserialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let data = "{ /// \"name\": \"Jon Snow\", /// \"knowledge\": 0 /// }"; /// let person: Person = serde_any::from_str(data, Format::Json)?; /// println!("{:#?}", person); /// Ok(()) /// } /// ``` pub fn from_str<'a, T>(s: &'a str, format: Format) -> Result<T, Error> where T: for<'de> Deserialize<'de>, { #[allow(unreachable_patterns)] match format { #[cfg(feature = "yaml")] Format::Yaml => Ok(serde_yaml::from_str::<T>(s)?), #[cfg(feature = "json")] Format::Json => Ok(serde_json::from_str::<T>(s)?), #[cfg(feature = "toml")] Format::Toml => Ok(toml::from_str::<T>(s)?), #[cfg(feature = "ron")] Format::Ron => Ok(ron::de::from_str::<T>(s)?), _ => Err(Error::UnsupportedFormat(format)), } } /// Deserialize from a string using any supported format /// /// This function will attempt to deserialize the string using each supported format, /// and will return the result of the first successful deserialization. /// /// # Errors /// /// If none of the supported formats can deserialize the string successfully, /// `Error::NoSuccessfulParse` is returned. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use failure::Error; /// /// use serde_any::Format; /// /// #[derive(Deserialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let data = "{ /// \"name\": \"Jon Snow\", /// \"knowledge\": 0 /// }"; /// let person: Person = serde_any::from_str_any(data)?; /// println!("{:#?}", person); /// Ok(()) /// } /// ``` pub fn from_str_any<'a, T>(s: &'a str) -> Result<T, Error> where T: for<'de> Deserialize<'de>, { for format in supported_formats() { match from_str(&s, format) { Ok(t) => return Ok(t), Err(_) => continue, } } Err(Error::NoSuccessfulParse) } /// Deserialize from a byte slice using a specified format /// /// This function will attempt to deserialize the string using each supported format, /// and will return the result of the first successful deserialization. /// /// # Errors /// /// If none of the supported formats can deserialize the string successfully, /// `Error::NoSuccessfulParse` is returned. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use failure::Error; /// /// use serde_any::Format; /// /// #[derive(Deserialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let data = b"{ /// \"name\": \"Jon Snow\", /// \"knowledge\": 0 /// }"; /// let person: Person = serde_any::from_slice(data, Format::Json)?; /// println!("{:#?}", person); /// Ok(()) /// } /// ``` pub fn from_slice<'a, T>(s: &'a [u8], format: Format) -> Result<T, Error> where T: for<'de> Deserialize<'de>, { #[allow(unreachable_patterns)] match format { #[cfg(feature = "yaml")] Format::Yaml => Ok(serde_yaml::from_slice::<T>(s)?), #[cfg(feature = "json")] Format::Json => Ok(serde_json::from_slice::<T>(s)?), #[cfg(feature = "toml")] Format::Toml => Ok(toml::from_slice::<T>(s)?), #[cfg(feature = "ron")] Format::Ron => Ok(ron::de::from_bytes::<T>(s)?), _ => Err(Error::UnsupportedFormat(format)), } } /// Deserialize from a byte slice using any supported format /// /// # Errors /// /// If the specified format is not supported, this function returns /// `Error::UnsupportedFormat`. /// /// If the conversion itself fails, the format-specific variant of `Error` /// will be returned, with the underlying error as its cause. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use failure::Error; /// /// use serde_any::Format; /// /// #[derive(Deserialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let data = b"{ /// \"name\": \"Jon Snow\", /// \"knowledge\": 0 /// }"; /// let person: Person = serde_any::from_slice_any(data)?; /// println!("{:#?}", person); /// Ok(()) /// } /// ``` pub fn from_slice_any<'a, T>(s: &'a [u8]) -> Result<T, Error> where T: for<'de> Deserialize<'de>, { for format in supported_formats() { match from_slice(&s, format) { Ok(t) => return Ok(t), Err(_) => continue, } } Err(Error::NoSuccessfulParse) } /// Deserialize from a file /// /// The format is detected using `guess_format`. /// If that fails, such as if the file extension is not recognized, /// the whole file is read into a buffer, /// and deserialization is attempted using `from_slice_any`. /// /// # Errors /// /// If the file extension is recognized, but parsing fails, this function returns /// the error from `from_reader`. /// /// If the file extension is not recognized and the file cannot be opened, /// it returns `Error::Io` with the underlying error as the cause. /// /// If the file extension is not recognized, the file can opened but deserialization fails, /// this function returns the error from `from_slice_any`. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// /// #[derive(Deserialize, Debug)] /// struct User { /// fingerprint: String, /// location: String, /// } /// /// fn main() { /// match serde_any::from_file::<User, _>("test.json") { /// Ok(u) => println!("{:#?}", u), /// Err(e) => println!("Error deserializing user: {}", e), /// }; /// } /// ``` pub fn from_file<T, P>(path: P) -> Result<T, Error> where T: DeserializeOwned, P: AsRef<Path>, { let format = guess_format(&path); match format { Some(format) => from_reader(File::open(path)?, format), None => { let mut s = Vec::new(); let mut reader = File::open(&path)?; reader.read_to_end(&mut s)?; Ok(from_slice_any(&s)?) } } } /// Deserialize from any file with a given stem /// /// This function tries to deserialize from any file with stem `stem` and any of the supported extensions. /// The list of supported extensions can be queried with `supported_extensions`. /// /// # Errors /// /// If none of the supported formats can deserialize the string successfully, /// `Error::NoSuccessfulParse` is returned. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// /// #[derive(Deserialize, Debug)] /// struct User { /// fingerprint: String, /// location: String, /// } /// /// fn main() { /// // Will attempt "user.json", "user.yaml", "user.toml" and "user.ron" /// // If any of the features is disabled, that extension is skipped /// match serde_any::from_file_stem::<User, _>("user") { /// Ok(u) => println!("{:#?}", u), /// Err(e) => println!("Error deserializing user: {}", e), /// }; /// } /// ``` pub fn from_file_stem<T, P>(stem: P) -> Result<T, Error> where T: DeserializeOwned, P: AsRef<Path>, { for extension in supported_extensions() { let path = stem.as_ref().with_extension(extension); if let Ok(t) = from_file(&path) { return Ok(t); } } Err(Error::NoSuccessfulParse) } /// Serialize to a `String` /// /// # Errors /// /// If serialization fails, the format-specific error type is returned, /// with the underlying error as its cause. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use serde_any::Format; /// use failure::Error; /// /// #[derive(Serialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let bran = Person { /// name: "Brandon Stark".to_string(), /// knowledge: 100, /// }; /// let data = serde_any::to_string(&bran, Format::Toml)?; /// println!("{}", data); /// assert_eq!(&data[..], "name = \"Brandon Stark\"\nknowledge = 100\n"); /// Ok(()) /// } /// ``` #[allow(unused_mut)] pub fn to_string<T>(value: &T, format: Format) -> Result<String, Error> where T: Serialize, { #[allow(unreachable_patterns)] match format { #[cfg(feature = "yaml")] Format::Yaml => Ok(serde_yaml::to_string(value)?), #[cfg(feature = "json")] Format::Json => Ok(serde_json::to_string(value)?), #[cfg(feature = "toml")] Format::Toml => Ok(toml::to_string(value)?), #[cfg(feature = "ron")] Format::Ron => Ok(ron::ser::to_string(value)?), _ => Err(Error::UnsupportedFormat(format)), } } /// Serialize to a byte vector /// /// # Errors /// /// If serialization fails, the format-specific error type is returned, /// with the underlying error as its cause. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use serde_any::Format; /// use failure::Error; /// /// #[derive(Serialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let bran = Person { /// name: "Brandon Stark".to_string(), /// knowledge: 100, /// }; /// let data = serde_any::to_vec(&bran, Format::Toml)?; /// assert_eq!( /// data, /// b"name = \"Brandon Stark\"\nknowledge = 100\n".to_vec() /// ); /// Ok(()) /// } /// ``` pub fn to_vec<T>(value: &T, format: Format) -> Result<Vec<u8>, Error> where T: Serialize, { #[allow(unreachable_patterns)] match format { #[cfg(feature = "yaml")] Format::Yaml => Ok(serde_yaml::to_vec(value)?), #[cfg(feature = "json")] Format::Json => Ok(serde_json::to_vec(value)?), #[cfg(feature = "toml")] Format::Toml => Ok(toml::to_vec(value)?), #[cfg(feature = "ron")] Format::Toml => Ok(ron::ser::to_string(value)?.into_bytes()), _ => Err(Error::UnsupportedFormat(format)), } } /// Serialize to a writer /// /// # Errors /// /// If serialization fails, the format-specific error type is returned, /// with the underlying error as its cause. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use serde_any::Format; /// use failure::Error; /// /// use std::fs::File; /// /// #[derive(Serialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let bran = Person { /// name: "Brandon Stark".to_string(), /// knowledge: 100, /// }; /// let file = File::create("weirwood.ron")?; /// let data = serde_any::to_writer(file, &bran, Format::Ron)?; /// Ok(()) /// } /// ``` #[allow(unused_mut)] pub fn to_writer<W, T>(mut writer: W, value: &T, format: Format) -> Result<(), Error> where W: Write, T: Serialize, { #[allow(unreachable_patterns)] match format { #[cfg(feature = "yaml")] Format::Yaml => Ok(serde_yaml::to_writer(writer, value)?), #[cfg(feature = "json")] Format::Json => Ok(serde_json::to_writer(writer, value)?), #[cfg(feature = "toml")] Format::Toml => { let s = toml::to_vec(value)?; writer.write(&s)?; Ok(()) } #[cfg(feature = "ron")] Format::Ron => { let s = ron::ser::to_string(value)?; write!(&mut writer, "{}", s)?; Ok(()) } _ => Err(Error::UnsupportedFormat(format)), } } /// Serialize to a file /// /// # Errors /// /// If the serialization format cannot be inferred from the file name, /// `UnsupportedFileExtension` is returned. /// /// If serialization fails, the format-specific error type is returned, /// with the underlying error as its cause. /// /// # Example /// /// ``` /// #[macro_use] /// extern crate serde; /// extern crate serde_any; /// extern crate failure; /// /// use serde_any::Format; /// use failure::Error; /// /// use std::fs::File; /// /// #[derive(Serialize, Debug)] /// struct Person { /// name: String, /// knowledge: u32, /// } /// /// fn main() -> Result<(), Error> { /// let bran = Person { /// name: "Brandon Stark".to_string(), /// knowledge: 100, /// }; /// serde_any::to_file("bran.yaml", &bran)?; /// Ok(()) /// } /// ``` pub fn to_file<T, P>(path: P, value: &T) -> Result<(), Error> where T: Serialize, P: AsRef<Path>, { let format = guess_format(&path); match format { Some(format) => to_writer(File::create(path)?, value, format), None => { let ext = path.as_ref() .extension() .and_then(OsStr::to_str) .map(String::from) .unwrap_or(String::new()); Err(Error::UnsupportedFileExtension(ext)) } } } #[cfg(test)] mod tests { use super::*; #[test] fn extensions() { for ext in supported_extensions() { let stem = Path::new("test"); let from_ext = guess_format_from_extension(ext); let from_path = guess_format(stem.with_extension(ext)); assert!(from_ext.is_some()); assert!(from_path.is_some()); assert_eq!(from_ext, from_path); } } }