Skip to content

Commit

Permalink
Add concise Display implementation for DirectoryTree
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Mar 18, 2024
1 parent 9324629 commit f1d0337
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lighthouse-client/examples/list_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async fn run(url: &str, auth: Authentication) -> Result<()> {
info!("Connected to the Lighthouse server");

let tree = lh.list(&[], ()).await?.payload;
info!("Got {:#?}", tree);
info!("Got {}", tree);

Ok(())
}
Expand Down
22 changes: 21 additions & 1 deletion lighthouse-protocol/src/payload.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{collections::HashMap, fmt};

use serde::{Serialize, Deserialize};

Expand All @@ -18,3 +18,23 @@ pub enum Model {
pub struct DirectoryTree {
pub entries: HashMap<String, Option<DirectoryTree>>,
}

impl fmt::Display for DirectoryTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{")?;
let count = self.entries.len();
for (i, (key, value)) in self.entries.iter().enumerate() {
write!(f, "\"{}\": ", key)?;
if let Some(value) = value {
write!(f, "{}", value)?;
} else {
write!(f, "None")?;
}
if i < count - 1 {
write!(f, ", ")?;
}
}
write!(f, "}}")?;
Ok(())
}
}

0 comments on commit f1d0337

Please sign in to comment.