Skip to content

Commit fc7fa92

Browse files
[gix-object] EntryMode: Hide the internal data representation
We'll want to change it to fix issue GitoxideLabs#1887. First step: don't expose it.
1 parent 0ca9525 commit fc7fa92

File tree

4 files changed

+43
-18
lines changed

4 files changed

+43
-18
lines changed

gitoxide-core/src/repository/diff.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn write_changes(
4949
} => {
5050
writeln!(out, "A: {}", typed_location(location, entry_mode))?;
5151
writeln!(out, " {}", id.attach(repo).shorten_or_id())?;
52-
writeln!(out, " -> {:o}", entry_mode.0)?;
52+
writeln!(out, " -> {:o}", entry_mode)?;
5353
}
5454
gix::diff::tree_with_rewrites::Change::Deletion {
5555
location,
@@ -59,7 +59,7 @@ fn write_changes(
5959
} => {
6060
writeln!(out, "D: {}", typed_location(location, entry_mode))?;
6161
writeln!(out, " {}", id.attach(repo).shorten_or_id())?;
62-
writeln!(out, " {:o} ->", entry_mode.0)?;
62+
writeln!(out, " {:o} ->", entry_mode)?;
6363
}
6464
gix::diff::tree_with_rewrites::Change::Modification {
6565
location,
@@ -76,7 +76,7 @@ fn write_changes(
7676
id = id.attach(repo).shorten_or_id()
7777
)?;
7878
if previous_entry_mode != entry_mode {
79-
writeln!(out, " {:o} -> {:o}", previous_entry_mode.0, entry_mode.0)?;
79+
writeln!(out, " {:o} -> {:o}", previous_entry_mode, entry_mode)?;
8080
}
8181
}
8282
gix::diff::tree_with_rewrites::Change::Rewrite {
@@ -101,7 +101,7 @@ fn write_changes(
101101
id = id.attach(repo).shorten_or_id()
102102
)?;
103103
if source_entry_mode != entry_mode {
104-
writeln!(out, " {:o} -> {:o}", source_entry_mode.0, entry_mode.0)?;
104+
writeln!(out, " {:o} -> {:o}", source_entry_mode, entry_mode)?;
105105
}
106106
}
107107
}

gix-index/src/entry/mode.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ impl Mode {
7171

7272
impl From<gix_object::tree::EntryMode> for Mode {
7373
fn from(value: gix_object::tree::EntryMode) -> Self {
74-
Self::from_bits_truncate(u32::from(value.0))
74+
let value: u16 = value.into();
75+
Self::from_bits_truncate(u32::from(value))
7576
}
7677
}
7778

gix-object/src/tree/mod.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{
44
};
55
use std::cell::RefCell;
66
use std::cmp::Ordering;
7+
use std::fmt;
78

89
///
910
pub mod editor;
@@ -44,14 +45,20 @@ pub struct Editor<'a> {
4445
/// create it by converting [`EntryKind`] into `EntryMode`.
4546
#[derive(Clone, Copy, PartialEq, Eq, Ord, PartialOrd, Hash)]
4647
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47-
pub struct EntryMode(pub u16);
48+
pub struct EntryMode(u16);
4849

4950
impl std::fmt::Debug for EntryMode {
5051
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
5152
write!(f, "EntryMode({:#o})", self.0)
5253
}
5354
}
5455

56+
impl std::fmt::Octal for EntryMode {
57+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58+
write!(f, "{:o}", self.0)
59+
}
60+
}
61+
5562
/// A discretized version of ideal and valid values for entry modes.
5663
///
5764
/// Note that even though it can represent every valid [mode](EntryMode), it might
@@ -72,6 +79,18 @@ pub enum EntryKind {
7279
Commit = 0o160000,
7380
}
7481

82+
impl From<u16> for EntryMode {
83+
fn from(value: u16) -> Self {
84+
EntryMode(value)
85+
}
86+
}
87+
88+
impl From<EntryMode> for u16 {
89+
fn from(value: EntryMode) -> Self {
90+
value.0
91+
}
92+
}
93+
7594
impl From<EntryKind> for EntryMode {
7695
fn from(value: EntryKind) -> Self {
7796
EntryMode(value as u16)
@@ -199,6 +218,11 @@ impl EntryMode {
199218
}
200219
.into()
201220
}
221+
222+
/// Display the octal representation of this EntryMode
223+
pub fn as_octal_representation(&self) -> fmt::Result {
224+
Ok(print!("{:o}", self.0))
225+
}
202226
}
203227

204228
impl TreeRef<'_> {

gix-object/tests/object/tree/entry_mode.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ fn is_methods() {
1616
}
1717

1818
assert!(mode(EntryKind::Blob).is_blob());
19-
assert!(EntryMode(0o100645).is_blob());
20-
assert_eq!(EntryMode(0o100645).kind(), EntryKind::Blob);
21-
assert!(!EntryMode(0o100675).is_executable());
22-
assert!(EntryMode(0o100700).is_executable());
23-
assert_eq!(EntryMode(0o100700).kind(), EntryKind::BlobExecutable);
19+
assert!(EntryMode::from(0o100645).is_blob());
20+
assert_eq!(EntryMode::from(0o100645).kind(), EntryKind::Blob);
21+
assert!(!EntryMode::from(0o100675).is_executable());
22+
assert!(EntryMode::from(0o100700).is_executable());
23+
assert_eq!(EntryMode::from(0o100700).kind(), EntryKind::BlobExecutable);
2424
assert!(!mode(EntryKind::Blob).is_link());
2525
assert!(mode(EntryKind::BlobExecutable).is_blob());
2626
assert!(mode(EntryKind::BlobExecutable).is_executable());
@@ -29,17 +29,17 @@ fn is_methods() {
2929

3030
assert!(!mode(EntryKind::Link).is_blob());
3131
assert!(mode(EntryKind::Link).is_link());
32-
assert!(EntryMode(0o121234).is_link());
33-
assert_eq!(EntryMode(0o121234).kind(), EntryKind::Link);
32+
assert!(EntryMode::from(0o121234).is_link());
33+
assert_eq!(EntryMode::from(0o121234).kind(), EntryKind::Link);
3434
assert!(mode(EntryKind::Link).is_blob_or_symlink());
3535
assert!(mode(EntryKind::Tree).is_tree());
36-
assert!(EntryMode(0o040101).is_tree());
37-
assert_eq!(EntryMode(0o040101).kind(), EntryKind::Tree);
36+
assert!(EntryMode::from(0o040101).is_tree());
37+
assert_eq!(EntryMode::from(0o040101).kind(), EntryKind::Tree);
3838
assert!(mode(EntryKind::Commit).is_commit());
39-
assert!(EntryMode(0o167124).is_commit());
40-
assert_eq!(EntryMode(0o167124).kind(), EntryKind::Commit);
39+
assert!(EntryMode::from(0o167124).is_commit());
40+
assert_eq!(EntryMode::from(0o167124).kind(), EntryKind::Commit);
4141
assert_eq!(
42-
EntryMode(0o000000).kind(),
42+
EntryMode::from(0o000000).kind(),
4343
EntryKind::Commit,
4444
"commit is really 'anything else' as `kind()` can't fail"
4545
);

0 commit comments

Comments
 (0)