Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
- Derives `PartialOrd` and `Ord` instead of manually implementing it.
- Imports modules and types that are used multiple times.
- Cleans up `use` statements in some places.
  • Loading branch information
xfbs committed Oct 17, 2023
1 parent dc9d20c commit c04ffbc
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 56 deletions.
4 changes: 2 additions & 2 deletions src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
};

#[cfg(feature = "build")]
use crate::{generator, generator::Language};
use crate::generator::{Generator, Language};
#[cfg(feature = "build")]
use std::path::PathBuf;

Expand Down Expand Up @@ -281,7 +281,7 @@ pub async fn uninstall() -> miette::Result<()> {
/// Generate bindings for a given language
#[cfg(feature = "build")]
pub async fn generate(language: Language, out_dir: PathBuf) -> miette::Result<()> {
generator::Generator::Protoc { language, out_dir }
Generator::Protoc { language, out_dir }
.generate()
.await
.wrap_err(miette!("failed to generate {language} bindings"))
Expand Down
11 changes: 6 additions & 5 deletions src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use std::{
use miette::{ensure, miette, Context, IntoDiagnostic};
use protoc_bin_vendored::protoc_bin_path;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};

use crate::{manifest::Manifest, package::PackageStore};

Expand Down Expand Up @@ -99,7 +100,7 @@ impl Generator {

protoc_cmd.args(&protos);

tracing::debug!(":: running {protoc_cmd:?}");
debug!(":: running {protoc_cmd:?}");

let output = protoc_cmd.output().await.into_diagnostic()?;

Expand All @@ -113,7 +114,7 @@ impl Generator {
String::from_utf8_lossy(&output.stderr)
);

tracing::info!(":: {language} code generated successfully");
info!(":: {language} code generated successfully");
}
}

Expand All @@ -126,7 +127,7 @@ impl Generator {
pub async fn generate(&self) -> miette::Result<()> {
let manifest = Manifest::read().await?;

tracing::info!(":: initializing code generator");
info!(":: initializing code generator");

ensure!(
manifest.package.kind.compilable() || !manifest.dependencies.is_empty(),
Expand All @@ -139,7 +140,7 @@ impl Generator {

if manifest.package.kind.compilable() {
let location = Path::new(PackageStore::PROTO_PATH);
tracing::info!(
info!(
":: compiled {} [{}]",
manifest.package.name,
location.display()
Expand All @@ -148,7 +149,7 @@ impl Generator {

for dependency in manifest.dependencies {
let location = PackageStore::locate(&dependency.package);
tracing::info!(
info!(
":: compiled {} [{}]",
dependency.package,
location.display()
Expand Down
51 changes: 9 additions & 42 deletions src/lock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ use crate::{
pub const LOCKFILE: &str = "Proto.lock";

/// Supported types of digest algorithms
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum DigestAlgorithm {
// Do not reorder variants: the ordering is significant, see #38 and #106.
/// 256 bits variant of SHA-2
#[serde(rename = "sha256")]
SHA256,
Expand Down Expand Up @@ -64,27 +65,12 @@ impl fmt::Display for DigestAlgorithm {
}
}

impl Ord for DigestAlgorithm {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self {
DigestAlgorithm::SHA256 => match other {
DigestAlgorithm::SHA256 => std::cmp::Ordering::Equal,
},
}
}
}

impl PartialOrd for DigestAlgorithm {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

/// A representation of a cryptographic digest for data integrity validation
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, PartialEq, Eq, Ord, PartialOrd)]
pub struct Digest {
bytes: Vec<u8>,
// Do not reorder fields: the ordering is significant, see #38 and #106.
algorithm: DigestAlgorithm,
bytes: Vec<u8>,
}

impl TryFrom<digest::Digest> for Digest {
Expand All @@ -104,13 +90,9 @@ impl TryFrom<digest::Digest> for Digest {
}
}

impl std::fmt::Display for Digest {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!(
"{}:{}",
self.algorithm,
hex::encode(&self.bytes)
))
impl fmt::Display for Digest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.algorithm, hex::encode(&self.bytes))
}
}

Expand All @@ -128,7 +110,7 @@ struct DigestVisitor;
impl<'de> Visitor<'de> for DigestVisitor {
type Value = Digest;

fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a hexadecimal encoded cryptographic digest")
}

Expand Down Expand Up @@ -158,21 +140,6 @@ impl<'de> Deserialize<'de> for Digest {
}
}

impl Ord for Digest {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
match self.algorithm.cmp(&other.algorithm) {
std::cmp::Ordering::Equal => self.bytes.cmp(&other.bytes),
other => other,
}
}
}

impl PartialOrd for Digest {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}

/// Captures immutable metadata about a given package
///
/// It is used to ensure that future installations will use the exact same dependencies.
Expand Down
11 changes: 6 additions & 5 deletions src/registry/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,13 @@ impl LocalRegistry {

#[cfg(test)]
mod tests {
use crate::manifest::{Dependency, Manifest, PackageManifest};
use crate::package::{Package, PackageType};
use crate::registry::local::LocalRegistry;
use crate::{
manifest::{Dependency, Manifest, PackageManifest},
package::{Package, PackageType},
registry::local::LocalRegistry,
};
use bytes::Bytes;
use std::env;
use std::path::PathBuf;
use std::{env, path::PathBuf};
use tokio::fs;

#[tokio::test]
Expand Down
4 changes: 2 additions & 2 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

use std::{
fmt::{self, Display},
ops::DerefMut,
ops::{Deref, DerefMut},
str::FromStr,
};

Expand All @@ -41,7 +41,7 @@ impl From<RegistryUri> for Url {
}
}

impl std::ops::Deref for RegistryUri {
impl Deref for RegistryUri {
type Target = Url;

fn deref(&self) -> &Self::Target {
Expand Down

0 comments on commit c04ffbc

Please sign in to comment.