| 1 | // NOTE: This file should be kept in sync with README.md |
| 2 | |
| 3 | use serderust-analyzer cargo serde 1.0.144 crate/::{Deserializerust-analyzer cargo serde 1.0.144 de/Deserialize#, Serializerust-analyzer cargo serde 1.0.144 ser/Serialize#}; |
| 4 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#, fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#}; |
| 5 | |
| 6 | // Types annotated with `Serialize` can be stored as CBOR. |
| 7 | // To be able to load them again add `Deserialize`. |
| 8 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Serializerust-analyzer cargo serde_derive 1.0.144 Serialize:, Deserializerust-analyzer cargo serde_derive 1.0.144 Deserialize:)] |
| 9 | struct Mascotrust-analyzer cargo serde_cbor_2 0.13.0 Mascot# { |
| 10 | namerust-analyzer cargo serde_cbor_2 0.13.0 Mascot#name.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 11 | speciesrust-analyzer cargo serde_cbor_2 0.13.0 Mascot#species.: Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#, |
| 12 | year_of_birthrust-analyzer cargo serde_cbor_2 0.13.0 Mascot#year_of_birth.: u32, |
| 13 | } |
| 14 | |
| 15 | fn mainrust-analyzer cargo serde_cbor_2 0.13.0 main().() -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Boxrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc boxed/Box#<dyn Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error#>> { |
| 16 | let ferris = Mascotrust-analyzer cargo serde_cbor_2 0.13.0 Mascot# { |
| 17 | namerust-analyzer cargo serde_cbor_2 0.13.0 Mascot#name.: "Ferris".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 18 | speciesrust-analyzer cargo serde_cbor_2 0.13.0 Mascot#species.: "crab".to_ownedrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc str/impl#[str][ToOwned]to_owned().(), |
| 19 | year_of_birthrust-analyzer cargo serde_cbor_2 0.13.0 Mascot#year_of_birth.: 2015, |
| 20 | }; |
| 21 | |
| 22 | let ferris_file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::createrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]create().("examples/ferris.cbor")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 23 | // Write Ferris to the given file. |
| 24 | // Instead of a file you can use any type that implements `io::Write` |
| 25 | // like a HTTP body, database connection etc. |
| 26 | serde_cbor_2rust-analyzer cargo serde_cbor_2 0.13.0 crate/::to_writerrust-analyzer cargo serde_cbor_2 0.13.0 ser/to_writer().(ferris_file, &ferris)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 27 | |
| 28 | let tux_file = Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#::openrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/impl#[File]open().("examples/tux.cbor")?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 29 | // Load Tux from a file. |
| 30 | // Serde CBOR performs roundtrip serialization meaning that |
| 31 | // the data will not change in any way. |
| 32 | let tux: Mascotrust-analyzer cargo serde_cbor_2 0.13.0 Mascot# = serde_cbor_2rust-analyzer cargo serde_cbor_2 0.13.0 crate/::from_readerrust-analyzer cargo serde_cbor_2 0.13.0 de/from_reader().(tux_file)?rust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`][Try]branch().; |
| 33 | |
| 34 | printlnrust-analyzer cargo std https://github.com/rust-lang/rust/library/std macros/println!!("{tux:?}"); |
| 35 | // prints: Mascot { name: "Tux", species: "penguin", year_of_birth: 1996 } |
| 36 | |
| 37 | Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()) |
| 38 | } |