Skip to content

Commit b1c48c4

Browse files
authored
Merge pull request #250 from LilyAcorn/refactor-graph-wrapper
Refactor graph wrapper
2 parents 8c9f104 + c17e3f7 commit b1c48c4

5 files changed

Lines changed: 620 additions & 624 deletions

File tree

‎rust/src/errors.rs‎

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
use crate::exceptions::{
2-
CorruptCache, InvalidModuleExpression, ModuleNotPresent, NoSuchContainer, ParseError,
3-
};
1+
use crate::exceptions;
42
use pyo3::PyErr;
53
use pyo3::exceptions::PyValueError;
64
use ruff_python_parser::ParseError as RuffParseError;
75
use thiserror::Error;
86

97
#[derive(Debug, Error)]
10-
pub enum GrimpError {
11-
#[error("Module {0} is not present in the graph.")]
12-
ModuleNotPresent(String),
8+
#[error("Module {0} is not present in the graph.")]
9+
pub struct ModuleNotPresent(pub String);
10+
11+
impl From<ModuleNotPresent> for PyErr {
12+
fn from(value: ModuleNotPresent) -> Self {
13+
exceptions::ModuleNotPresent::new_err(value.to_string())
14+
}
15+
}
1316

17+
#[derive(Debug, Error)]
18+
pub enum GrimpError {
1419
#[error("Container {0} does not exist.")]
1520
NoSuchContainer(String),
1621

@@ -39,16 +44,17 @@ impl From<GrimpError> for PyErr {
3944
fn from(value: GrimpError) -> Self {
4045
// A default mapping from `GrimpError`s to python exceptions.
4146
match value {
42-
GrimpError::ModuleNotPresent(_) => ModuleNotPresent::new_err(value.to_string()),
43-
GrimpError::NoSuchContainer(_) => NoSuchContainer::new_err(value.to_string()),
47+
GrimpError::NoSuchContainer(_) => {
48+
exceptions::NoSuchContainer::new_err(value.to_string())
49+
}
4450
GrimpError::SharedDescendants => PyValueError::new_err(value.to_string()),
4551
GrimpError::InvalidModuleExpression(_) => {
46-
InvalidModuleExpression::new_err(value.to_string())
52+
exceptions::InvalidModuleExpression::new_err(value.to_string())
4753
}
4854
GrimpError::ParseError {
4955
line_number, text, ..
50-
} => PyErr::new::<ParseError, _>((line_number, text)),
51-
GrimpError::CorruptCache(_) => CorruptCache::new_err(value.to_string()),
56+
} => PyErr::new::<exceptions::ParseError, _>((line_number, text)),
57+
GrimpError::CorruptCache(_) => exceptions::CorruptCache::new_err(value.to_string()),
5258
}
5359
}
5460
}

‎rust/src/graph/direct_import_queries.rs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::errors::{GrimpError, GrimpResult};
22
use crate::graph::{
3-
EMPTY_IMPORT_DETAILS, EMPTY_MODULE_TOKENS, ExtendWithDescendants, Graph, ImportDetails,
4-
MODULE_NAMES, ModuleToken,
3+
EMPTY_IMPORT_DETAILS, EMPTY_MODULE_TOKENS, ExtendWithDescendants, Graph, MODULE_NAMES,
4+
ModuleToken, PyImportDetails,
55
};
66
use crate::module_expressions::ModuleExpression;
77
use rustc_hash::FxHashSet;
@@ -49,7 +49,7 @@ impl Graph {
4949
&self,
5050
importer: ModuleToken,
5151
imported: ModuleToken,
52-
) -> &FxHashSet<ImportDetails> {
52+
) -> &FxHashSet<PyImportDetails> {
5353
match self.import_details.get(&(importer, imported)) {
5454
Some(import_details) => import_details,
5555
None => &EMPTY_IMPORT_DETAILS,

‎rust/src/graph/graph_manipulation.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::graph::{
2-
Graph, IMPORT_LINE_CONTENTS, ImportDetails, MODULE_NAMES, Module, ModuleIterator, ModuleToken,
2+
Graph, IMPORT_LINE_CONTENTS, MODULE_NAMES, Module, ModuleIterator, ModuleToken, PyImportDetails,
33
};
44
use rustc_hash::FxHashSet;
55
use slotmap::secondary::Entry;
@@ -137,7 +137,7 @@ impl Graph {
137137
self.import_details
138138
.entry((importer, imported))
139139
.or_default()
140-
.insert(ImportDetails::new(line_number, line_contents));
140+
.insert(PyImportDetails::new(line_number, line_contents));
141141
}
142142
}
143143

0 commit comments

Comments
 (0)