@@ -145,6 +145,8 @@ allow-direct-references = true
145
145
{%- elif build_system == "maturin" %}
146
146
147
147
[tool.maturin]
148
+ python-source = "python"
149
+ module-name = {{ name_safe ~ "._lowlevel" }}
148
150
features = ["pyo3/extension-module"]
149
151
150
152
{%- endif %}
@@ -183,21 +185,27 @@ fn hello() -> PyResult<String> {
183
185
184
186
/// A Python module implemented in Rust.
185
187
#[pymodule]
186
- fn rustdemo (_py: Python, m: &PyModule) -> PyResult<()> {
188
+ fn _lowlevel (_py: Python, m: &PyModule) -> PyResult<()> {
187
189
m.add_function(wrap_pyfunction!(hello, m)?)?;
188
190
Ok(())
189
191
}
190
192
"# ;
191
193
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
+
192
200
/// Template for the Cargo.toml
193
201
const CARGO_TOML_TEMPLATE : & str = r#"[package]
194
- name = " {{ name }}"
202
+ name = {{ name }}
195
203
version = "0.1.0"
196
204
edition = "2021"
197
205
198
206
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
199
207
[lib]
200
- name = " {{ name }}"
208
+ name = {{ name_safe }}
201
209
crate-type = ["cdylib"]
202
210
203
211
[dependencies]
@@ -382,12 +390,14 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
382
390
} ;
383
391
384
392
let private = cmd. private ;
393
+ let name_safe = metadata. name . as_ref ( ) . unwrap ( ) . replace ( '-' , "_" ) ;
385
394
386
395
let rv = env. render_named_str (
387
396
"pyproject.json" ,
388
397
TOML_TEMPLATE ,
389
398
context ! {
390
399
name => metadata. name,
400
+ name_safe => name_safe,
391
401
description => metadata. description,
392
402
version => metadata. version,
393
403
author => metadata. author,
@@ -407,10 +417,28 @@ pub fn execute(cmd: Args) -> Result<(), Error> {
407
417
if !imported_something && !src_dir. is_dir ( ) {
408
418
let name = metadata. name . expect ( "project name" ) ;
409
419
if is_rust {
420
+ let project_dir = src_dir. join ( "python" ) . join ( name. replace ( '-' , "_" ) ) ;
421
+ fs:: create_dir_all ( & project_dir) . ok ( ) ;
410
422
let rv = env. render_named_str ( "lib.rs" , LIB_RS_TEMPLATE , context ! { name } ) ?;
411
423
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
+ ) ?;
413
432
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" ) ?;
414
442
} else {
415
443
let project_dir = src_dir. join ( name. replace ( '-' , "_" ) ) ;
416
444
fs:: create_dir_all ( & project_dir) . ok ( ) ;
0 commit comments