Skip to content

Commit c17e3f7

Browse files
committed
Implement ModuleNotPresent as a standalone struct
This allows making previously unreachable code paths unrepresentable.
1 parent 33c8f5b commit c17e3f7

2 files changed

Lines changed: 25 additions & 27 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/mod.rs

Lines changed: 8 additions & 16 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;
1919
use crate::graph::higher_order_queries::PackageDependency as PyPackageDependency;
2020
use crate::module_expressions::ModuleExpression;
@@ -80,11 +80,11 @@ pub struct GraphWrapper {
8080
}
8181

8282
impl GraphWrapper {
83-
fn get_visible_module_by_name(&self, name: &str) -> Result<&Module, GrimpError> {
83+
fn get_visible_module_by_name(&self, name: &str) -> Result<&Module, ModuleNotPresent> {
8484
self._graph
8585
.get_module_by_name(name)
8686
.filter(|m| !m.is_invisible())
87-
.ok_or(GrimpError::ModuleNotPresent(name.to_owned()))
87+
.ok_or(ModuleNotPresent(name.to_owned()))
8888
}
8989

9090
fn parse_containers(
@@ -95,10 +95,7 @@ impl GraphWrapper {
9595
.iter()
9696
.map(|name| match self.get_visible_module_by_name(name) {
9797
Ok(module) => Ok(module),
98-
Err(GrimpError::ModuleNotPresent(_)) => {
99-
Err(GrimpError::NoSuchContainer(name.into()))?
100-
}
101-
_ => panic!("unexpected error parsing containers"),
98+
Err(ModuleNotPresent(_)) => Err(GrimpError::NoSuchContainer(name.into()))?,
10299
})
103100
.collect::<Result<HashSet<_>, GrimpError>>()
104101
}
@@ -132,8 +129,7 @@ impl GraphWrapper {
132129
.filter_map(|name| match self.get_visible_module_by_name(&name) {
133130
Ok(module) => Some(module.token()),
134131
// TODO(peter) Error here? Or silently continue (backwards compatibility?)
135-
Err(GrimpError::ModuleNotPresent(_)) => None,
136-
_ => panic!("unexpected error parsing levels"),
132+
Err(ModuleNotPresent(_)) => None,
137133
})
138134
.collect::<FxHashSet<_>>();
139135

@@ -218,11 +214,7 @@ impl GraphWrapper {
218214
}
219215

220216
pub fn contains_module(&self, name: &str) -> bool {
221-
match self.get_visible_module_by_name(name) {
222-
Ok(_) => true,
223-
Err(GrimpError::ModuleNotPresent(_)) => false,
224-
_ => panic!("unexpected error checking for module existence"),
225-
}
217+
self.get_visible_module_by_name(name).is_ok()
226218
}
227219

228220
#[pyo3(signature = (module, is_squashed = false))]
@@ -312,7 +304,7 @@ impl GraphWrapper {
312304
let module = self
313305
._graph
314306
.get_module_by_name(module)
315-
.ok_or(GrimpError::ModuleNotPresent(module.to_owned()))?;
307+
.ok_or(ModuleNotPresent(module.to_owned()))?;
316308
Ok(self
317309
._graph
318310
.get_module_children(module.token())
@@ -325,7 +317,7 @@ impl GraphWrapper {
325317
let module = self
326318
._graph
327319
.get_module_by_name(module)
328-
.ok_or(GrimpError::ModuleNotPresent(module.to_owned()))?;
320+
.ok_or(ModuleNotPresent(module.to_owned()))?;
329321
Ok(self
330322
._graph
331323
.get_module_descendants(module.token())

0 commit comments

Comments
 (0)