From 98b894bd76a2bbffdfffa92e9a9c74af8a5405cc Mon Sep 17 00:00:00 2001 From: Andrei Marinica Date: Sat, 23 Oct 2021 02:48:58 +0300 Subject: [PATCH] rust tutorial code extractor --- .gitignore | 5 ++ Cargo.toml | 8 +++ docs/developers/tutorials/crowdfunding-p1.md | 3 +- docs/developers/tutorials/crowdfunding-p2.md | 8 +-- testing/crowdfunding-esdt/.gitignore | 7 +++ testing/extract-tutorial-code/Cargo.toml | 13 +++++ .../extract-tutorial-code/src/extract_code.rs | 52 +++++++++++++++++++ 7 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 Cargo.toml create mode 100644 testing/crowdfunding-esdt/.gitignore create mode 100644 testing/extract-tutorial-code/Cargo.toml create mode 100644 testing/extract-tutorial-code/src/extract_code.rs diff --git a/.gitignore b/.gitignore index ebb2363bc..18d652bc2 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,8 @@ desktop.ini /build tags + +# Rust workspace build target +/target +/testing/extract-tutorial-code/target +Cargo.lock diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 000000000..e647788e5 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[workspace] +members = [ + "testing/extract-tutorial-code", +] + +exclude = [ + "testing/crowdfunding-esdt", +] diff --git a/docs/developers/tutorials/crowdfunding-p1.md b/docs/developers/tutorials/crowdfunding-p1.md index 8007502ce..0620c8426 100644 --- a/docs/developers/tutorials/crowdfunding-p1.md +++ b/docs/developers/tutorials/crowdfunding-p1.md @@ -83,8 +83,7 @@ Let's have a quick look around the project. Open `Cargo.toml` in the text editor of your choice, and add the following content: -``` -Cargo.toml +```toml,file=Cargo.toml [package] name = "crowdfunding" version = "0.0.1" diff --git a/docs/developers/tutorials/crowdfunding-p2.md b/docs/developers/tutorials/crowdfunding-p2.md index c6b4243d9..afac80fe6 100644 --- a/docs/developers/tutorials/crowdfunding-p2.md +++ b/docs/developers/tutorials/crowdfunding-p2.md @@ -43,7 +43,7 @@ Also note that BigUint logic does not reside in the contract, but is built into Let's test that initialization works. -``` +```json,file=crowdfunding-init.scen.json { "name": "crowdfunding deployment test", "steps": [ @@ -154,7 +154,7 @@ To test the function, we'll add a new test file, in the same `mandos` folder. Le To avoid duplicating the deployment code, we import it from `test-init.scen.json` . -``` +```json,file=crowdfunding-fund.scen.json { "name": "crowdfunding funding", "steps": [ @@ -272,7 +272,7 @@ Note: `panic!` works in contracts, but it is highly discouraged. We'll create another test file to verify that the validation works: `test-fund-too-late.scen.json` . -``` +```json,file=crowdfunding-fund-too-late.scen.json { "name": "trying to fund one block too late", "steps": [ @@ -513,7 +513,7 @@ The only new function here is `send_tx`, which simply forwards funds from the co If you followed all the steps presented until now, you should have ended up with a contract that looks something like: -``` +```rust,file=final.rs #![no_std] imports!(); diff --git a/testing/crowdfunding-esdt/.gitignore b/testing/crowdfunding-esdt/.gitignore new file mode 100644 index 000000000..db0647a22 --- /dev/null +++ b/testing/crowdfunding-esdt/.gitignore @@ -0,0 +1,7 @@ +# Generated by `extract-tutorial-code` +/mandos +/src/lib.rs +/Cargo.toml + +/target/ +*/target/ diff --git a/testing/extract-tutorial-code/Cargo.toml b/testing/extract-tutorial-code/Cargo.toml new file mode 100644 index 000000000..4e64768d0 --- /dev/null +++ b/testing/extract-tutorial-code/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "extract-tutorial-code" +version = "0.0.0" +authors = ["Andrei Marinica "] +edition = "2018" + +[[bin]] +name = "test-gen" +path = "src/extract_code.rs" + +[dependencies] +waltz = "0.4.1" +pulldown-cmark = { version = "0.1", default-features = false } diff --git a/testing/extract-tutorial-code/src/extract_code.rs b/testing/extract-tutorial-code/src/extract_code.rs new file mode 100644 index 000000000..25fad121a --- /dev/null +++ b/testing/extract-tutorial-code/src/extract_code.rs @@ -0,0 +1,52 @@ +use std::{fs, fs::File, io::Write, path::Path}; + +use waltz::CodeBlock; + +fn extract_code_blocks_from_file>(path: P) -> Vec { + let contents = fs::read_to_string(path.as_ref()) + .unwrap_or_else(|e| panic!("not found: {} {:?}", e, path.as_ref())); + let markdown = pulldown_cmark::Parser::new(contents.as_str()); + waltz::extract_code_blocks(markdown).unwrap() +} + +fn extract_crowdfunding_tutorial_code_blocks() -> Vec { + let mut code_blocks_1 = + extract_code_blocks_from_file("../../docs/developers/tutorials/crowdfunding-p1.md"); + let code_blocks_2 = + extract_code_blocks_from_file("../../docs/developers/tutorials/crowdfunding-p2.md"); + code_blocks_1.extend(code_blocks_2.into_iter()); + code_blocks_1 +} + +fn write_code_block>(path: P, code_block: &CodeBlock) { + let mut file = File::create(path).unwrap(); + file.write_all(code_block.content().as_bytes()).unwrap(); +} + +fn main() { + fs::create_dir_all("../crowdfunding-esdt/mandos").unwrap(); + + let code_blocks = extract_crowdfunding_tutorial_code_blocks(); + for code_block in &code_blocks { + if let Some(filename) = code_block.filename() { + println!("{}", filename.as_str()); + match filename.as_str() { + "Cargo.toml" => write_code_block("../crowdfunding-esdt/Cargo.toml", code_block), + "final.rs" => write_code_block("../crowdfunding-esdt/src/lib.rs", code_block), + "crowdfunding-init.scen.json" => write_code_block( + "../crowdfunding-esdt/mandos/crowdfunding-init.scen.json", + code_block, + ), + "crowdfunding-fund.scen.json" => write_code_block( + "../crowdfunding-esdt/mandos/crowdfunding-fund.scen.json", + code_block, + ), + "crowdfunding-fund-too-late.scen.json" => write_code_block( + "../crowdfunding-esdt/mandos/crowdfunding-fund-too-late.scen.json", + code_block, + ), + _ => {} + } + } + } +}