Skip to content

Commit

Permalink
Rust project creation bugfix and improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Aug 29, 2023
1 parent ad24ee8 commit 2a90103
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
4 changes: 0 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
This file contains tracks the changes landing in Rye. It includes changes
that were not yet released.

## 0.14.0

_Unreleased_

<!-- released start -->

## 0.13.0
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions docs/guide/rust.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ The following structure will be created:
├── README.md
├── pyproject.toml
├── Cargo.toml
├── python
└── my_project
└── __init__.py
└── src
└── lib.rs
```
Expand Down
2 changes: 1 addition & 1 deletion rye/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rye"
version = "0.14.0"
version = "0.13.0"
edition = "2021"
license = "MIT"

Expand Down
36 changes: 32 additions & 4 deletions rye/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ allow-direct-references = true
{%- elif build_system == "maturin" %}
[tool.maturin]
python-source = "python"
module-name = {{ name_safe ~ "._lowlevel" }}
features = ["pyo3/extension-module"]
{%- endif %}
Expand Down Expand Up @@ -183,21 +185,27 @@ fn hello() -> PyResult<String> {
/// A Python module implemented in Rust.
#[pymodule]
fn rustdemo(_py: Python, m: &PyModule) -> PyResult<()> {
fn _lowlevel(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_function(wrap_pyfunction!(hello, m)?)?;
Ok(())
}
"#;

/// Template for the __init__.py
const RUST_INIT_PY_TEMPLATE: &str = r#"from {{ name_safe }}._lowlevel import hello
__all__ = ["hello"]
"#;

/// Template for the Cargo.toml
const CARGO_TOML_TEMPLATE: &str = r#"[package]
name = "{{ name }}"
name = {{ name }}
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "{{ name }}"
name = {{ name_safe }}
crate-type = ["cdylib"]
[dependencies]
Expand Down Expand Up @@ -382,12 +390,14 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
};

let private = cmd.private;
let name_safe = metadata.name.as_ref().unwrap().replace('-', "_");

let rv = env.render_named_str(
"pyproject.json",
TOML_TEMPLATE,
context! {
name => metadata.name,
name_safe => name_safe,
description => metadata.description,
version => metadata.version,
author => metadata.author,
Expand All @@ -407,10 +417,28 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
if !imported_something && !src_dir.is_dir() {
let name = metadata.name.expect("project name");
if is_rust {
let project_dir = src_dir.join("python").join(name.replace('-', "_"));
fs::create_dir_all(&project_dir).ok();
let rv = env.render_named_str("lib.rs", LIB_RS_TEMPLATE, context! { name })?;
fs::write(src_dir.join("lib.rs"), rv).context("failed to write lib.rs")?;
let rv = env.render_named_str("Cargo.toml", CARGO_TOML_TEMPLATE, context! { name })?;
let rv = env.render_named_str(
"Cargo.json",
CARGO_TOML_TEMPLATE,
context! {
name,
name_safe,
},
)?;
fs::write(dir.join("Cargo.toml"), rv).context("failed to write Cargo.toml")?;
let rv = env.render_named_str(
"__init__.py",
RUST_INIT_PY_TEMPLATE,
context! {
name_safe
},
)?;
fs::write(project_dir.join("__init__.py"), rv)
.context("failed to write __init__.py")?;
} else {
let project_dir = src_dir.join(name.replace('-', "_"));
fs::create_dir_all(&project_dir).ok();
Expand Down

0 comments on commit 2a90103

Please sign in to comment.