@@ -9,11 +9,20 @@ pub struct StubInfo {
9
9
}
10
10
11
11
impl StubInfo {
12
+ /// Initialize [StubInfo] from a `pyproject.toml` file in `CARGO_MANIFEST_DIR`.
13
+ /// This is automatically set up by the [crate::define_stub_info_gatherer] macro.
12
14
pub fn from_pyproject_toml ( path : impl AsRef < Path > ) -> Result < Self > {
13
15
let pyproject = PyProject :: parse_toml ( path) ?;
14
16
Ok ( StubInfoBuilder :: from_pyproject_toml ( pyproject) . build ( ) )
15
17
}
16
18
19
+ /// Initialize [StubInfo] with a specific module name and project root.
20
+ /// This must be placed in your PyO3 library crate, i.e. the same crate where [inventory::submit]ted,
21
+ /// not in the `gen_stub` executables due to [inventory]'s mechanism.
22
+ pub fn from_project_root ( default_module_name : String , project_root : PathBuf ) -> Result < Self > {
23
+ Ok ( StubInfoBuilder :: from_project_root ( default_module_name, project_root) . build ( ) )
24
+ }
25
+
17
26
pub fn generate ( & self ) -> Result < ( ) > {
18
27
for ( name, module) in self . modules . iter ( ) {
19
28
let path = name. replace ( "." , "/" ) ;
@@ -47,12 +56,19 @@ struct StubInfoBuilder {
47
56
48
57
impl StubInfoBuilder {
49
58
fn from_pyproject_toml ( pyproject : PyProject ) -> Self {
50
- Self {
51
- modules : BTreeMap :: new ( ) ,
52
- default_module_name : pyproject. module_name ( ) . to_string ( ) ,
53
- python_root : pyproject
59
+ StubInfoBuilder :: from_project_root (
60
+ pyproject. module_name ( ) . to_string ( ) ,
61
+ pyproject
54
62
. python_source ( )
55
63
. unwrap_or ( PathBuf :: from ( std:: env:: var ( "CARGO_MANIFEST_DIR" ) . unwrap ( ) ) ) ,
64
+ )
65
+ }
66
+
67
+ fn from_project_root ( default_module_name : String , project_root : PathBuf ) -> Self {
68
+ Self {
69
+ modules : BTreeMap :: new ( ) ,
70
+ default_module_name,
71
+ python_root : project_root,
56
72
}
57
73
}
58
74
0 commit comments