diff --git a/gix-ref/src/fullname.rs b/gix-ref/src/fullname.rs index eb6ac854dde..423baf5a26b 100644 --- a/gix-ref/src/fullname.rs +++ b/gix-ref/src/fullname.rs @@ -1,6 +1,5 @@ -use std::{borrow::Borrow, path::Path}; - use gix_object::bstr::{BStr, BString, ByteSlice}; +use std::{borrow::Borrow, path::Path}; use crate::{bstr::ByteVec, name::is_pseudo_ref, Category, FullName, FullNameRef, Namespace, PartialNameRef}; @@ -73,6 +72,12 @@ impl std::fmt::Display for FullName { } } +impl std::fmt::Display for FullNameRef { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } +} + impl FullNameRef { /// Interpret this fully qualified reference name as partial name. pub fn as_partial_name(&self) -> &PartialNameRef { diff --git a/gix-ref/src/name.rs b/gix-ref/src/name.rs index f48a2691420..e6d58f21386 100644 --- a/gix-ref/src/name.rs +++ b/gix-ref/src/name.rs @@ -266,6 +266,18 @@ impl convert::TryFrom for PartialName { } } +impl std::fmt::Display for PartialName { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } +} + +impl std::fmt::Display for PartialNameRef { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + std::fmt::Display::fmt(&self.0, f) + } +} + /// Note that this method is disagreeing with `gix_validate` as it allows dashes '-' for some reason. /// Since partial names cannot be created with dashes inside we adjusted this as it's probably unintended or git creates pseudo-refs /// which wouldn't pass its safety checks. diff --git a/gix-ref/tests/refs/fullname.rs b/gix-ref/tests/refs/fullname.rs index edf2b4fa18e..3208b19dde6 100644 --- a/gix-ref/tests/refs/fullname.rs +++ b/gix-ref/tests/refs/fullname.rs @@ -1,6 +1,6 @@ use std::borrow::Cow; -use gix_ref::{Category, FullNameRef, PartialNameRef}; +use gix_ref::{Category, FullName, FullNameRef, PartialNameRef}; #[test] fn cow() { @@ -137,3 +137,12 @@ fn prefix_with_namespace_and_stripping() { "idempotent stripping" ); } + +#[test] +fn display() { + let full_name = FullName::try_from("refs/heads/main").unwrap(); + assert_eq!(format!("{full_name}"), "refs/heads/main"); + + let full_name_ref = full_name.as_ref(); + assert_eq!(format!("{full_name_ref}"), "refs/heads/main"); +} diff --git a/gix-ref/tests/refs/main.rs b/gix-ref/tests/refs/main.rs index ddd06716f88..bfa056c499a 100644 --- a/gix-ref/tests/refs/main.rs +++ b/gix-ref/tests/refs/main.rs @@ -36,6 +36,15 @@ mod partialname { ); Ok(()) } + + #[test] + fn display() { + let partial_name = PartialName::try_from("heads/main").unwrap(); + assert_eq!(format!("{partial_name}"), "heads/main"); + + let partial_name_ref = partial_name.as_ref(); + assert_eq!(format!("{partial_name_ref}"), "heads/main"); + } } mod namespace; mod packed;