Skip to content

Commit

Permalink
update and fix formatting (#45)
Browse files Browse the repository at this point in the history
* tidy up

* update readme
  • Loading branch information
danieleades authored Mar 23, 2021
1 parent f4731e3 commit 6f05c83
Show file tree
Hide file tree
Showing 10 changed files with 89 additions and 73 deletions.
10 changes: 8 additions & 2 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
version = "Two"
use_field_init_shorthand = true
merge_imports = true
imports_granularity="Crate"
wrap_comments = true
use_try_shorthand = true
use_try_shorthand = true
format_code_in_doc_comments = true
format_strings = true
normalize_comments = true
normalize_doc_attributes = true
remove_nested_parens = true
reorder_impl_items = true
8 changes: 3 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,13 @@ registry.

## Basic Usage
```rust
use crate_index::{Index, Url, Record, Version};
use crate_index::{Index, Record, Url, Version};

// Create a new index, backed by the filesystem and a git repository
let root = "/index";
let download = "https://my-crates-server.com/api/v1/crates/{crate}/{version}/download";

let mut index = Index::initialise(root, download)
.build()
.await?;
let mut index = Index::initialise(root, download).build().await?;

// Create a new crate 'Record' object
let name = "foo";
Expand All @@ -36,7 +34,7 @@ index.insert(record).await?;

## Requirements

- Minimum compiler version: **1.39.0**
- Minimum compiler version: **1.46.0**

## License

Expand Down
4 changes: 2 additions & 2 deletions src/blocking.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! This module contains blocking equivalents of the types defined in the main
//! [`index`](crate::index) module.
//! This module contains blocking equivalents of the core types defined in this
//! crate.
mod index;
pub use index::tree;
35 changes: 18 additions & 17 deletions src/blocking/index/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ where
}

/// An interface to a crate index directory on the filesystem
#[derive(Debug)]
pub struct Tree {
async_tree: AsyncTree,
}

/// Builder for creating a new [`Tree`]
#[derive(Debug)]
#[must_use]
pub struct Builder {
async_builder: AsyncBuilder,
Expand Down Expand Up @@ -104,7 +106,6 @@ impl Tree {
/// let root = "/index";
/// let download = "https://my-crates-server.com/api/v1/crates/{crate}/{version}/download";
///
///
/// let index_tree = Tree::initialise(root, download)
/// .api(Url::parse("https://my-crates-server.com/").unwrap())
/// .allowed_registry(Url::parse("https://my-intranet:8080/index").unwrap())
Expand Down Expand Up @@ -157,14 +158,14 @@ impl Tree {
/// # .build()
/// # .expect("couldn't create tree");
/// #
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.yank(crate_name, &version)? {
/// Ok(()) => println!("crate yanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.yank(crate_name, &version)? {
/// Ok(()) => println!("crate yanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// #
/// # Ok(())
/// # }
Expand Down Expand Up @@ -201,14 +202,14 @@ impl Tree {
/// # .build()
/// # .expect("couldn't create tree");
/// #
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.unyank(crate_name, &version)? {
/// Ok(()) => println!("crate unyanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.unyank(crate_name, &version)? {
/// Ok(()) => println!("crate unyanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// #
/// # Ok(())
/// # }
Expand Down
36 changes: 18 additions & 18 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@ use git::{Identity, Repository};
/// It functions exactly the same way as a [`Tree`], except that all changes to
/// the crates index are also committed to the git repository, which allows this
/// to be synced to a remote.
#[derive(Debug)]
pub struct Index {
tree: Tree,
repo: Repository,
}

/// A builder for initialising a new [`Index`]
#[derive(Debug)]
#[must_use]
pub struct Builder<'a> {
tree_builder: TreeBuilder,
Expand All @@ -40,7 +42,6 @@ pub struct Builder<'a> {

impl<'a> Builder<'a> {
// Set the Url for the registry API.
///
/// The API should implement the REST interface as defined in
/// [the Cargo book](https://doc.rust-lang.org/cargo/reference/registries.html)
pub fn api(mut self, api: Url) -> Self {
Expand Down Expand Up @@ -137,7 +138,6 @@ impl Index {
/// let download = "https://my-crates-server.com/api/v1/crates/{crate}/{version}/download";
/// let origin = Url::parse("https://github.com/crates/index.git").unwrap();
///
///
/// let index = Index::initialise(root, download)
/// .api(Url::parse("https://my-crates-server.com/").unwrap())
/// .allowed_registry(Url::parse("https://my-intranet:8080/index").unwrap())
Expand Down Expand Up @@ -229,14 +229,14 @@ impl Index {
/// # .await
/// # .unwrap();
/// #
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match index.yank(crate_name, &version).await? {
/// Ok(()) => println!("crate yanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match index.yank(crate_name, &version).await? {
/// Ok(()) => println!("crate yanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// #
/// # Ok(())
/// # }
Expand Down Expand Up @@ -286,14 +286,14 @@ impl Index {
/// # .await
/// # .unwrap();
/// #
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match index.unyank(crate_name, &version).await? {
/// Ok(()) => println!("crate unyanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match index.unyank(crate_name, &version).await? {
/// Ok(()) => println!("crate unyanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// #
/// # Ok(())
/// # }
Expand Down
13 changes: 11 additions & 2 deletions src/index/git.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,23 @@

//! Abstractions over a git repository containing an index.
use std::path::Path;
use std::{fmt, path::Path};
use url::Url;

/// Representation of a git repository on the host filesystem
pub struct Repository {
repo: git2::Repository,
}

impl fmt::Debug for Repository {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Repository")
.field("repo", &self.repo.workdir())
.finish()
}
}

#[derive(Debug)]
pub(crate) struct Identity<'a> {
pub username: &'a str,
pub email: &'a str,
Expand Down Expand Up @@ -77,7 +86,7 @@ impl Repository {
let mut index = self.repo.index()?;
let oid = index.write_tree()?;
let signature = self.repo.signature()?;
//let parent_commit = self.find_last_commit()?;
// let parent_commit = self.find_last_commit()?;
let parent_commit = self.repo.head()?.peel_to_commit()?;
let tree = self.repo.find_tree(oid)?;
self.repo.commit(
Expand Down
39 changes: 21 additions & 18 deletions src/index/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ mod config;
use config::Config;

/// An interface to a crate index directory on the filesystem
#[derive(Debug)]
pub struct Tree {
root: PathBuf,
config: Config,
crates: HashSet<String>,
}

/// Builder for creating a new [`Tree`]
#[derive(Debug)]
#[must_use]
pub struct Builder {
root: PathBuf,
Expand Down Expand Up @@ -101,7 +103,6 @@ impl Tree {
/// let root = "/index";
/// let download = "https://my-crates-server.com/api/v1/crates/{crate}/{version}/download";
///
///
/// let index_tree = Tree::initialise(root, download)
/// .api(Url::parse("https://my-crates-server.com/").unwrap())
/// .allowed_registry(Url::parse("https://my-intranet:8080/index").unwrap())
Expand Down Expand Up @@ -207,14 +208,14 @@ impl Tree {
/// # .await
/// # .expect("couldn't create tree");
/// #
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.yank(crate_name, &version).await? {
/// Ok(()) => println!("crate yanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.yank(crate_name, &version).await? {
/// Ok(()) => println!("crate yanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// #
/// # Ok(())
/// # }
Expand Down Expand Up @@ -263,14 +264,14 @@ impl Tree {
/// # .await
/// # .expect("couldn't create tree");
/// #
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.unyank(crate_name, &version).await? {
/// Ok(()) => println!("crate unyanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// let crate_name = "some-crate";
/// let version = "0.1.0".parse().unwrap();
///
/// match tree.unyank(crate_name, &version).await? {
/// Ok(()) => println!("crate unyanked!"),
/// Err(NotFoundError::Crate(e)) => println!("crate not found! ({})", e.crate_name()),
/// Err(NotFoundError::Version(e)) => println!("version not found! ({})", e.version()),
/// }
/// #
/// # Ok(())
/// # }
Expand Down Expand Up @@ -383,7 +384,9 @@ impl CrateNotFoundError {
/// match error {
/// NotFoundError::Crate(e) => println!("couldn't find crate {}", e.crate_name()),
/// NotFoundError::Version(e) => println!(
/// "found crate {} but no version matching {}", e.crate_name(), e.version()
/// "found crate {} but no version matching {}",
/// e.crate_name(),
/// e.version()
/// ),
/// }
/// ```
Expand Down
4 changes: 2 additions & 2 deletions src/index/tree/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,17 @@ fn get_path(name: impl AsRef<str>) -> PathBuf {
}

impl<'a> IntoIterator for &'a IndexFile {
type Item = &'a Record;
type IntoIter = std::collections::btree_map::Values<'a, Version, Record>;
type Item = &'a Record;

fn into_iter(self) -> Self::IntoIter {
self.entries.values()
}
}

impl IntoIterator for IndexFile {
type Item = (Version, Record);
type IntoIter = std::collections::btree_map::IntoIter<Version, Record>;
type Item = (Version, Record);

fn into_iter(self) -> Self::IntoIter {
self.entries.into_iter()
Expand Down
10 changes: 4 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![deny(clippy::all, missing_docs)]
#![deny(clippy::all, missing_docs, missing_debug_implementations)]
#![warn(clippy::pedantic)]

//! Crate-Index is a library for managing and manipulating a Cargo crate
Expand All @@ -8,17 +8,15 @@
//!
//! # Basic Usage
//! ```no_run
//! use crate_index::{Index, Url, Record, Version};
//! use crate_index::{Index, Record, Url, Version};
//! # use crate_index::Error;
//!
//! # async {
//! // Create a new index, backed by the filesystem and a git repository
//! let root = "/index";
//! let download = "https://my-crates-server.com/api/v1/crates/{crate}/{version}/download";
//!
//! let mut index = Index::initialise(root, download)
//! .build()
//! .await?;
//! let mut index = Index::initialise(root, download).build().await?;
//!
//! // Create a new crate 'Record' object
//! let name = "foo";
Expand All @@ -36,7 +34,7 @@
//!
//! # Requirements
//!
//! - Minimum compiler version: **1.39.0**
//! - Minimum compiler version: **1.46.0**
pub mod record;
#[doc(inline)]
Expand Down
3 changes: 2 additions & 1 deletion src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ pub(crate) fn name(name: &str) -> Result<(), Error> {
} else if !REGEX.is_match(name) || !name.is_ascii() {
Err(Error::invalid_name(
name,
"crate name must be ASCII, be alphanumeric + '-' and '_', and begin with a letter ([a-zA-Z][a-zA-Z0-9-_]*).",
"crate name must be ASCII, be alphanumeric + '-' and '_', and begin with a letter \
([a-zA-Z][a-zA-Z0-9-_]*).",
))
} else {
Ok(())
Expand Down

0 comments on commit 6f05c83

Please sign in to comment.