Skip to content

Commit 2460bc0

Browse files
committed
Add StubInfo::from_project_root
1 parent c78176a commit 2460bc0

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

pyo3-stub-gen/src/generate/stub_info.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ pub struct StubInfo {
99
}
1010

1111
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.
1214
pub fn from_pyproject_toml(path: impl AsRef<Path>) -> Result<Self> {
1315
let pyproject = PyProject::parse_toml(path)?;
1416
Ok(StubInfoBuilder::from_pyproject_toml(pyproject).build())
1517
}
1618

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+
1726
pub fn generate(&self) -> Result<()> {
1827
for (name, module) in self.modules.iter() {
1928
let path = name.replace(".", "/");
@@ -47,12 +56,19 @@ struct StubInfoBuilder {
4756

4857
impl StubInfoBuilder {
4958
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
5462
.python_source()
5563
.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,
5672
}
5773
}
5874

0 commit comments

Comments
 (0)