Skip to content

Commit

Permalink
style: add a .rustfmt.toml
Browse files Browse the repository at this point in the history
enforces a slightly more opinionate style that should help keep the
code a bit more consistent.
  • Loading branch information
nrdxp committed Oct 17, 2024
1 parent 8f29531 commit 7c57805
Show file tree
Hide file tree
Showing 27 changed files with 239 additions and 188 deletions.
21 changes: 21 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
edition = "2021"
newline_style = "unix"
use_field_init_shorthand = true
use_try_shorthand = true

unstable_features = true

comment_width = 100
condense_wildcard_suffixes = true
error_on_line_overflow = true
format_code_in_doc_comments = true
format_macro_bodies = true
format_macro_matchers = true
format_strings = true
group_imports = "StdExternalCrate"
imports_granularity = "Module"
match_block_trailing_comma = true
normalize_doc_attributes = true
reorder_impl_items = true
style_edition = "2024"
wrap_comments = true
9 changes: 6 additions & 3 deletions crates/atom/src/core.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::{borrow::Cow, path::Path};

use super::id::Id;
use std::borrow::Cow;
use std::path::Path;

use semver::Version;
use serde::{Deserialize, Serialize};

use super::id::Id;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
/// Represents the deserialized form of an Atom, directly constructed from the TOML manifest.
///
Expand Down Expand Up @@ -69,9 +70,11 @@ impl AtomPaths<PathBuf> {
pub fn lock(&self) -> &Path {
self.lock.as_ref()
}

pub fn spec(&self) -> &Path {
self.spec.as_ref()
}

pub fn content(&self) -> &Path {
self.content.as_ref()
}
Expand Down
9 changes: 7 additions & 2 deletions crates/atom/src/id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#[cfg(test)]
mod tests;

use serde::{Deserialize, Serialize, Serializer};

use std::borrow::Borrow;
use std::fmt;
use std::ops::Deref;
use std::str::FromStr;

use serde::{Deserialize, Serialize, Serializer};
use thiserror::Error;
use unic_ucd_category::GeneralCategory;

Expand Down Expand Up @@ -80,6 +80,7 @@ impl<R> Serialize for AtomId<R> {

impl<T> Deref for AtomHash<'_, T> {
type Target = [u8; 32];

fn deref(&self) -> &Self::Target {
&self.hash
}
Expand Down Expand Up @@ -124,6 +125,7 @@ where
let root = src.calculate_root()?;
Ok(AtomId { root, id })
}

/// The root field, which serves as a derived key for the blake-3 hash used to
/// identify the Atom in backend implementations.
pub fn root(&self) -> &R {
Expand Down Expand Up @@ -158,6 +160,7 @@ impl Id {

Ok(())
}

pub(super) fn is_invalid_start(c: char) -> bool {
matches!(
GeneralCategory::of(c),
Expand All @@ -166,6 +169,7 @@ impl Id {
|| c == '-'
|| !Id::is_valid_char(c)
}

pub(super) fn is_valid_char(c: char) -> bool {
matches!(
GeneralCategory::of(c),
Expand All @@ -183,6 +187,7 @@ impl Id {

impl Deref for Id {
type Target = String;

fn deref(&self) -> &Self::Target {
&self.0
}
Expand Down
7 changes: 3 additions & 4 deletions crates/atom/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ pub mod publish;
pub mod store;
pub mod uri;
pub use core::Atom;
pub use id::AtomId;
pub use id::CalculateRoot;
pub use manifest::Manifest;

use std::sync::LazyLock;

pub use id::{AtomId, CalculateRoot};
pub use manifest::Manifest;
const TOML: &str = "toml";
const BASE32: base32::Alphabet = base32::Alphabet::Rfc4648HexLower { padding: false };
static ATOM_EXT: LazyLock<String> = LazyLock::new(|| format!("@.{}", crate::TOML));
14 changes: 10 additions & 4 deletions crates/atom/src/manifest.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
//! # Atom Manifest
//!
//! Provides the core types for working with an Atom's manifest format.
use crate::Atom;
use serde::{Deserialize, Serialize};
mod depends;

use std::collections::HashMap;
use std::str::FromStr;

use serde::{Deserialize, Serialize};
use thiserror::Error;
use toml_edit::de;
use toml_edit::DocumentMut;
use toml_edit::{DocumentMut, de};

use crate::Atom;
use crate::id::Id;

/// Errors which occur during manifest (de)serialization.
#[derive(Error, Debug)]
Expand Down Expand Up @@ -53,6 +58,7 @@ impl Manifest {

impl FromStr for Manifest {
type Err = de::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
de::from_str(s)
}
Expand Down
1 change: 1 addition & 0 deletions crates/atom/src/manifest/depends.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl AtomSrc {
_ => None,
}
}

pub(crate) fn path(self) -> Option<PathBuf> {
match self {
AtomSrc::Path(path) => Some(path),
Expand Down
12 changes: 7 additions & 5 deletions crates/atom/src/publish/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ pub enum PublishError {
#[cfg(feature = "git")]
pub mod git {
//! # Git Publishing Errors
use crate::store::git::Root;
use gix::object;
use std::path::PathBuf;

use gix::object;

use crate::store::git::Root;
/// An error representing a failure during publishing to a Git Ekala store.
#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -103,13 +105,13 @@ pub mod git {
remote_root = %**remote,
suggest = Error::INCONSISTENT_ROOT_SUGGESTION
);
}
},
Error::Invalid(e, path) => {
tracing::warn!(message = %self, path = %path.display(), message = format!("\n{}", e));
}
},
Error::NotAnAtom(path) => {
tracing::warn!(message = %self, path = %path.display());
}
},
Error::Failed => (),
_ => tracing::warn!(message = %self),
}
Expand Down
50 changes: 22 additions & 28 deletions crates/atom/src/publish/git/inner.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,22 @@
use super::{
super::{ATOM_FORMAT_VERSION, ATOM_MANIFEST, EMPTY_SIG},
AtomContext, AtomRef, GitContext, GitResult, RefKind,
};
use crate::{
core::AtomPaths,
publish::{error::git::Error, ATOM_ORIGIN},
store::git,
Atom, AtomId, Manifest,
};

use gix::{
actor::Signature,
diff::object::Commit as AtomCommit,
object::tree::Entry,
objs::{tree::Entry as AtomEntry, WriteTo},
worktree::object::Tree as AtomTree,
ObjectId, Reference,
};
use std::{
io::{self, Read},
os::unix::ffi::OsStrExt,
path::Path,
};

use std::path::PathBuf;
use std::io::{self, Read};
use std::os::unix::ffi::OsStrExt;
use std::path::{Path, PathBuf};

use gix::actor::Signature;
use gix::diff::object::Commit as AtomCommit;
use gix::object::tree::Entry;
use gix::objs::WriteTo;
use gix::objs::tree::Entry as AtomEntry;
use gix::worktree::object::Tree as AtomTree;
use gix::{ObjectId, Reference};

use super::super::{ATOM_FORMAT_VERSION, ATOM_MANIFEST, EMPTY_SIG};
use super::{AtomContext, AtomRef, GitContext, GitResult, RefKind};
use crate::core::AtomPaths;
use crate::publish::ATOM_ORIGIN;
use crate::publish::error::git::Error;
use crate::store::git;
use crate::{Atom, AtomId, Manifest};
impl<'a> GitContext<'a> {
/// Method to verify the manifest of an entry
pub(super) fn verify_manifest(&self, obj: &Object, path: &Path) -> GitResult<Atom> {
Expand Down Expand Up @@ -149,13 +142,13 @@ impl<'a> AtomContext<'a> {
false
}
}

/// Method to write the atom tree object
pub(super) fn write_atom_tree(
&self,
entries: &AtomEntries,
) -> GitResult<MaybeSkipped<AtomTreeId>> {
use Err as Skipped;
use Ok as Wrote;
use {Err as Skipped, Ok as Wrote};

let mut entries: Vec<_> = entries.iter().map(atom_entry).collect();

Expand Down Expand Up @@ -320,6 +313,7 @@ impl CommittedAtom {
pub fn commit(&self) -> &AtomCommit {
&self.commit
}

#[must_use]
/// Returns a reference to the tip of this [`CommittedAtom`].
pub fn tip(&self) -> &ObjectId {
Expand Down
51 changes: 29 additions & 22 deletions crates/atom/src/publish/git/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,19 @@ mod test;

mod inner;

use super::{error::git::Error, Content, PublishOutcome, Record};
use crate::{
core::AtomPaths,
store::{git::Root, NormalizeStorePath},
Atom, AtomId,
};

use gix::Commit;
use gix::{ObjectId, Repository, Tree};
use std::cell::RefCell;
use std::path::{Path, PathBuf};

use gix::{Commit, ObjectId, Repository, Tree};
use tokio::task::JoinSet;

use super::error::git::Error;
use super::{Content, PublishOutcome, Record};
use crate::core::AtomPaths;
use crate::store::NormalizeStorePath;
use crate::store::git::Root;
use crate::{Atom, AtomId};

type GitAtomId = AtomId<Root>;
/// The Outcome of an Atom publish attempt to a Git store.
pub type GitOutcome = PublishOutcome<Root>;
Expand Down Expand Up @@ -198,7 +198,7 @@ impl<'a> StateValidator<Root> for GitPublisher<'a> {
return Err(Error::Duplicates);
}
atoms.insert(atom.id, path);
}
},
Err(e) => e.warn(),
}
}
Expand Down Expand Up @@ -228,31 +228,36 @@ impl GitContent {
pub fn spec(&self) -> &gix::refs::Reference {
&self.spec
}

/// Return a reference to the Atom src Git ref.
#[must_use]
pub fn origin(&self) -> &gix::refs::Reference {
&self.origin
}

/// Return a reference to the Atom content ref.
#[must_use]
pub fn content(&self) -> &gix::refs::Reference {
&self.content
}

/// Return a reference to the path to the Atom.
#[must_use]
pub fn path(&self) -> &PathBuf {
&self.path
}

/// Return a reference to the atom ref prefix.
#[must_use]
pub fn ref_prefix(&self) -> &String {
&self.ref_prefix
}
}

use std::collections::HashMap;

use super::Publish;
use crate::id::Id;
use std::collections::HashMap;

impl<'a> super::private::Sealed for GitContext<'a> {}

Expand All @@ -261,12 +266,15 @@ impl<'a> Publish<Root> for GitContext<'a> {

/// Publishes atoms.
///
/// This function processes a collection of paths, each representing an atom to be published. The publishing
/// process includes path normalization, existence checks, and actual publishing attempts.
/// This function processes a collection of paths, each representing an atom to be published.
/// The publishing process includes path normalization, existence checks, and actual
/// publishing attempts.
///
/// # Path Normalization
/// - First attempts to interpret each path as relative to the caller's current location inside the repository.
/// - If normalization fails (e.g., in a bare repository), falls back to treating the path as already relative to the repo root.
/// - First attempts to interpret each path as relative to the caller's current location inside
/// the repository.
/// - If normalization fails (e.g., in a bare repository), falls back to treating the path as
/// already relative to the repo root.
/// - The normalized path is used to search the Git history, not the file system.
///
/// # Publishing Process
Expand All @@ -285,8 +293,8 @@ impl<'a> Publish<Root> for GitContext<'a> {
///
/// # Return Value
/// Returns a vector of results types (`Vec<Result<PublishOutcome<T>, Self::Error>>`), where the
/// outter result represents whether an atom has failed, and the inner result determines whether an
/// atom was safely skipped, e.g. because it already exists..
/// outter result represents whether an atom has failed, and the inner result determines whether
/// an atom was safely skipped, e.g. because it already exists..
fn publish<C>(&self, paths: C) -> Vec<GitResult<GitOutcome>>
where
C: IntoIterator<Item = PathBuf>,
Expand All @@ -306,8 +314,7 @@ impl<'a> Publish<Root> for GitContext<'a> {
}

fn publish_atom<P: AsRef<Path>>(&self, path: P) -> GitResult<GitOutcome> {
use Err as Skipped;
use Ok as Published;
use {Err as Skipped, Ok as Published};

let atom = AtomContext::set(path.as_ref(), self)?;

Expand Down Expand Up @@ -385,13 +392,13 @@ impl<'a> GitContext<'a> {
if !output.is_empty() {
tracing::info!(output = %String::from_utf8_lossy(&output));
}
}
},
Ok(Err(e)) => {
errors.push(e);
}
},
Err(e) => {
errors.push(Error::JoinFailed(e));
}
},
}
}
}
Expand Down
Loading

0 comments on commit 7c57805

Please sign in to comment.