Skip to content

Commit 45d98ac

Browse files
committed
Implement ModuleNotPresent as a standalone struct
This allows making previously unreachable code paths unrepresentable.
1 parent 9e28eb4 commit 45d98ac

File tree

2 files changed

+23
-25
lines changed

2 files changed

+23
-25
lines changed

rust/src/errors.rs

Lines changed: 15 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,15 @@ 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(_) => exceptions::NoSuchContainer::new_err(value.to_string()),
4448
GrimpError::SharedDescendants => PyValueError::new_err(value.to_string()),
4549
GrimpError::InvalidModuleExpression(_) => {
46-
InvalidModuleExpression::new_err(value.to_string())
50+
exceptions::InvalidModuleExpression::new_err(value.to_string())
4751
}
4852
GrimpError::ParseError {
4953
line_number, text, ..
50-
} => PyErr::new::<ParseError, _>((line_number, text)),
51-
GrimpError::CorruptCache(_) => CorruptCache::new_err(value.to_string()),
54+
} => PyErr::new::<exceptions::ParseError, _>((line_number, text)),
55+
GrimpError::CorruptCache(_) => exceptions::CorruptCache::new_err(value.to_string()),
5256
}
5357
}
5458
}

rust/src/graph/mod.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use std::sync::{LazyLock, RwLock};
1414
use string_interner::backend::StringBackend;
1515
use string_interner::{DefaultSymbol, StringInterner};
1616

17-
use crate::errors::{GrimpError, GrimpResult};
17+
use crate::errors::{GrimpError, GrimpResult, ModuleNotPresent};
1818
use crate::graph::higher_order_queries::{Level, PackageDependency};
1919
use crate::module_expressions::ModuleExpression;
2020

@@ -73,10 +73,10 @@ pub struct Graph {
7373
}
7474

7575
impl Graph {
76-
fn get_visible_module_by_name(&self, name: &str) -> Result<&Module, GrimpError> {
76+
fn get_visible_module_by_name(&self, name: &str) -> Result<&Module, ModuleNotPresent> {
7777
self.get_module_by_name(name)
7878
.filter(|m| !m.is_invisible())
79-
.ok_or(GrimpError::ModuleNotPresent(name.to_owned()))
79+
.ok_or(ModuleNotPresent(name.to_owned()))
8080
}
8181

8282
fn parse_containers(
@@ -87,10 +87,9 @@ impl Graph {
8787
.iter()
8888
.map(|name| match self.get_visible_module_by_name(name) {
8989
Ok(module) => Ok(module),
90-
Err(GrimpError::ModuleNotPresent(_)) => {
90+
Err(ModuleNotPresent(_)) => {
9191
Err(GrimpError::NoSuchContainer(name.into()))?
9292
}
93-
_ => panic!("unexpected error parsing containers"),
9493
})
9594
.collect::<Result<HashSet<_>, GrimpError>>()
9695
}
@@ -124,8 +123,7 @@ impl Graph {
124123
.filter_map(|name| match self.get_visible_module_by_name(&name) {
125124
Ok(module) => Some(module.token()),
126125
// TODO(peter) Error here? Or silently continue (backwards compatibility?)
127-
Err(GrimpError::ModuleNotPresent(_)) => None,
128-
_ => panic!("unexpected error parsing levels"),
126+
Err(ModuleNotPresent(_)) => None,
129127
})
130128
.collect::<FxHashSet<_>>();
131129

@@ -207,11 +205,7 @@ impl Graph {
207205
}
208206

209207
pub fn contains_module(&self, name: &str) -> bool {
210-
match self.get_visible_module_by_name(name) {
211-
Ok(_) => true,
212-
Err(GrimpError::ModuleNotPresent(_)) => false,
213-
_ => panic!("unexpected error checking for module existence"),
214-
}
208+
self.get_visible_module_by_name(name).is_ok()
215209
}
216210

217211
#[pyo3(signature = (module, is_squashed = false))]
@@ -301,7 +295,7 @@ impl Graph {
301295
pub fn find_children(&self, module: &str) -> PyResult<HashSet<String>> {
302296
let module = self
303297
.get_module_by_name(module)
304-
.ok_or(GrimpError::ModuleNotPresent(module.to_owned()))?;
298+
.ok_or(ModuleNotPresent(module.to_owned()))?;
305299
Ok(self
306300
.get_module_children(module.token())
307301
.visible()
@@ -312,7 +306,7 @@ impl Graph {
312306
pub fn find_descendants(&self, module: &str) -> PyResult<HashSet<String>> {
313307
let module = self
314308
.get_module_by_name(module)
315-
.ok_or(GrimpError::ModuleNotPresent(module.to_owned()))?;
309+
.ok_or(ModuleNotPresent(module.to_owned()))?;
316310
Ok(self
317311
.get_module_descendants(module.token())
318312
.visible()

0 commit comments

Comments
 (0)