aboutsummaryrefslogtreecommitdiff
path: root/examples/readme.rs
diff options
context:
space:
mode:
Diffstat (limited to 'examples/readme.rs')
-rw-r--r--examples/readme.rs39
1 files changed, 39 insertions, 0 deletions
diff --git a/examples/readme.rs b/examples/readme.rs
new file mode 100644
index 0000000..7689394
--- /dev/null
+++ b/examples/readme.rs
@@ -0,0 +1,39 @@
+// NOTE: This file should be kept in sync with README.md
+
+use serde_derive::{Deserialize, Serialize};
+use std::error::Error;
+use std::fs::File;
+
+// Types annotated with `Serialize` can be stored as CBOR.
+// To be able to load them again add `Deserialize`.
+#[derive(Debug, Serialize, Deserialize)]
+struct Mascot {
+ name: String,
+ species: String,
+ year_of_birth: u32,
+}
+
+fn main() -> Result<(), Box<dyn Error>> {
+ let ferris = Mascot {
+ name: "Ferris".to_owned(),
+ species: "crab".to_owned(),
+ year_of_birth: 2015,
+ };
+
+ let ferris_file = File::create("examples/ferris.cbor")?;
+ // Write Ferris to the given file.
+ // Instead of a file you can use any type that implements `io::Write`
+ // like a HTTP body, database connection etc.
+ serde_cbor::to_writer(ferris_file, &ferris)?;
+
+ let tux_file = File::open("examples/tux.cbor")?;
+ // Load Tux from a file.
+ // Serde CBOR performs roundtrip serialization meaning that
+ // the data will not change in any way.
+ let tux: Mascot = serde_cbor::from_reader(tux_file)?;
+
+ println!("{:?}", tux);
+ // prints: Mascot { name: "Tux", species: "penguin", year_of_birth: 1996 }
+
+ Ok(())
+}