From 4e373fb8de5bfa136d9095e0c618cc22b02bf4ec Mon Sep 17 00:00:00 2001 From: Han Xu Date: Wed, 21 Dec 2022 13:19:44 -0800 Subject: [PATCH] remove source/ layer and file_system/ layer. - Merge `person.rs` into `object/commit.rs`. - Move `source/commit.rs` into `object/`. - Merge `file_system/directory.rs` into `file_system.rs`. - Rename `file_system.rs` to `fs.rs`. Signed-off-by: Han Xu --- radicle-surf/examples/browsing.rs | 6 +- radicle-surf/src/file_system.rs | 114 ------------------ .../src/{file_system/directory.rs => fs.rs} | 0 radicle-surf/src/git/repo.rs | 15 +-- radicle-surf/src/lib.rs | 4 +- radicle-surf/src/{source => }/object.rs | 8 +- radicle-surf/src/{source => }/object/blob.rs | 2 +- radicle-surf/src/{source => object}/commit.rs | 12 +- radicle-surf/src/{source => }/object/tree.rs | 10 +- radicle-surf/src/source.rs | 20 --- radicle-surf/src/source/person.rs | 29 ----- radicle-surf/t/src/file_system.rs | 16 +-- radicle-surf/t/src/git/code_browsing.rs | 10 +- 13 files changed, 46 insertions(+), 200 deletions(-) delete mode 100644 radicle-surf/src/file_system.rs rename radicle-surf/src/{file_system/directory.rs => fs.rs} (100%) rename radicle-surf/src/{source => }/object.rs (91%) rename radicle-surf/src/{source => }/object/blob.rs (99%) rename radicle-surf/src/{source => object}/commit.rs (94%) rename radicle-surf/src/{source => }/object/tree.rs (95%) delete mode 100644 radicle-surf/src/source.rs delete mode 100644 radicle-surf/src/source/person.rs diff --git a/radicle-surf/examples/browsing.rs b/radicle-surf/examples/browsing.rs index f1b1052..5461c2a 100644 --- a/radicle-surf/examples/browsing.rs +++ b/radicle-surf/examples/browsing.rs @@ -25,7 +25,7 @@ //! the directories in a tree-like structure. use radicle_surf::{ - file_system::{directory, Directory}, + fs::{self, Directory}, git::Repository, }; use std::{env, time::Instant}; @@ -53,8 +53,8 @@ fn print_directory(d: &Directory, repo: &Repository, indent_level: usize) { println!("{}{}/", &indent, d.name()); for entry in d.entries(repo).unwrap() { match entry { - directory::Entry::File(f) => println!(" {}{}", &indent, f.name()), - directory::Entry::Directory(d) => print_directory(&d, repo, indent_level + 1), + fs::Entry::File(f) => println!(" {}{}", &indent, f.name()), + fs::Entry::Directory(d) => print_directory(&d, repo, indent_level + 1), } } } diff --git a/radicle-surf/src/file_system.rs b/radicle-surf/src/file_system.rs deleted file mode 100644 index b1c30ab..0000000 --- a/radicle-surf/src/file_system.rs +++ /dev/null @@ -1,114 +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 . - -//! A model of a non-empty directory data structure that can be searched, -//! queried, and rendered. The concept is to represent VCS directory, but is not -//! necessarily tied to one. -//! -//! # Examples -//! -//! ``` -//! use nonempty::NonEmpty; -//! use radicle_surf::file_system as fs; -//! -//! // This used for unsafe set up of the directory, but should not be used in production code. -//! use radicle_surf::file_system::unsound; -//! -//! let mut directory = fs::Directory::root(); -//! -//! // Set up root files -//! let readme = fs::File::new(b"Radicle Surfing"); -//! let cargo = fs::File::new(b"[package]\nname = \"radicle-surf\""); -//! let root_files = NonEmpty::from(( -//! (unsound::label::new("README.md"), readme), -//! vec![(unsound::label::new("Cargo.toml"), cargo)], -//! )); -//! -//! // Set up src files -//! let lib = fs::File::new(b"pub mod diff;\npub mod file_system;\n pub mod vcs;"); -//! let file_system_mod = fs::File::new(b"pub mod directory;\npub mod error;\nmod path;"); -//! -//! directory.insert_files(&[], root_files); -//! directory.insert_file(unsound::path::new("src/lib.rs"), lib.clone()); -//! directory.insert_file(unsound::path::new("src/file_system/mod.rs"), file_system_mod); -//! -//! // With a directory in place we can begin to operate on it -//! // The first we will do is list what contents are at the root. -//! let root_contents = directory.list_directory(); -//! -//! // Checking that we have the correct contents -//! assert_eq!( -//! root_contents, -//! vec![ -//! fs::SystemType::file(unsound::label::new("Cargo.toml")), -//! fs::SystemType::file(unsound::label::new("README.md")), -//! fs::SystemType::directory(unsound::label::new("src")), -//! ] -//! ); -//! -//! // We can then go down one level to explore sub-directories -//! // Note here that we can use `Path::new`, since there's guranteed to be a `Label`, -//! // although we cheated and created the label unsafely. -//! let src = directory.find_directory(fs::Path::new(unsound::label::new("src"))); -//! -//! // Ensure that we found the src directory -//! assert!(src.is_some()); -//! let src = src.unwrap(); -//! -//! let src_contents = src.list_directory(); -//! -//! // Checking we have the correct contents of 'src' -//! assert_eq!( -//! src_contents, -//! vec![ -//! fs::SystemType::directory(unsound::label::new("file_system")), -//! fs::SystemType::file(unsound::label::new("lib.rs")), -//! ] -//! ); -//! -//! // We can dive down to 'file_system' either from the root or src, they should be the same. -//! assert_eq!( -//! src.find_directory(unsound::path::new("file_system")), -//! directory.find_directory(unsound::path::new("src/file_system")), -//! ); -//! -//! // We can also find files -//! assert_eq!( -//! src.find_file(unsound::path::new("lib.rs")), -//! Some(lib) -//! ); -//! -//! // From anywhere -//! assert_eq!( -//! directory.find_file(unsound::path::new("src/file_system/mod.rs")), -//! src.find_file(unsound::path::new("file_system/mod.rs")), -//! ); -//! -//! // And we can also check the size of directories and files -//! assert_eq!( -//! directory.find_file(unsound::path::new("src/file_system/mod.rs")).map(|f| f.size()), -//! Some(43), -//! ); -//! -//! assert_eq!( -//! directory.size(), -//! 137, -//! ); -//! ``` - -pub mod directory; -pub use directory::{Directory, Entries, Entry, File, FileContent}; diff --git a/radicle-surf/src/file_system/directory.rs b/radicle-surf/src/fs.rs similarity index 100% rename from radicle-surf/src/file_system/directory.rs rename to radicle-surf/src/fs.rs diff --git a/radicle-surf/src/git/repo.rs b/radicle-surf/src/git/repo.rs index 4396d19..730d1fd 100644 --- a/radicle-surf/src/git/repo.rs +++ b/radicle-surf/src/git/repo.rs @@ -28,10 +28,7 @@ use thiserror::Error; use crate::{ diff::{self, *}, - file_system::{ - directory::{self, File, FileContent}, - Directory, - }, + fs::{self, Directory, File, FileContent}, git::{ commit, glob, @@ -47,7 +44,7 @@ use crate::{ Tag, ToCommit, }, - source::{self, commit::Header, Blob, Tree, TreeEntry}, + object::{commit::Header, Blob, Tree, TreeEntry}, }; pub mod iter; @@ -70,9 +67,9 @@ pub enum Error { Diff(#[from] diff::git::error::Diff), /// A wrapper around the generic [`git2::Error`]. #[error(transparent)] - Directory(#[from] directory::error::Directory), + Directory(#[from] fs::error::Directory), #[error(transparent)] - File(#[from] directory::error::File), + File(#[from] fs::error::File), #[error(transparent)] Git(#[from] git2::Error), #[error(transparent)] @@ -301,7 +298,7 @@ impl Repository { let last_commit = self .last_commit(path, commit)? .ok_or_else(|| Error::PathNotFound(path.as_ref().to_path_buf()))?; - let header = source::commit::Header::from(last_commit); + let header = Header::from(last_commit); Ok(Tree::new(dir.id(), entries, header)) } @@ -314,7 +311,7 @@ impl Repository { let last_commit = self .last_commit(path, commit)? .ok_or_else(|| Error::PathNotFound(path.as_ref().to_path_buf()))?; - let header = source::commit::Header::from(last_commit); + let header = Header::from(last_commit); let content = file.content(self)?; Ok(Blob::new(file.id(), content.as_bytes(), header)) diff --git a/radicle-surf/src/lib.rs b/radicle-surf/src/lib.rs index a400e96..d553770 100644 --- a/radicle-surf/src/lib.rs +++ b/radicle-surf/src/lib.rs @@ -29,6 +29,6 @@ pub extern crate git_ref_format; extern crate radicle_git_ext as git_ext; pub mod diff; -pub mod file_system; +pub mod fs; pub mod git; -pub mod source; +pub mod object; diff --git a/radicle-surf/src/source/object.rs b/radicle-surf/src/object.rs similarity index 91% rename from radicle-surf/src/source/object.rs rename to radicle-surf/src/object.rs index 6c943b5..4ecb18e 100644 --- a/radicle-surf/src/source/object.rs +++ b/radicle-surf/src/object.rs @@ -26,16 +26,18 @@ pub use blob::{Blob, BlobContent}; pub mod tree; pub use tree::{Tree, TreeEntry}; -use crate::{file_system::directory, git}; +pub mod commit; + +use crate::{fs, git}; /// An error reported by object types. #[derive(Debug, thiserror::Error)] pub enum Error { #[error(transparent)] - Directory(#[from] directory::error::Directory), + Directory(#[from] fs::error::Directory), #[error(transparent)] - File(#[from] directory::error::File), + File(#[from] fs::error::File), /// An error occurred during a git operation. #[error(transparent)] diff --git a/radicle-surf/src/source/object/blob.rs b/radicle-surf/src/object/blob.rs similarity index 99% rename from radicle-surf/src/source/object/blob.rs rename to radicle-surf/src/object/blob.rs index c245112..94d6095 100644 --- a/radicle-surf/src/source/object/blob.rs +++ b/radicle-surf/src/object/blob.rs @@ -27,7 +27,7 @@ use serde::{ Serialize, }; -use crate::source::commit; +use crate::object::commit; /// Represents a git blob object. pub struct Blob { diff --git a/radicle-surf/src/source/commit.rs b/radicle-surf/src/object/commit.rs similarity index 94% rename from radicle-surf/src/source/commit.rs rename to radicle-surf/src/object/commit.rs index 91ec149..6082d70 100644 --- a/radicle-surf/src/source/commit.rs +++ b/radicle-surf/src/object/commit.rs @@ -29,7 +29,6 @@ use serde::{ use crate::{ diff, git::{self, glob, Glob, Repository}, - source::person::Person, }; use radicle_git_ext::Oid; @@ -194,3 +193,14 @@ pub enum Error { #[error("the path '{0}' was not found")] PathNotFound(PathBuf), } + +/// Representation of a person (e.g. committer, author, signer) from a +/// repository. Usually extracted from a signature. +#[cfg_attr(feature = "serde", derive(serde::Serialize))] +#[derive(Clone, Debug)] +pub struct Person { + /// Name part of the commit signature. + pub name: String, + /// Email part of the commit signature. + pub email: String, +} diff --git a/radicle-surf/src/source/object/tree.rs b/radicle-surf/src/object/tree.rs similarity index 95% rename from radicle-surf/src/source/object/tree.rs rename to radicle-surf/src/object/tree.rs index 4463946..ee142a0 100644 --- a/radicle-surf/src/source/object/tree.rs +++ b/radicle-surf/src/object/tree.rs @@ -27,7 +27,7 @@ use serde::{ Serialize, }; -use crate::{file_system::directory, source::commit}; +use crate::{fs, object::commit}; /// Represents a tree object as in git. It is essentially the content of /// one directory. Note that multiple directories can have the same content, @@ -176,11 +176,11 @@ impl PartialEq for TreeEntry { impl Eq for TreeEntry {} -impl From for Entry { - fn from(entry: directory::Entry) -> Self { +impl From for Entry { + fn from(entry: fs::Entry) -> Self { match entry { - directory::Entry::File(f) => Entry::Blob(f.id()), - directory::Entry::Directory(d) => Entry::Tree(d.id()), + fs::Entry::File(f) => Entry::Blob(f.id()), + fs::Entry::Directory(d) => Entry::Tree(d.id()), } } } diff --git a/radicle-surf/src/source.rs b/radicle-surf/src/source.rs deleted file mode 100644 index 9e16e85..0000000 --- a/radicle-surf/src/source.rs +++ /dev/null @@ -1,20 +0,0 @@ -//! The `source` module provides a layer on top of the [`crate::git`] -//! functionality. -//! -//! It provides data types of [`Blob`]s, [`Tree`]s, and [`Commit`]s -//! (see [git objects][git-objects]). These types are analgous to -//! [`crate::file_system::File`], [`crate::file_system::Directory`], and -//! [`crate::git::Commit`]. However, they provide extra metadata and -//! can be serialized to serve to other applications. For example, -//! they could be used in an HTTP server for viewing a Git repository. -//! -//! [git-objects]: https://git-scm.com/book/en/v2/Git-Internals-Git-Objects - -pub mod object; -pub use object::{blob, tree, Blob, BlobContent, Tree, TreeEntry}; - -pub mod commit; -pub use commit::{commit, commits, Commit}; - -mod person; -pub use person::Person; diff --git a/radicle-surf/src/source/person.rs b/radicle-surf/src/source/person.rs deleted file mode 100644 index ad7ceec..0000000 --- a/radicle-surf/src/source/person.rs +++ /dev/null @@ -1,29 +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 . - -//! Represents a person in a repo. - -/// Representation of a person (e.g. committer, author, signer) from a -/// repository. Usually extracted from a signature. -#[cfg_attr(feature = "serde", derive(serde::Serialize))] -#[derive(Clone, Debug)] -pub struct Person { - /// Name part of the commit signature. - pub name: String, - /// Email part of the commit signature. - pub email: String, -} diff --git a/radicle-surf/t/src/file_system.rs b/radicle-surf/t/src/file_system.rs index 6ce557b..a0a8438 100644 --- a/radicle-surf/t/src/file_system.rs +++ b/radicle-surf/t/src/file_system.rs @@ -6,7 +6,7 @@ mod directory { use git_ref_format::refname; use radicle_surf::{ - file_system::{directory, Entry}, + fs::{self, Entry}, git::{Branch, Repository}, }; use std::path::Path; @@ -21,21 +21,21 @@ mod directory { // find_entry for a file. let path = Path::new("src/memory.rs"); let entry = root.find_entry(&path, &repo).unwrap(); - assert!(matches!(entry, Some(directory::Entry::File(_)))); + assert!(matches!(entry, Some(fs::Entry::File(_)))); // find_entry for a directory. let path = Path::new("this/is/a/really/deeply/nested/directory/tree"); let entry = root.find_entry(&path, &repo).unwrap(); - assert!(matches!(entry, Some(directory::Entry::Directory(_)))); + assert!(matches!(entry, Some(fs::Entry::Directory(_)))); // find_entry for a non-leaf directory and its relative path. let path = Path::new("text"); let entry = root.find_entry(&path, &repo).unwrap(); - assert!(matches!(entry, Some(directory::Entry::Directory(_)))); - if let Some(directory::Entry::Directory(sub_dir)) = entry { + assert!(matches!(entry, Some(fs::Entry::Directory(_)))); + if let Some(fs::Entry::Directory(sub_dir)) = entry { let inner_path = Path::new("garden.txt"); let inner_entry = sub_dir.find_entry(&inner_path, &repo).unwrap(); - assert!(matches!(inner_entry, Some(directory::Entry::File(_)))); + assert!(matches!(inner_entry, Some(fs::Entry::File(_)))); } // find_entry for non-existing file @@ -108,8 +108,8 @@ mod directory { let path = Path::new("src"); let entry = root.find_entry(&path, &repo).unwrap(); - assert!(matches!(entry, Some(directory::Entry::Directory(_)))); - if let Some(directory::Entry::Directory(d)) = entry { + assert!(matches!(entry, Some(fs::Entry::Directory(_)))); + if let Some(fs::Entry::Directory(d)) = entry { assert_eq!(16297, d.size(&repo).unwrap()); } } diff --git a/radicle-surf/t/src/git/code_browsing.rs b/radicle-surf/t/src/git/code_browsing.rs index 181febc..3f06f64 100644 --- a/radicle-surf/t/src/git/code_browsing.rs +++ b/radicle-surf/t/src/git/code_browsing.rs @@ -2,7 +2,7 @@ use std::path::Path; use git_ref_format::refname; use radicle_surf::{ - file_system::{directory, Directory}, + fs::{self, Directory}, git::{Branch, Repository}, }; @@ -22,14 +22,14 @@ fn iterate_root_dir_recursive() { /// recursively. /// Returns the number of items visited (i.e. printed) fn println_dir(dir: &Directory, repo: &Repository) -> i32 { - dir.traverse::( + dir.traverse::( repo, (0, 0), &mut |(count, indent_level), entry| { println!("> {}{}", " ".repeat(indent_level * 4), entry.name()); match entry { - directory::Entry::File(_) => Ok((count + 1, indent_level)), - directory::Entry::Directory(_) => Ok((count + 1, indent_level + 1)), + fs::Entry::File(_) => Ok((count + 1, indent_level)), + fs::Entry::Directory(_) => Ok((count + 1, indent_level + 1)), } }, ) @@ -49,7 +49,7 @@ fn browse_repo_lazily() { assert_eq!(count, 36); fn traverse(dir: &Directory, repo: &Repository) -> i32 { - dir.traverse::(repo, 0, &mut |count, _| Ok(count + 1)) + dir.traverse::(repo, 0, &mut |count, _| Ok(count + 1)) .unwrap() } }