From 27a0368897c22286545900d370f2ef588ca2bda0 Mon Sep 17 00:00:00 2001 From: Omar Shehata Date: Sun, 18 Aug 2019 17:44:32 -0400 Subject: [PATCH] Add runnable examples for reading & writing --- README.md | 6 +++++- examples/reading.rs | 10 ++++++++++ examples/writing.rs | 23 +++++++++++++++++++++++ tests/swfs/SimpleRedBackground.swf | Bin 0 -> 43 bytes 4 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 examples/reading.rs create mode 100644 examples/writing.rs create mode 100644 tests/swfs/SimpleRedBackground.swf diff --git a/README.md b/README.md index ba4994498..ccc521ec5 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,11 @@ use std::fs::File; let file = File::open("file.swf").unwrap(); let reader = BufReader::new(file); let swf = swf::read_swf(reader).unwrap(); -println!("The SWF has {} frames", swf.num_frames); +println!("The SWF has {} frames", swf.header.num_frames); ``` +Try `cargo run --example reading` in this repository to run this example. + ## Writing ```rust,no_run @@ -48,6 +50,8 @@ let writer = std::io::BufWriter::new(file); swf::write_swf(&swf, writer).unwrap(); ``` +Try `cargo run --example writing` in this repository to run this example. + ## License Licensed under either of diff --git a/examples/reading.rs b/examples/reading.rs new file mode 100644 index 000000000..ac00f905d --- /dev/null +++ b/examples/reading.rs @@ -0,0 +1,10 @@ +use std::io::BufReader; +use std::fs::File; + +fn main() { + let file = File::open("tests/swfs/SimpleRedBackground.swf").unwrap(); + let reader = BufReader::new(file); + let swf = swf::read_swf(reader).unwrap(); + println!("The SWF has {} frame(s).", swf.header.num_frames); + println!("The SWF has {} tag(s).", swf.tags.len()); +} diff --git a/examples/writing.rs b/examples/writing.rs new file mode 100644 index 000000000..d6553bb36 --- /dev/null +++ b/examples/writing.rs @@ -0,0 +1,23 @@ +use swf::*; + +fn main() { + let swf = Swf { + header: Header { + version: 6, + compression: Compression::Zlib, + stage_size: Rectangle { + x_min: Twips::from_pixels(0.0), x_max: Twips::from_pixels(400.0), + y_min: Twips::from_pixels(0.0), y_max: Twips::from_pixels(400.0) + }, + frame_rate: 60.0, + num_frames: 1, + }, + tags: vec![ + Tag::SetBackgroundColor(Color { r: 255, g: 0, b: 0, a: 255 }), + Tag::ShowFrame + ] + }; + let file = std::fs::File::create("tests/swfs/SimpleRedBackground.swf").unwrap(); + let writer = std::io::BufWriter::new(file); + swf::write_swf(&swf, writer).unwrap(); +} diff --git a/tests/swfs/SimpleRedBackground.swf b/tests/swfs/SimpleRedBackground.swf new file mode 100644 index 0000000000000000000000000000000000000000..0952a5f7393fc017932c65720a7784e08c430c5c GIT binary patch literal 43 zcmZ<`4`!2PU|^`2!+O9_kU@ci;lOn!v7!&>oLrRDxoa#u3#ad&-Ogy(&U_mH|4|K! literal 0 HcmV?d00001