From 3df883c95ddc76b2a6dd10c93702d91dccd1eceb Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Wed, 9 Oct 2024 15:04:54 -0400 Subject: [PATCH] uv-pep508: add Debug impl for MarkerTree's actual internal representation I found myself using this more verbose representation to double check that there wasn't any other "hidden" state occurring in markers, and that the graph debug display wasn't hiding anything that I was missing. --- crates/uv-pep508/src/marker/tree.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/crates/uv-pep508/src/marker/tree.rs b/crates/uv-pep508/src/marker/tree.rs index 55902a69a..f8c418115 100644 --- a/crates/uv-pep508/src/marker/tree.rs +++ b/crates/uv-pep508/src/marker/tree.rs @@ -1238,6 +1238,15 @@ impl MarkerTree { MarkerTreeDebugGraph { marker: self } } + /// Formats a [`MarkerTree`] in its "raw" representation. + /// + /// This is useful for debugging when one wants to look at a + /// representation of a `MarkerTree` that is precisely identical + /// to its internal representation. + pub fn debug_raw(&self) -> MarkerTreeDebugRaw<'_> { + MarkerTreeDebugRaw { marker: self } + } + fn fmt_graph(&self, f: &mut fmt::Formatter<'_>, level: usize) -> fmt::Result { match self.kind() { MarkerTreeKind::True => return write!(f, "true"), @@ -1341,6 +1350,24 @@ impl<'a> fmt::Debug for MarkerTreeDebugGraph<'a> { } } +/// Formats a [`MarkerTree`] using its raw internals. +/// +/// This is very verbose and likely only useful if you're working +/// on the internals of this crate. +/// +/// This type is created by the [`MarkerTree::debug_raw`] routine. +#[derive(Clone)] +pub struct MarkerTreeDebugRaw<'a> { + marker: &'a MarkerTree, +} + +impl<'a> fmt::Debug for MarkerTreeDebugRaw<'a> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let node = INTERNER.shared.node(self.marker.0); + f.debug_tuple("MarkerTreeDebugRaw").field(node).finish() + } +} + /// The underlying kind of an arbitrary node in a [`MarkerTree`]. /// /// A marker tree is represented as an algebraic decision tree with two terminal nodes