Skip to content

Commit

Permalink
refactor: minor tweaks found by Clippy (#639)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkniewallner committed Mar 24, 2024
1 parent 64746f2 commit 8ac9dab
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 15 deletions.
5 changes: 2 additions & 3 deletions src/file_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ pub fn read_file(file_path: &str) -> PyResult<String> {
Ok(content) => Ok(content),
Err(e) => match e.kind() {
ErrorKind::NotFound => Err(PyFileNotFoundError::new_err(format!(
"File not found: '{}'",
file_path
"File not found: '{file_path}'",
))),
ErrorKind::InvalidData => {
let file = File::open(path).unwrap();
Expand All @@ -32,7 +31,7 @@ pub fn read_file(file_path: &str) -> PyResult<String> {
.unwrap_or_else(|| guess_encoding(&buffer));
read_with_encoding(&buffer, encoding)
}
_ => Err(PyIOError::new_err(format!("An error occurred: '{}'", e))),
_ => Err(PyIOError::new_err(format!("An error occurred: '{e}'"))),
},
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/imports/ipynb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn _extract_code_from_notebook_cells(cells: &[serde_json::Value]) -> String {
let code_lines: Vec<String> = cells
.iter()
.filter(|cell| cell["cell_type"] == "code")
.flat_map(|cell| cell["source"].as_array())
.filter_map(|cell| cell["source"].as_array())
.flatten()
.filter_map(|line| line.as_str())
.map(str::to_owned)
Expand Down
6 changes: 3 additions & 3 deletions src/imports/shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn get_ast_from_file_content(file_content: &str) -> PyResult<Mod> {
}

/// Iterates through an AST to identify and collect import statements, and returns them together with their
/// respective TextRange for each occurrence.
/// respective `TextRange` for each occurrence.
pub fn extract_imports_from_ast(ast: Mod) -> HashMap<String, Vec<TextRange>> {
let mut visitor = ImportVisitor::new();

Expand Down Expand Up @@ -62,7 +62,7 @@ pub fn convert_imports_with_textranges_to_location_objects(
.column
.get();
Location {
file: file_path.to_string(),
file: file_path.to_owned(),
line: Some(start_line),
column: Some(start_col),
}
Expand All @@ -73,7 +73,7 @@ pub fn convert_imports_with_textranges_to_location_objects(
imports_with_locations
}

/// Transforms a Rust HashMap containing import data into a Python dictionary suitable for Python-side consumption.
/// Transforms a Rust `HashMap` containing import data into a Python dictionary suitable for Python-side consumption.
pub fn convert_to_python_dict(
py: Python<'_>,
imports_with_locations: FileToImportsMap,
Expand Down
10 changes: 5 additions & 5 deletions src/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ pub struct Location {
#[pymethods]
impl Location {
#[new]
pub fn new(file: String, line: Option<usize>, column: Option<usize>) -> Self {
Location { file, line, column }
fn new(file: String, line: Option<usize>, column: Option<usize>) -> Self {
Self { file, line, column }
}

fn __repr__(&self) -> PyResult<String> {
Ok(format!(
fn __repr__(&self) -> String {
format!(
"Location(file='{}', line={:?}, column={:?})",
self.file, self.line, self.column
))
)
}
}
6 changes: 3 additions & 3 deletions src/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub struct ImportVisitor {

impl ImportVisitor {
pub fn new() -> Self {
ImportVisitor {
Self {
imports: HashMap::new(),
}
}
Expand Down Expand Up @@ -49,11 +49,11 @@ impl<'a> Visitor<'a> for ImportVisitor {
}

/// Extracts the top-level module name from a potentially nested module path.
/// e.g. when a module_name is `foo.bar`, this returns `foo`.
/// e.g. when a `module_name` is `foo.bar`, this returns `foo`.
fn get_top_level_module_name(module_name: &str) -> String {
module_name
.split('.')
.next()
.unwrap_or(module_name)
.to_string()
.to_owned()
}

0 comments on commit 8ac9dab

Please sign in to comment.