From 88e6f0c94cb3ee584113ca15ce2362a19900d7f1 Mon Sep 17 00:00:00 2001 From: Han Xu Date: Tue, 17 Jan 2023 10:40:14 -0800 Subject: [PATCH] move up blob.rs and tree.rs to src/ Signed-off-by: Han Xu --- radicle-surf/src/{object => }/blob.rs | 0 radicle-surf/src/lib.rs | 3 +- radicle-surf/src/object.rs | 25 ---------- radicle-surf/src/repo.rs | 7 +-- radicle-surf/src/{object => }/tree.rs | 67 +++++++++++++-------------- 5 files changed, 38 insertions(+), 64 deletions(-) rename radicle-surf/src/{object => }/blob.rs (100%) delete mode 100644 radicle-surf/src/object.rs rename radicle-surf/src/{object => }/tree.rs (78%) diff --git a/radicle-surf/src/object/blob.rs b/radicle-surf/src/blob.rs similarity index 100% rename from radicle-surf/src/object/blob.rs rename to radicle-surf/src/blob.rs diff --git a/radicle-surf/src/lib.rs b/radicle-surf/src/lib.rs index 7829974..9102c18 100644 --- a/radicle-surf/src/lib.rs +++ b/radicle-surf/src/lib.rs @@ -28,9 +28,10 @@ pub extern crate git_ref_format; extern crate radicle_git_ext as git_ext; +pub mod blob; pub mod diff; pub mod fs; -pub mod object; +pub mod tree; // Re-export git2 as sub-module pub use git2::{self, Error as Git2Error, Time}; diff --git a/radicle-surf/src/object.rs b/radicle-surf/src/object.rs deleted file mode 100644 index 426f09c..0000000 --- a/radicle-surf/src/object.rs +++ /dev/null @@ -1,25 +0,0 @@ -// This file is part of radicle-surf -// -// -// Copyright (C) 2019-2020 The Radicle Team -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License version 3 or -// later as published by the Free Software Foundation. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -//! Common definitions for git objects (blob and tree). -//! See git [doc](https://git-scm.com/book/en/v2/Git-Internals-Git-Objects) for more details. - -pub mod blob; -pub use blob::{Blob, BlobContent}; - -pub mod tree; -pub use tree::{Tree, TreeEntry}; diff --git a/radicle-surf/src/repo.rs b/radicle-surf/src/repo.rs index 0315f39..b887307 100644 --- a/radicle-surf/src/repo.rs +++ b/radicle-surf/src/repo.rs @@ -27,13 +27,14 @@ use radicle_git_ext::Oid; use thiserror::Error; use crate::{ + blob::Blob, commit, diff::{self, *}, fs::{self, Directory, File, FileContent}, glob, namespace, - object::{Blob, Tree, TreeEntry}, refs::{self, BranchNames, Branches, Categories, Namespaces, TagNames, Tags}, + tree::{Entry, Tree}, Branch, Commit, Glob, @@ -283,9 +284,9 @@ impl Repository { let commit = self .last_commit(&path, commit.id)? .ok_or(Error::PathNotFound(path))?; - Ok(TreeEntry::new(name, en.into(), commit)) + Ok(Entry::new(name, en.into(), commit)) }) - .collect::, Error>>()?; + .collect::, Error>>()?; entries.sort(); let last_commit = self diff --git a/radicle-surf/src/object/tree.rs b/radicle-surf/src/tree.rs similarity index 78% rename from radicle-surf/src/object/tree.rs rename to radicle-surf/src/tree.rs index fe46729..4f4105b 100644 --- a/radicle-surf/src/object/tree.rs +++ b/radicle-surf/src/tree.rs @@ -37,14 +37,14 @@ pub struct Tree { /// The object id of this tree. id: Oid, /// The first descendant entries for this tree. - entries: Vec, + entries: Vec, /// The commit object that created this tree object. commit: Commit, } impl Tree { /// Creates a new tree, ensuring the `entries` are sorted. - pub(crate) fn new(id: Oid, mut entries: Vec, commit: Commit) -> Self { + pub(crate) fn new(id: Oid, mut entries: Vec, commit: Commit) -> Self { entries.sort(); Self { id, @@ -63,7 +63,7 @@ impl Tree { } /// Returns the entries of the tree. - pub fn entries(&self) -> &Vec { + pub fn entries(&self) -> &Vec { &self.entries } } @@ -71,7 +71,7 @@ impl Tree { #[cfg(feature = "serde")] impl Serialize for Tree { /// Sample output: - /// (for `` and `` sample output, see [`TreeEntry`]) + /// (for `` and `` sample output, see [`Entry`]) /// ``` /// { /// "entries": [ @@ -109,50 +109,47 @@ impl Serialize for Tree { } #[derive(Debug, Clone, Copy, PartialEq, Eq)] -pub enum Entry { +pub enum EntryKind { Tree(Oid), Blob(Oid), } -impl PartialOrd for Entry { +impl PartialOrd for EntryKind { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl Ord for Entry { +impl Ord for EntryKind { fn cmp(&self, other: &Self) -> Ordering { match (self, other) { - (Entry::Tree(_), Entry::Tree(_)) => Ordering::Equal, - (Entry::Tree(_), Entry::Blob(_)) => Ordering::Less, - (Entry::Blob(_), Entry::Tree(_)) => Ordering::Greater, - (Entry::Blob(_), Entry::Blob(_)) => Ordering::Equal, + (EntryKind::Tree(_), EntryKind::Tree(_)) => Ordering::Equal, + (EntryKind::Tree(_), EntryKind::Blob(_)) => Ordering::Less, + (EntryKind::Blob(_), EntryKind::Tree(_)) => Ordering::Greater, + (EntryKind::Blob(_), EntryKind::Blob(_)) => Ordering::Equal, } } } /// An entry that can be found in a tree. /// -/// Each entry consists of a [`TreeEntry::name`], its kind -- given as [`Entry`] -/// -- and the header of its associated [`TreeEntry::commit`]. -/// /// # Ordering /// -/// The ordering of a `TreeEntry` is first by its [`Entry`] kind where -/// [`Entry::Tree`]s come before [`Entry::Blob`]. If both kinds are -/// equal then they are next compared by the lexicographical ordering +/// The ordering of a [`Entry`] is first by its `entry` where +/// [`EntryKind::Tree`]s come before [`EntryKind::Blob`]. If both kinds +/// are equal then they are next compared by the lexicographical ordering /// of their `name`s. #[derive(Clone, Debug)] -pub struct TreeEntry { +pub struct Entry { name: String, - entry: Entry, + entry: EntryKind, /// The commit object that created this entry object. commit: Commit, } -impl TreeEntry { - pub(crate) fn new(name: String, entry: Entry, commit: Commit) -> Self { +impl Entry { + pub(crate) fn new(name: String, entry: EntryKind, commit: Commit) -> Self { Self { name, entry, @@ -164,12 +161,12 @@ impl TreeEntry { &self.name } - pub fn entry(&self) -> &Entry { + pub fn entry(&self) -> &EntryKind { &self.entry } pub fn is_tree(&self) -> bool { - matches!(self.entry, Entry::Tree(_)) + matches!(self.entry, EntryKind::Tree(_)) } pub fn commit(&self) -> &Commit { @@ -178,14 +175,14 @@ impl TreeEntry { pub fn object_id(&self) -> Oid { match self.entry { - Entry::Blob(id) => id, - Entry::Tree(id) => id, + EntryKind::Blob(id) => id, + EntryKind::Tree(id) => id, } } } // To support `sort`. -impl Ord for TreeEntry { +impl Ord for Entry { fn cmp(&self, other: &Self) -> Ordering { self.entry .cmp(&other.entry) @@ -193,31 +190,31 @@ impl Ord for TreeEntry { } } -impl PartialOrd for TreeEntry { +impl PartialOrd for Entry { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) } } -impl PartialEq for TreeEntry { +impl PartialEq for Entry { fn eq(&self, other: &Self) -> bool { self.entry == other.entry && self.name == other.name } } -impl Eq for TreeEntry {} +impl Eq for Entry {} -impl From for Entry { +impl From for EntryKind { fn from(entry: fs::Entry) -> Self { match entry { - fs::Entry::File(f) => Entry::Blob(f.id()), - fs::Entry::Directory(d) => Entry::Tree(d.id()), + fs::Entry::File(f) => EntryKind::Blob(f.id()), + fs::Entry::Directory(d) => EntryKind::Tree(d.id()), } } } #[cfg(feature = "serde")] -impl Serialize for TreeEntry { +impl Serialize for Entry { /// Sample output: /// ```json /// { @@ -250,8 +247,8 @@ impl Serialize for TreeEntry { state.serialize_field( "kind", match self.entry { - Entry::Blob(_) => "blob", - Entry::Tree(_) => "tree", + EntryKind::Blob(_) => "blob", + EntryKind::Tree(_) => "tree", }, )?; state.serialize_field("oid", &self.object_id())?;