Skip to content

Commit 3a5068e

Browse files
committed
feat: add tree::EntryRef::to_owned().
That way it's in a more reasonable spot as sibling to `Entry` and it's clearer how to convert noe into the other.
1 parent 3ef6b55 commit 3a5068e

File tree

2 files changed

+71
-64
lines changed

2 files changed

+71
-64
lines changed

gix/src/object/tree/iter.rs renamed to gix/src/object/tree/entry.rs

Lines changed: 50 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
1-
use super::Tree;
2-
use crate::Repository;
3-
4-
/// An entry within a tree
5-
pub struct EntryRef<'repo, 'a> {
6-
/// The actual entry ref we are wrapping.
7-
pub inner: gix_object::tree::EntryRef<'a>,
8-
/// The owning repository.
9-
pub repo: &'repo Repository,
1+
use crate::object::tree::EntryRef;
2+
use crate::{bstr::BStr, ext::ObjectIdExt, object::tree::Entry};
3+
4+
/// Access
5+
impl<'repo> Entry<'repo> {
6+
/// The kind of object to which `oid` is pointing to.
7+
pub fn mode(&self) -> gix_object::tree::EntryMode {
8+
self.inner.mode
9+
}
10+
11+
/// The name of the file in the parent tree.
12+
pub fn filename(&self) -> &BStr {
13+
self.inner.filename.as_ref()
14+
}
15+
16+
/// Return the object id of the entry.
17+
pub fn id(&self) -> crate::Id<'repo> {
18+
self.inner.oid.attach(self.repo)
19+
}
20+
21+
/// Return the object this entry points to.
22+
pub fn object(&self) -> Result<crate::Object<'repo>, crate::object::find::existing::Error> {
23+
self.id().object()
24+
}
25+
26+
/// Return the plain object id of this entry, without access to the repository.
27+
pub fn oid(&self) -> &gix_hash::oid {
28+
&self.inner.oid
29+
}
30+
31+
/// Return the plain object id of this entry, without access to the repository.
32+
pub fn object_id(&self) -> gix_hash::ObjectId {
33+
self.inner.oid
34+
}
35+
}
36+
37+
/// Consuming
38+
impl Entry<'_> {
39+
/// Return the contained object.
40+
pub fn detach(self) -> gix_object::tree::Entry {
41+
self.inner
42+
}
1043
}
1144

1245
impl<'repo, 'a> EntryRef<'repo, 'a> {
@@ -49,6 +82,14 @@ impl<'repo, 'a> EntryRef<'repo, 'a> {
4982
pub fn detach(&self) -> gix_object::tree::EntryRef<'a> {
5083
self.inner
5184
}
85+
86+
/// Create an instance that doesn't bind to a buffer anymore (but that still contains a repository reference).
87+
pub fn to_owned(&self) -> Entry<'repo> {
88+
Entry {
89+
inner: self.inner.into(),
90+
repo: self.repo,
91+
}
92+
}
5293
}
5394

5495
impl std::fmt::Display for EntryRef<'_, '_> {
@@ -63,11 +104,3 @@ impl std::fmt::Display for EntryRef<'_, '_> {
63104
)
64105
}
65106
}
66-
67-
impl<'repo> Tree<'repo> {
68-
/// Return an iterator over tree entries to obtain information about files and directories this tree contains.
69-
pub fn iter(&self) -> impl Iterator<Item = Result<EntryRef<'repo, '_>, gix_object::decode::Error>> {
70-
let repo = self.repo;
71-
gix_object::TreeRefIter::from_bytes(&self.data).map(move |e| e.map(|entry| EntryRef { inner: entry, repo }))
72-
}
73-
}

gix/src/object/tree/mod.rs

Lines changed: 21 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use gix_hash::ObjectId;
22
pub use gix_object::tree::{EntryKind, EntryMode};
33
use gix_object::{bstr::BStr, FindExt, TreeRefIter};
44

5-
use crate::{object::find, Id, ObjectDetached, Tree};
5+
use crate::{object::find, Id, ObjectDetached, Repository, Tree};
66

77
/// All state needed to conveniently edit a tree, using only [update-or-insert](Editor::upsert()) and [removals](Editor::remove()).
88
#[cfg(feature = "tree-editor")]
@@ -185,15 +185,32 @@ pub mod diff;
185185
pub mod traverse;
186186

187187
///
188-
mod iter;
189-
pub use iter::EntryRef;
188+
mod iter {
189+
use super::{EntryRef, Tree};
190+
191+
impl<'repo> Tree<'repo> {
192+
/// Return an iterator over tree entries to obtain information about files and directories this tree contains.
193+
pub fn iter(&self) -> impl Iterator<Item = Result<EntryRef<'repo, '_>, gix_object::decode::Error>> {
194+
let repo = self.repo;
195+
gix_object::TreeRefIter::from_bytes(&self.data).map(move |e| e.map(|entry| EntryRef { inner: entry, repo }))
196+
}
197+
}
198+
}
190199

191200
impl std::fmt::Debug for Tree<'_> {
192201
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
193202
write!(f, "Tree({})", self.id)
194203
}
195204
}
196205

206+
/// An entry within a tree
207+
pub struct EntryRef<'repo, 'a> {
208+
/// The actual entry ref we are wrapping.
209+
pub inner: gix_object::tree::EntryRef<'a>,
210+
/// The owning repository.
211+
pub repo: &'repo Repository,
212+
}
213+
197214
/// An entry in a [`Tree`], similar to an entry in a directory.
198215
#[derive(PartialEq, Debug, Clone)]
199216
pub struct Entry<'repo> {
@@ -202,50 +219,7 @@ pub struct Entry<'repo> {
202219
pub repo: &'repo crate::Repository,
203220
}
204221

205-
mod entry {
206-
use crate::{bstr::BStr, ext::ObjectIdExt, object::tree::Entry};
207-
208-
/// Access
209-
impl<'repo> Entry<'repo> {
210-
/// The kind of object to which `oid` is pointing to.
211-
pub fn mode(&self) -> gix_object::tree::EntryMode {
212-
self.inner.mode
213-
}
214-
215-
/// The name of the file in the parent tree.
216-
pub fn filename(&self) -> &BStr {
217-
self.inner.filename.as_ref()
218-
}
219-
220-
/// Return the object id of the entry.
221-
pub fn id(&self) -> crate::Id<'repo> {
222-
self.inner.oid.attach(self.repo)
223-
}
224-
225-
/// Return the object this entry points to.
226-
pub fn object(&self) -> Result<crate::Object<'repo>, crate::object::find::existing::Error> {
227-
self.id().object()
228-
}
229-
230-
/// Return the plain object id of this entry, without access to the repository.
231-
pub fn oid(&self) -> &gix_hash::oid {
232-
&self.inner.oid
233-
}
234-
235-
/// Return the plain object id of this entry, without access to the repository.
236-
pub fn object_id(&self) -> gix_hash::ObjectId {
237-
self.inner.oid
238-
}
239-
}
240-
241-
/// Consuming
242-
impl Entry<'_> {
243-
/// Return the contained object.
244-
pub fn detach(self) -> gix_object::tree::Entry {
245-
self.inner
246-
}
247-
}
248-
}
222+
mod entry;
249223

250224
mod _impls {
251225
use crate::Tree;

0 commit comments

Comments
 (0)