From f70ad60e6512da656d293b98e870ba21fdc491be Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Thu, 12 May 2022 11:40:23 -0400 Subject: [PATCH] DirMgr: Improve display for DocSource (Also, implement Display for tor_dirclient::SourceInfo). --- Cargo.lock | 1 + crates/tor-dirclient/Cargo.toml | 1 + crates/tor-dirclient/src/response.rs | 3 ++- crates/tor-dirmgr/src/lib.rs | 14 +++++++++++--- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d79299cca..31fc83d64 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3446,6 +3446,7 @@ version = "0.3.0" dependencies = [ "async-compression", "base64", + "derive_more", "futures", "futures-await-test", "hex", diff --git a/crates/tor-dirclient/Cargo.toml b/crates/tor-dirclient/Cargo.toml index c94a8c081..90dae396e 100644 --- a/crates/tor-dirclient/Cargo.toml +++ b/crates/tor-dirclient/Cargo.toml @@ -29,6 +29,7 @@ tor-rtcompat = { path = "../tor-rtcompat", version = "0.3.0"} async-compression = { version = "0.3.5", features = ["futures-io", "zlib"] } base64 = "0.13.0" +derive_more = "0.99" futures = "0.3.14" hex = "0.4" http = "0.2" diff --git a/crates/tor-dirclient/src/response.rs b/crates/tor-dirclient/src/response.rs index 1f55f030f..08a0d1bd0 100644 --- a/crates/tor-dirclient/src/response.rs +++ b/crates/tor-dirclient/src/response.rs @@ -23,7 +23,8 @@ pub struct DirResponse { /// /// We use this to remember when a request has failed, so we can /// abandon the circuit. -#[derive(Debug, Clone)] +#[derive(Debug, Clone, derive_more::Display)] +#[display(fmt = "{cache_id} via {circuit}")] pub struct SourceInfo { /// Unique identifier for the circuit we're using circuit: UniqId, diff --git a/crates/tor-dirmgr/src/lib.rs b/crates/tor-dirmgr/src/lib.rs index 4107928ee..24e40764e 100644 --- a/crates/tor-dirmgr/src/lib.rs +++ b/crates/tor-dirmgr/src/lib.rs @@ -255,20 +255,28 @@ impl<'a> BoolResetter<'a> { /// /// Used (for example) to report where we got a document from if it fails to /// parse. -#[derive(Debug, Clone, derive_more::Display)] +#[derive(Debug, Clone)] #[non_exhaustive] pub enum DocSource { /// We loaded the document from our cache. - #[display(fmt = "local cache")] LocalCache, /// We fetched the document from a server. - #[display(fmt = "directory server")] DirServer { /// Information about the server we fetched the document from. source: Option, }, } +impl std::fmt::Display for DocSource { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + DocSource::LocalCache => write!(f, "local cache"), + DocSource::DirServer { source: None } => write!(f, "directory server"), + DocSource::DirServer { source: Some(info) } => write!(f, "directory server {}", info), + } + } +} + impl DirMgr { /// Try to load the directory from disk, without launching any /// kind of update process.