From a29242c586a6e75438e7e6fc70f67a31ff23da88 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 4 Jul 2016 19:41:21 +0200 Subject: [PATCH] Impl Display for Ref --- libimagref/src/reference.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/libimagref/src/reference.rs b/libimagref/src/reference.rs index 5c83e35c..465ca105 100644 --- a/libimagref/src/reference.rs +++ b/libimagref/src/reference.rs @@ -6,8 +6,10 @@ use std::ops::Deref; use std::ops::DerefMut; use std::collections::BTreeMap; use std::fs::File; -use std::io::Read; +use std::io::{Read, Write}; +use std::fmt::{Display, Error as FmtError, Formatter}; use std::fs::Permissions; +use std::result::Result as RResult; use libimagstore::store::FileLockEntry; use libimagstore::storeid::StoreId; @@ -495,6 +497,20 @@ impl<'a> DerefMut for Ref<'a> { } +impl<'a> Display for Ref<'a> { + + fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> { + let path = self.fs_file() + .map(|pb| String::from(pb.to_str().unwrap_or(""))) + .unwrap_or(String::from("Could not read Path from reference object")); + + let hash = self.get_stored_hash().unwrap_or(String::from("")); + + write!(fmt, "Ref({} -> {})", hash, path) + } + +} + fn hash_file_contents(f: &mut File) -> String { let mut hasher = Sha1::new(); let mut s = String::new();