Skip to content

Commit

Permalink
rust tutorial code extractor
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Oct 22, 2021
1 parent 88b6a13 commit 98b894b
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ desktop.ini
/build

tags

# Rust workspace build target
/target
/testing/extract-tutorial-code/target
Cargo.lock
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[workspace]
members = [
"testing/extract-tutorial-code",
]

exclude = [
"testing/crowdfunding-esdt",
]
3 changes: 1 addition & 2 deletions docs/developers/tutorials/crowdfunding-p1.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions docs/developers/tutorials/crowdfunding-p2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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": [
Expand Down Expand Up @@ -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!();
Expand Down
7 changes: 7 additions & 0 deletions testing/crowdfunding-esdt/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Generated by `extract-tutorial-code`
/mandos
/src/lib.rs
/Cargo.toml

/target/
*/target/
13 changes: 13 additions & 0 deletions testing/extract-tutorial-code/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[package]
name = "extract-tutorial-code"
version = "0.0.0"
authors = ["Andrei Marinica <[email protected]>"]
edition = "2018"

[[bin]]
name = "test-gen"
path = "src/extract_code.rs"

[dependencies]
waltz = "0.4.1"
pulldown-cmark = { version = "0.1", default-features = false }
52 changes: 52 additions & 0 deletions testing/extract-tutorial-code/src/extract_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
use std::{fs, fs::File, io::Write, path::Path};

use waltz::CodeBlock;

fn extract_code_blocks_from_file<P: AsRef<Path>>(path: P) -> Vec<CodeBlock> {
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<CodeBlock> {
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<P: AsRef<Path>>(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,
),
_ => {}
}
}
}
}

0 comments on commit 98b894b

Please sign in to comment.