Skip to content

Commit 73bfc90

Browse files
committed
Add ct-python crate.
1 parent ad7937f commit 73bfc90

File tree

4 files changed

+61
-1
lines changed

4 files changed

+61
-1
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ inline-python-macros = { version = "0.5.0", path = "./macros" }
1717
pyo3 = { version = "0.10.0", default-features = false }
1818

1919
[workspace]
20-
members = ["examples"]
20+
members = ["examples", "ct-python"]

ct-python/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "ct-python"
3+
version = "0.0.0"
4+
authors = ["Mara Bos <[email protected]>"]
5+
description = "Execute Python code at compile time to generate Rust code"
6+
license = "BSD-2-Clause"
7+
edition = "2018"
8+
repository = "https://github.com/fusion-engineering/inline-python/tree/master/ct-python"
9+
keywords = ["python", "macro", "generate", "compile-time"]
10+
11+
[dependencies]
12+
inline-python-macros = { version = "0.5.0", path = "../macros" }
File renamed without changes.

ct-python/src/lib.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//! Execute Python code at compile time to generate Rust code.
2+
//!
3+
//! # Example
4+
//!
5+
//! ```
6+
//! #![feature(proc_macro_hygiene)]
7+
//! use ct_python::ct_python;
8+
//!
9+
//! static SIN_2: f64 = ct_python! {
10+
//! from math import sin
11+
//! print(sin(2))
12+
//! };
13+
//!
14+
//! ct_python! {
15+
//! print("type num = f64;")
16+
//! }
17+
//!
18+
//! fn main() {
19+
//! assert_eq!(num::sin(2.0), SIN_2);
20+
//! }
21+
//! ```
22+
//!
23+
//! # How to use
24+
//!
25+
//! Use the `ct_python!{..}` macro to generate Rust code from an embedded
26+
//! Python script.
27+
//! The output of the script (`print()` and anything else through `sys.stdout`)
28+
//! is captured, and will be parsed as Rust code.
29+
//!
30+
//! If you want to use the macro to generate an expression (as in the example),
31+
//! you'll need to add `#![feature(proc_macro_hygiene)]`.
32+
//!
33+
//! ## Python Errors
34+
//!
35+
//! Any syntax errors or runtime exceptions from the Python code will be
36+
//! reported by the Rust compiler as compiler errors.
37+
//!
38+
//! ## Syntax issues
39+
//!
40+
//! Since the Rust tokenizer will tokenize the Python code, some valid Python
41+
//! code is rejected. See [the `inline-python` documentation][1] for details.
42+
//!
43+
//! [1]: https://docs.rs/inline-python/#syntax-issues
44+
45+
/// A block of compile-time executed Rust code generating Python code.
46+
///
47+
/// See [the crate's module level documentation](index.html) for examples.
48+
pub use inline_python_macros::ct_python;

0 commit comments

Comments
 (0)