Skip to content

Commit 7197edf

Browse files
committed
Implement code generation example
Main goal is to create simple crate that generates code while compilation (see `build.rs`) and includes it via `include!(concat!(env!("OUT_DIR"), "/generated-code.rs"));`. It should help to create proper tests in intellij-rust plugin
1 parent bda4c81 commit 7197edf

File tree

6 files changed

+44
-0
lines changed

6 files changed

+44
-0
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/target
2+
**/*.rs.bk
3+
.idea
4+
*.iml

Cargo.lock

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "code-generation-example"
3+
version = "0.1.0"
4+
authors = ["Arseniy Pendryak <[email protected]>"]
5+
edition = "2018"
6+
7+
license = "MIT"
8+
description = "Simple lib with code generation example"
9+
homepage = "https://github.com/intellij-rust/code-generation-example"
10+
repository = "https://github.com/intellij-rust/code-generation-example"
11+
documentation = "https://docs.rs/code-generation-example"

build.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::env;
2+
use std::fs::File;
3+
use std::io::Write;
4+
use std::path::Path;
5+
6+
fn main() {
7+
let out_dir = env::var("OUT_DIR").unwrap();
8+
let dest_path = Path::new(&out_dir).join("hello.rs");
9+
let mut f = File::create(&dest_path).unwrap();
10+
11+
f.write_all(b"
12+
pub fn message() -> &'static str {
13+
\"Hello, World!\"
14+
}",
15+
)
16+
.unwrap();
17+
}

examples/example.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
use code_generation_example::message;
2+
3+
fn main() {
4+
println!("{}", message());
5+
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
include!(concat!(env!("OUT_DIR"), "/hello.rs"));

0 commit comments

Comments
 (0)