Skip to content

Commit 2a90103

Browse files
committed
Rust project creation bugfix and improvement
1 parent ad24ee8 commit 2a90103

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

CHANGELOG.md

-4
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@
33
This file contains tracks the changes landing in Rye. It includes changes
44
that were not yet released.
55

6-
## 0.14.0
7-
8-
_Unreleased_
9-
106
<!-- released start -->
117

128
## 0.13.0

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/guide/rust.md

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ The following structure will be created:
2121
├── README.md
2222
├── pyproject.toml
2323
├── Cargo.toml
24+
├── python
25+
└── my_project
26+
└── __init__.py
2427
└── src
2528
└── lib.rs
2629
```

rye/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "rye"
3-
version = "0.14.0"
3+
version = "0.13.0"
44
edition = "2021"
55
license = "MIT"
66

rye/src/cli/init.rs

+32-4
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ allow-direct-references = true
145145
{%- elif build_system == "maturin" %}
146146
147147
[tool.maturin]
148+
python-source = "python"
149+
module-name = {{ name_safe ~ "._lowlevel" }}
148150
features = ["pyo3/extension-module"]
149151
150152
{%- endif %}
@@ -183,21 +185,27 @@ fn hello() -> PyResult<String> {
183185
184186
/// A Python module implemented in Rust.
185187
#[pymodule]
186-
fn rustdemo(_py: Python, m: &PyModule) -> PyResult<()> {
188+
fn _lowlevel(_py: Python, m: &PyModule) -> PyResult<()> {
187189
m.add_function(wrap_pyfunction!(hello, m)?)?;
188190
Ok(())
189191
}
190192
"#;
191193

194+
/// Template for the __init__.py
195+
const RUST_INIT_PY_TEMPLATE: &str = r#"from {{ name_safe }}._lowlevel import hello
196+
__all__ = ["hello"]
197+
198+
"#;
199+
192200
/// Template for the Cargo.toml
193201
const CARGO_TOML_TEMPLATE: &str = r#"[package]
194-
name = "{{ name }}"
202+
name = {{ name }}
195203
version = "0.1.0"
196204
edition = "2021"
197205
198206
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
199207
[lib]
200-
name = "{{ name }}"
208+
name = {{ name_safe }}
201209
crate-type = ["cdylib"]
202210
203211
[dependencies]
@@ -382,12 +390,14 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
382390
};
383391

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

386395
let rv = env.render_named_str(
387396
"pyproject.json",
388397
TOML_TEMPLATE,
389398
context! {
390399
name => metadata.name,
400+
name_safe => name_safe,
391401
description => metadata.description,
392402
version => metadata.version,
393403
author => metadata.author,
@@ -407,10 +417,28 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
407417
if !imported_something && !src_dir.is_dir() {
408418
let name = metadata.name.expect("project name");
409419
if is_rust {
420+
let project_dir = src_dir.join("python").join(name.replace('-', "_"));
421+
fs::create_dir_all(&project_dir).ok();
410422
let rv = env.render_named_str("lib.rs", LIB_RS_TEMPLATE, context! { name })?;
411423
fs::write(src_dir.join("lib.rs"), rv).context("failed to write lib.rs")?;
412-
let rv = env.render_named_str("Cargo.toml", CARGO_TOML_TEMPLATE, context! { name })?;
424+
let rv = env.render_named_str(
425+
"Cargo.json",
426+
CARGO_TOML_TEMPLATE,
427+
context! {
428+
name,
429+
name_safe,
430+
},
431+
)?;
413432
fs::write(dir.join("Cargo.toml"), rv).context("failed to write Cargo.toml")?;
433+
let rv = env.render_named_str(
434+
"__init__.py",
435+
RUST_INIT_PY_TEMPLATE,
436+
context! {
437+
name_safe
438+
},
439+
)?;
440+
fs::write(project_dir.join("__init__.py"), rv)
441+
.context("failed to write __init__.py")?;
414442
} else {
415443
let project_dir = src_dir.join(name.replace('-', "_"));
416444
fs::create_dir_all(&project_dir).ok();

0 commit comments

Comments
 (0)