Skip to content

Commit 0a576b9

Browse files
authored
Merge pull request #2196 from GitoxideLabs/copilot/fix-713956e8-cf8b-499a-99a8-92001948c9f8
Implement `std::fmt::Display` for `FullNameRef`, `PartialName`, and `PartialNameRef`
2 parents 9c686d9 + e670294 commit 0a576b9

File tree

4 files changed

+38
-3
lines changed

4 files changed

+38
-3
lines changed

gix-ref/src/fullname.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
use std::{borrow::Borrow, path::Path};
2-
31
use gix_object::bstr::{BStr, BString, ByteSlice};
2+
use std::{borrow::Borrow, path::Path};
43

54
use crate::{bstr::ByteVec, name::is_pseudo_ref, Category, FullName, FullNameRef, Namespace, PartialNameRef};
65

@@ -73,6 +72,12 @@ impl std::fmt::Display for FullName {
7372
}
7473
}
7574

75+
impl std::fmt::Display for FullNameRef {
76+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77+
std::fmt::Display::fmt(&self.0, f)
78+
}
79+
}
80+
7681
impl FullNameRef {
7782
/// Interpret this fully qualified reference name as partial name.
7883
pub fn as_partial_name(&self) -> &PartialNameRef {

gix-ref/src/name.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,18 @@ impl convert::TryFrom<BString> for PartialName {
266266
}
267267
}
268268

269+
impl std::fmt::Display for PartialName {
270+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
271+
std::fmt::Display::fmt(&self.0, f)
272+
}
273+
}
274+
275+
impl std::fmt::Display for PartialNameRef {
276+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
277+
std::fmt::Display::fmt(&self.0, f)
278+
}
279+
}
280+
269281
/// Note that this method is disagreeing with `gix_validate` as it allows dashes '-' for some reason.
270282
/// Since partial names cannot be created with dashes inside we adjusted this as it's probably unintended or git creates pseudo-refs
271283
/// which wouldn't pass its safety checks.

gix-ref/tests/refs/fullname.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22

3-
use gix_ref::{Category, FullNameRef, PartialNameRef};
3+
use gix_ref::{Category, FullName, FullNameRef, PartialNameRef};
44

55
#[test]
66
fn cow() {
@@ -137,3 +137,12 @@ fn prefix_with_namespace_and_stripping() {
137137
"idempotent stripping"
138138
);
139139
}
140+
141+
#[test]
142+
fn display() {
143+
let full_name = FullName::try_from("refs/heads/main").unwrap();
144+
assert_eq!(format!("{full_name}"), "refs/heads/main");
145+
146+
let full_name_ref = full_name.as_ref();
147+
assert_eq!(format!("{full_name_ref}"), "refs/heads/main");
148+
}

gix-ref/tests/refs/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,15 @@ mod partialname {
3636
);
3737
Ok(())
3838
}
39+
40+
#[test]
41+
fn display() {
42+
let partial_name = PartialName::try_from("heads/main").unwrap();
43+
assert_eq!(format!("{partial_name}"), "heads/main");
44+
45+
let partial_name_ref = partial_name.as_ref();
46+
assert_eq!(format!("{partial_name_ref}"), "heads/main");
47+
}
3948
}
4049
mod namespace;
4150
mod packed;

0 commit comments

Comments
 (0)