Skip to content

Commit 65922f1

Browse files
committed
Update after review
Signed-off-by: Nicolae Dicu <[email protected]>
1 parent 443a2d7 commit 65922f1

File tree

6 files changed

+55
-63
lines changed

6 files changed

+55
-63
lines changed

crates/analyzer/src/analyze/crate_.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn analyze_crate(path: &str) -> Result<AnalysisResult> {
1010
// make the path absolute
1111
// TODO we use dunce to canonicalize the path because otherwise there is issues with python's os.path.relpath on windows, but maybe we should fix this on the Python side
1212
let crate_dir =
13-
dunce::canonicalize(path).context(format!("Error resolving crate path: {}", path))?;
13+
dunce::canonicalize(path).context(format!("Error resolving crate path: {path}"))?;
1414
eprintln!("running new analyzer");
1515
// check the path is a directory
1616
if !crate_dir.is_dir() {
@@ -43,7 +43,12 @@ pub fn analyze_crate(path: &str) -> Result<AnalysisResult> {
4343
.targets
4444
.iter()
4545
.find(|t| t.kind.contains(&"lib".into()))
46-
.or_else(|| root_pkg.targets.iter().find(|t| t.kind.contains(&"bin".into())))
46+
.or_else(|| {
47+
root_pkg
48+
.targets
49+
.iter()
50+
.find(|t| t.kind.contains(&"bin".into()))
51+
})
4752
.ok_or_else(|| anyhow!("No lib or bin target defined in manifest"))?;
4853

4954
let crate_name = root_target.name.clone();
@@ -61,15 +66,11 @@ pub fn analyze_crate(path: &str) -> Result<AnalysisResult> {
6166

6267
// read the top-level module
6368
let content = std::fs::read_to_string(&root_module)?;
64-
let (module, structs, enums, functions) = Module::parse(
65-
Some(&root_module),
66-
&[&result.crate_.name],
67-
&content,
68-
)
69-
.context(format!(
70-
"Error parsing module {}",
71-
root_module.to_string_lossy()
72-
))?;
69+
let (module, structs, enums, functions) =
70+
Module::parse(Some(&root_module), &[&result.crate_.name], &content).context(format!(
71+
"Error parsing module {}",
72+
root_module.to_string_lossy()
73+
))?;
7374

7475
let mut modules_to_read = module
7576
.declarations
@@ -91,20 +92,21 @@ pub fn analyze_crate(path: &str) -> Result<AnalysisResult> {
9192
// recursively find/read the public sub‑modules
9293
let mut read_modules = vec![];
9394
while let Some((parent_dir, module_name, parent)) = modules_to_read.pop() {
94-
let (module_path, submodule_dir) = if parent_dir.join(&module_name).with_extension("rs").exists() {
95-
(
96-
parent_dir.join(&module_name).with_extension("rs"),
97-
parent_dir.join(&module_name),
98-
)
99-
} else if parent_dir.join(&module_name).join("mod.rs").exists() {
100-
(
101-
parent_dir.join(&module_name).join("mod.rs"),
102-
parent_dir.to_path_buf(),
103-
)
104-
} else {
105-
// TODO warn about missing module?
106-
continue;
107-
};
95+
let (module_path, submodule_dir) =
96+
if parent_dir.join(&module_name).with_extension("rs").exists() {
97+
(
98+
parent_dir.join(&module_name).with_extension("rs"),
99+
parent_dir.join(&module_name),
100+
)
101+
} else if parent_dir.join(&module_name).join("mod.rs").exists() {
102+
(
103+
parent_dir.join(&module_name).join("mod.rs"),
104+
parent_dir.to_path_buf(),
105+
)
106+
} else {
107+
// TODO warn about missing module?
108+
continue;
109+
};
108110

109111
if read_modules.contains(&module_path) {
110112
continue;

crates/py_binding/src/data_query.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ where
2626
Ok(crate_) => crate_,
2727
Err(err) => {
2828
return Err(PyIOError::new_err(format!(
29-
"Could not deserialize {}: {}",
30-
name, err
29+
"Could not deserialize {name}: {err}"
3130
)))
3231
}
3332
};
@@ -39,7 +38,7 @@ where
3938
pub fn load_crate(cache_path: &str, name: &str) -> PyResult<Option<Crate>> {
4039
let path = std::path::Path::new(cache_path)
4140
.join("crates")
42-
.join(format!("{}.json", name));
41+
.join(format!("{name}.json"));
4342
if !path.exists() {
4443
return Ok(None);
4544
}
@@ -53,7 +52,7 @@ pub fn load_crate(cache_path: &str, name: &str) -> PyResult<Option<Crate>> {
5352
pub fn load_module(cache_path: &str, full_name: &str) -> PyResult<Option<Module>> {
5453
let path = std::path::Path::new(cache_path)
5554
.join("modules")
56-
.join(format!("{}.json", full_name));
55+
.join(format!("{full_name}.json"));
5756
if !path.exists() {
5857
return Ok(None);
5958
}
@@ -67,7 +66,7 @@ pub fn load_module(cache_path: &str, full_name: &str) -> PyResult<Option<Module>
6766
pub fn load_struct(cache_path: &str, full_name: &str) -> PyResult<Option<Struct>> {
6867
let path = std::path::Path::new(cache_path)
6968
.join("structs")
70-
.join(format!("{}.json", full_name));
69+
.join(format!("{full_name}.json"));
7170
if !path.exists() {
7271
return Ok(None);
7372
}
@@ -81,7 +80,7 @@ pub fn load_struct(cache_path: &str, full_name: &str) -> PyResult<Option<Struct>
8180
pub fn load_enum(cache_path: &str, full_name: &str) -> PyResult<Option<Enum>> {
8281
let path = std::path::Path::new(cache_path)
8382
.join("enums")
84-
.join(format!("{}.json", full_name));
83+
.join(format!("{full_name}.json"));
8584
if !path.exists() {
8685
return Ok(None);
8786
}
@@ -95,7 +94,7 @@ pub fn load_enum(cache_path: &str, full_name: &str) -> PyResult<Option<Enum>> {
9594
pub fn load_function(cache_path: &str, full_name: &str) -> PyResult<Option<Function>> {
9695
let path = std::path::Path::new(cache_path)
9796
.join("functions")
98-
.join(format!("{}.json", full_name));
97+
.join(format!("{full_name}.json"));
9998
if !path.exists() {
10099
return Ok(None);
101100
}
@@ -106,14 +105,8 @@ pub fn load_function(cache_path: &str, full_name: &str) -> PyResult<Option<Funct
106105

107106
/// Check if a path is a child of a given parent, and return the fully qualified name of the child.
108107
fn is_child(path: &std::path::PathBuf, parent: &Vec<String>) -> Option<String> {
109-
let name = match path.file_stem() {
110-
Some(name) => name,
111-
None => return None,
112-
};
113-
let name = match name.to_str() {
114-
Some(name) => name,
115-
None => return None,
116-
};
108+
let name = path.file_stem()?;
109+
let name = name.to_str()?;
117110
let name_path = name.split("::").collect::<Vec<_>>();
118111
if name_path.len() != parent.len() + 1 {
119112
return None;

crates/py_binding/src/lib.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,25 +162,20 @@ where
162162
Ok(value) => value,
163163
Err(err) => {
164164
return Err(PyIOError::new_err(format!(
165-
"Could not serialize value: {}",
166-
err
165+
"Could not serialize value: {err}"
167166
)))
168167
}
169168
};
170169
if path.exists() {
171-
match std::fs::read_to_string(path) {
172-
Ok(old_value) => {
173-
if value == old_value {
174-
return Ok(());
175-
}
170+
if let Ok(old_value) = std::fs::read_to_string(path) {
171+
if value == old_value {
172+
return Ok(());
176173
}
177-
Err(_) => {}
178174
};
179175
}
180176
match std::fs::write(path, value) {
181177
Err(err) => Err(PyIOError::new_err(format!(
182-
"Could not write value to file: {}",
183-
err
178+
"Could not write value to file: {err}"
184179
))),
185180
Ok(_) => Ok(()),
186181
}

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ classifiers = [
3737
"Topic :: Text Processing :: Markup",
3838
]
3939
dependencies = [
40-
"sphinx>=7.3"
40+
"sphinx>=8,<9"
4141
]
4242

4343
[project.urls]

python/sphinx_rust/directives/_core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class RustAutoDirective(SphinxDirective):
3131

3232
@property
3333
def doc(self) -> nodes.document:
34-
return self.state.document # type: ignore[no-any-return]
34+
return self.state.document
3535

3636
@property
3737
def rust_config(self) -> RustConfig:

python/sphinx_rust/domain.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
from dataclasses import dataclass
44
import os
55
from pathlib import Path
6+
import re
67
import shutil
78
from typing import TYPE_CHECKING, Literal, TypedDict
8-
import re
99

1010
from sphinx import addnodes
1111
from sphinx.domains import Domain
@@ -21,7 +21,6 @@
2121
from sphinx_rust.directives.struct import RustStructAutoDirective
2222
from sphinx_rust.sphinx_rust import analyze_crate, load_descendant_modules
2323

24-
2524
INVALID_CHARS = r"[^A-Za-z0-9._-]"
2625

2726

@@ -34,6 +33,7 @@ def slugify_rust_name(fullname: str) -> Path:
3433
cleaned = [re.sub(INVALID_CHARS, "_", p) for p in parts]
3534
return Path(*cleaned)
3635

36+
3737
if TYPE_CHECKING:
3838
from docutils.nodes import Element
3939
from sphinx.addnodes import pending_xref
@@ -248,7 +248,7 @@ def create_object_pages(folder: Path, otype: str, names: list[str]) -> None:
248248
"",
249249
".. toctree::",
250250
" :maxdepth: 1",
251-
""
251+
"",
252252
]
253253

254254
for rust_name in names:
@@ -280,12 +280,14 @@ def create_code_pages(crate_name: str, srcdir: Path, cache: Path) -> None:
280280
dst.parent.mkdir(parents=True, exist_ok=True)
281281

282282
dst.write_text(
283-
"\n".join((
284-
":orphan:",
285-
"",
286-
f".. literalinclude:: {os.path.relpath(file_path, dst.parent)}",
287-
f" :name: rust-code:{full_name}",
288-
" :language: rust",
289-
" :linenos:",
290-
))
283+
"\n".join(
284+
(
285+
":orphan:",
286+
"",
287+
f".. literalinclude:: {os.path.relpath(file_path, dst.parent)}",
288+
f" :name: rust-code:{full_name}",
289+
" :language: rust",
290+
" :linenos:",
291+
)
292+
)
291293
)

0 commit comments

Comments
 (0)