From: Greg Burri Date: Sat, 17 Sep 2022 21:17:37 +0000 (+0200) Subject: First part of chapter 7 X-Git-Url: https://git.euphorik.ch/?a=commitdiff_plain;h=1ea8a60e5aff0bb2bd3ceee4731b3d45cf1ded25;p=rust_in_action.git First part of chapter 7 --- diff --git a/ch7-file-and-storage/Cargo.lock b/ch7-file-and-storage/Cargo.lock new file mode 100644 index 0000000..21b858a --- /dev/null +++ b/ch7-file-and-storage/Cargo.lock @@ -0,0 +1,114 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + +[[package]] +name = "ch7-file-and-storage" +version = "0.1.0" +dependencies = [ + "bincode", + "serde", + "serde_cbor", + "serde_derive", + "serde_json", +] + +[[package]] +name = "half" +version = "1.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eabb4a44450da02c90444cf74558da904edde8fb4e9035a9a6a4e15445af0bd7" + +[[package]] +name = "itoa" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6c8af84674fe1f223a982c933a0ee1086ac4d4052aa0fb8060c12c6ad838e754" + +[[package]] +name = "proc-macro2" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0a2ca2c61bc9f3d74d2886294ab7b9853abd9c1ad903a3ac7815c58989bb7bab" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbe448f377a7d6961e30f5955f9b8d106c3f5e449d493ee1b125c1d43c2b5179" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "ryu" +version = "1.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09" + +[[package]] +name = "serde" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0f747710de3dcd43b88c9168773254e809d8ddbdf9653b84e2554ab219f17860" + +[[package]] +name = "serde_cbor" +version = "0.11.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5" +dependencies = [ + "half", + "serde", +] + +[[package]] +name = "serde_derive" +version = "1.0.144" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "94ed3a816fb1d101812f83e789f888322c34e291f894f19590dc310963e87a00" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.85" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e55a28e3aaef9d5ce0506d0a14dbba8054ddc7e499ef522dd8b26859ec9d4a44" +dependencies = [ + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "syn" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58dbef6ec655055e20b86b15a8cc6d439cca19b667537ac6a1369572d151ab13" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dcc811dc4066ac62f84f11307873c4850cb653bfa9b1719cee2bd2204a4bc5dd" diff --git a/ch7-file-and-storage/Cargo.toml b/ch7-file-and-storage/Cargo.toml new file mode 100644 index 0000000..1cbb580 --- /dev/null +++ b/ch7-file-and-storage/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "ch7-file-and-storage" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +bincode = "1" +serde = "1" +serde_cbor = "0.11" +serde_derive = "1" +serde_json = "1" \ No newline at end of file diff --git a/ch7-file-and-storage/src/fview.rs b/ch7-file-and-storage/src/fview.rs new file mode 100644 index 0000000..2f18dc4 --- /dev/null +++ b/ch7-file-and-storage/src/fview.rs @@ -0,0 +1,29 @@ +use std::fs::File; +use std::io::prelude::*; +use std::env; + +const BYTES_PER_LINE: usize = 16; + +pub fn run() { + + let arg1 = env::args().nth(1); + let fname = arg1.expect("Usage: fview FILENAME"); + + let mut f = File::open(&fname).expect("Unable to open file."); + let mut pos = 0; + let mut buffer = [0; BYTES_PER_LINE]; + + while let Ok(_) = f.read_exact(&mut buffer) { + print!("[0x{:08x}] ", pos); + for byte in &buffer { + match *byte { + 0x00 => print!(". "), + 0xff => print!("## "), + _ => print!("{:02x} ", byte), + } + } + + println!(""); + pos += BYTES_PER_LINE; + } +} \ No newline at end of file diff --git a/ch7-file-and-storage/src/main.rs b/ch7-file-and-storage/src/main.rs new file mode 100644 index 0000000..af9214d --- /dev/null +++ b/ch7-file-and-storage/src/main.rs @@ -0,0 +1,18 @@ +use std::fs::File; +use std::fs::OpenOptions; +use std::io::prelude::*; + +mod serde; +mod fview; + +fn main() { + //serde::run(); + //fview::run(); + + let mut f = OpenOptions::new().read(true).write(true).create(true).open("test.txt").expect("File can't be opened"); + let mut content = String::new(); + match f.read_to_string(&mut content) { + Ok(bytes_read) => println!("File content: \"{}\", bytes read: {}", content, bytes_read), + Err(err) => println!("Can't read file: {}", err), + } +} diff --git a/ch7-file-and-storage/src/serde.rs b/ch7-file-and-storage/src/serde.rs new file mode 100644 index 0000000..663958b --- /dev/null +++ b/ch7-file-and-storage/src/serde.rs @@ -0,0 +1,33 @@ +use bincode::serialize as to_bincode; +use serde_cbor::to_vec as to_cbor; +use serde_json::to_string as to_json; +use serde_derive::Serialize; + +#[derive(Serialize)] +struct City { + name: String, + population: usize, + latitude: f64, + longitude: f64, +} + +pub fn run() { + let calabar = City { + name: String::from("Calabar"), + population: 470_000, + latitude: 4.95, + longitude: 8.33, + }; + + let as_json = to_json(&calabar).unwrap(); + let as_cbor = to_cbor(&calabar).unwrap(); + let as_bincode = to_bincode(&calabar).unwrap(); + + println!("json:\n{}\n", &as_json); + println!("cbor:\n{:?}\n", &as_cbor); + println!("bincode:\n{:?}\n", &as_bincode); + + println!("json (as UTF8):\n{}\n", String::from_utf8_lossy(as_json.as_bytes())); + println!("cbor (as UTF8):\n{:?}\n", String::from_utf8_lossy(&as_cbor)); + println!("bincode (as UTF8):\n{:?}\n", String::from_utf8_lossy(&as_bincode)); +} \ No newline at end of file