DirMgr: Improve display for DocSource

(Also, implement Display for tor_dirclient::SourceInfo).
This commit is contained in:
Nick Mathewson 2022-05-12 11:40:23 -04:00
parent ef2640acfa
commit f70ad60e65
4 changed files with 15 additions and 4 deletions

1
Cargo.lock generated
View File

@ -3446,6 +3446,7 @@ version = "0.3.0"
dependencies = [ dependencies = [
"async-compression", "async-compression",
"base64", "base64",
"derive_more",
"futures", "futures",
"futures-await-test", "futures-await-test",
"hex", "hex",

View File

@ -29,6 +29,7 @@ tor-rtcompat = { path = "../tor-rtcompat", version = "0.3.0"}
async-compression = { version = "0.3.5", features = ["futures-io", "zlib"] } async-compression = { version = "0.3.5", features = ["futures-io", "zlib"] }
base64 = "0.13.0" base64 = "0.13.0"
derive_more = "0.99"
futures = "0.3.14" futures = "0.3.14"
hex = "0.4" hex = "0.4"
http = "0.2" http = "0.2"

View File

@ -23,7 +23,8 @@ pub struct DirResponse {
/// ///
/// We use this to remember when a request has failed, so we can /// We use this to remember when a request has failed, so we can
/// abandon the circuit. /// abandon the circuit.
#[derive(Debug, Clone)] #[derive(Debug, Clone, derive_more::Display)]
#[display(fmt = "{cache_id} via {circuit}")]
pub struct SourceInfo { pub struct SourceInfo {
/// Unique identifier for the circuit we're using /// Unique identifier for the circuit we're using
circuit: UniqId, circuit: UniqId,

View File

@ -255,20 +255,28 @@ impl<'a> BoolResetter<'a> {
/// ///
/// Used (for example) to report where we got a document from if it fails to /// Used (for example) to report where we got a document from if it fails to
/// parse. /// parse.
#[derive(Debug, Clone, derive_more::Display)] #[derive(Debug, Clone)]
#[non_exhaustive] #[non_exhaustive]
pub enum DocSource { pub enum DocSource {
/// We loaded the document from our cache. /// We loaded the document from our cache.
#[display(fmt = "local cache")]
LocalCache, LocalCache,
/// We fetched the document from a server. /// We fetched the document from a server.
#[display(fmt = "directory server")]
DirServer { DirServer {
/// Information about the server we fetched the document from. /// Information about the server we fetched the document from.
source: Option<SourceInfo>, source: Option<SourceInfo>,
}, },
} }
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<R: Runtime> DirMgr<R> { impl<R: Runtime> DirMgr<R> {
/// Try to load the directory from disk, without launching any /// Try to load the directory from disk, without launching any
/// kind of update process. /// kind of update process.