Skip to content

Commit

Permalink
refactor: remove rayon and anyhow (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
camshaft authored Jan 8, 2025
1 parent e15a039 commit 6ad5395
Show file tree
Hide file tree
Showing 21 changed files with 144 additions and 167 deletions.
1 change: 0 additions & 1 deletion duvet-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ http = ["dep:http", "reqwest"]
testing = ["tracing-subscriber"]

[dependencies]
anyhow = "1"
blake3 = "1"
bytes = "1"
console = "0.15"
Expand Down
6 changes: 0 additions & 6 deletions duvet-core/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ impl Diagnostic for Error {
}
}

impl From<anyhow::Error> for Error {
fn from(value: anyhow::Error) -> Self {
Report::msg(value).into()
}
}

impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Report::msg(value).into()
Expand Down
3 changes: 0 additions & 3 deletions duvet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ include = [
]

[dependencies]
anyhow = "1"
clap = { version = "4", features = ["derive"] }
duvet-core = { version = "0.1", path = "../duvet-core" }
fnv = { version = "1", default-features = false }
Expand All @@ -23,7 +22,6 @@ mimalloc = { version = "0.1", default-features = false }
once_cell = "1"
pathdiff = "0.2"
pulldown-cmark = { version = "0.12", default-features = false }
rayon = "1"
regex = "1"
serde = { version = "1", features = ["derive"] }
serde_spanned = "0.6"
Expand All @@ -39,4 +37,3 @@ v_jsonescape = "0.7"
insta = { version = "1", features = ["filters", "json"] }
serde_json = "1"
tempfile = "3"

25 changes: 12 additions & 13 deletions duvet/src/annotation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ use crate::{
target::{Target, TargetSet},
Error, Result,
};
use anyhow::anyhow;
use core::{fmt, ops::Range, str::FromStr};
use duvet_core::{diagnostic::IntoDiagnostic, file::Slice, path::Path, query};
use duvet_core::{diagnostic::IntoDiagnostic, error, file::Slice, path::Path, query};
use serde::Serialize;
use std::{
collections::{BTreeSet, HashMap, HashSet},
Expand All @@ -23,20 +22,20 @@ pub type AnnotationReferenceMap<'a> =
HashMap<(Target, Option<&'a str>), Vec<(usize, &'a Annotation)>>;

pub trait AnnotationSetExt {
fn targets(&self) -> Result<TargetSet, Error>;
fn reference_map(&self) -> Result<AnnotationReferenceMap, Error>;
fn targets(&self) -> Result<TargetSet>;
fn reference_map(&self) -> Result<AnnotationReferenceMap>;
}

impl AnnotationSetExt for AnnotationSet {
fn targets(&self) -> Result<TargetSet, Error> {
fn targets(&self) -> Result<TargetSet> {
let mut set = TargetSet::new();
for anno in self.iter() {
set.insert(anno.target()?);
}
Ok(set)
}

fn reference_map(&self) -> Result<AnnotationReferenceMap, Error> {
fn reference_map(&self) -> Result<AnnotationReferenceMap> {
let mut map = AnnotationReferenceMap::new();
for (id, anno) in self.iter().enumerate() {
let target = anno.target()?;
Expand Down Expand Up @@ -98,7 +97,7 @@ pub struct Annotation {
}

impl Annotation {
pub fn target(&self) -> Result<Target, Error> {
pub fn target(&self) -> Result<Target> {
Target::from_annotation(self)
}

Expand Down Expand Up @@ -135,7 +134,7 @@ impl Annotation {
})
}

pub fn resolve_file(&self, file: &std::path::Path) -> Result<PathBuf, Error> {
pub fn resolve_file(&self, file: &std::path::Path) -> Result<PathBuf> {
// If we have the right path, just return it
if file.is_file() {
return Ok(file.to_path_buf());
Expand All @@ -152,7 +151,7 @@ impl Annotation {
}
}

Err(anyhow!(format!("Could not resolve file {:?}", file)))
Err(error!("Could not resolve file {:?}", file))
}

pub fn quote_range(&self, contents: &str) -> Option<Range<usize>> {
Expand Down Expand Up @@ -200,7 +199,7 @@ impl FromStr for AnnotationType {
"EXCEPTION" | "exception" => Ok(Self::Exception),
"TODO" | "todo" => Ok(Self::Todo),
"IMPLICATION" | "implication" => Ok(Self::Implication),
_ => Err(anyhow!(format!(
_ => Err(error!(
"Invalid annotation type {:?}, expected one of {:?}",
v,
[
Expand All @@ -211,7 +210,7 @@ impl FromStr for AnnotationType {
"todo",
"implication"
]
))),
)),
}
}
}
Expand Down Expand Up @@ -255,11 +254,11 @@ impl FromStr for AnnotationLevel {
"MUST" => Ok(Self::Must),
"SHOULD" => Ok(Self::Should),
"MAY" => Ok(Self::May),
_ => Err(anyhow!(format!(
_ => Err(error!(
"Invalid annotation level {:?}, expected one of {:?}",
v,
["AUTO", "MUST", "SHOULD", "MAY"]
))),
)),
}
}
}
12 changes: 7 additions & 5 deletions duvet/src/comment.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::annotation::{AnnotationSet, AnnotationType};
use anyhow::anyhow;
use duvet_core::{diagnostic::Error, file::SourceFile};
use crate::{
annotation::{AnnotationSet, AnnotationType},
Error, Result,
};
use duvet_core::{error, file::SourceFile};
use std::sync::Arc;

pub mod parser;
Expand Down Expand Up @@ -42,11 +44,11 @@ impl Default for Pattern {
}

impl Pattern {
pub fn from_arg(arg: &str) -> Result<Self, anyhow::Error> {
pub fn from_arg(arg: &str) -> Result<Self> {
let mut parts = arg.split(',').filter(|p| !p.is_empty());
let meta = parts.next().expect("should have at least one pattern");
if meta.is_empty() {
return Err(anyhow!("compliance pattern cannot be empty"));
return Err(error!("compliance pattern cannot be empty"));
}

let content = parts.next().unwrap();
Expand Down
16 changes: 7 additions & 9 deletions duvet/src/comment/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use super::tokenizer::Token;
use crate::{
annotation::{Annotation, AnnotationLevel, AnnotationType},
specification::Format,
Error,
};
use anyhow::anyhow;
use duvet_core::{
diagnostic::Error,
ensure,
ensure, error,
file::{Slice, SourceFile},
Result,
};
Expand Down Expand Up @@ -42,7 +41,7 @@ struct Meta {
}

impl Meta {
fn set(&mut self, key: Option<Slice>, value: Slice) -> Result<()> {
fn set(&mut self, key: Option<Slice>, value: Slice) -> Result {
let source_value = value.clone();

let prev = match key.as_deref() {
Expand All @@ -65,15 +64,14 @@ impl Meta {
Some(_) => {
return Err(key
.unwrap()
.error(anyhow!("invalid metadata field"), "defined here"));
.error(error!("invalid metadata field"), "defined here"));
}
None => core::mem::replace(&mut self.target, Some(value)),
};

if let Some(prev) = prev {
let key = key.as_deref().unwrap_or("source");
let err: Error = anyhow!("{key:?} already specified").into();
let err = err
let err = error!("{key:?} already specified")
.with_source_slice(source_value, "redefined here")
.with_source_slice(prev, "already defined here");
return Err(err);
Expand All @@ -88,7 +86,7 @@ impl Meta {

let Some(target) = self.target else {
return Err(first_meta.error(
anyhow!("comment is missing source specification"),
error!("comment is missing source specification"),
"defined here",
));
};
Expand All @@ -104,7 +102,7 @@ impl Meta {
] {
if anno != allowed {
if let Some(value) = field {
return Err(value.error(anyhow!("invalid key for type {anno}"), "defined here"));
return Err(value.error(error!("invalid key for type {anno}"), "defined here"));
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions duvet/src/comment/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@

use super::*;

fn parse(pattern: &str, value: &str) -> (AnnotationSet, Vec<Error>) {
fn parse(pattern: &str, value: &str) -> (AnnotationSet, Vec<String>) {
let file = SourceFile::new("file.rs", value).unwrap();
let pattern = Pattern::from_arg(pattern).unwrap();
extract(&file, &pattern, Default::default())
let (annotations, errors) = extract(&file, &pattern, Default::default());
let errors = errors.into_iter().map(|error| error.to_string()).collect();
(annotations, errors)
}

macro_rules! snapshot {
Expand Down
73 changes: 35 additions & 38 deletions duvet/src/extract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ use crate::{
annotation::AnnotationLevel,
specification::{Format, Line, Section, Specification},
target::TargetPath,
Error,
Result,
};
use clap::Parser;
use duvet_core::diagnostic::IntoDiagnostic;
use lazy_static::lazy_static;
use rayon::prelude::*;
use regex::{Regex, RegexSet};
use std::{fs::OpenOptions, io::BufWriter, path::PathBuf};

Expand All @@ -34,10 +34,10 @@ lazy_static! {
.iter()
.cloned()
.map(|(pat, l)| {
let r = Regex::new(&format!("\\b{}\\b\"?", pat))?;
let r = Regex::new(&format!("\\b{}\\b\"?", pat)).into_diagnostic()?;
Ok((r, l))
})
.collect::<Result<_, Error>>()
.collect::<Result<_>>()
.unwrap()
};
static ref KEY_WORDS_SET: RegexSet =
Expand Down Expand Up @@ -67,7 +67,7 @@ pub struct Extract {
}

impl Extract {
pub async fn exec(&self) -> Result<(), Error> {
pub async fn exec(&self) -> Result {
let contents = self.target.load(self.spec_path.as_deref()).await?;
let local_path = contents.path();

Expand All @@ -80,37 +80,34 @@ impl Extract {
todo!("single file not implemented");
} else {
// output to directory
sections
.par_iter()
.map(|(section, features)| {
// The specification may be stored alongside the extracted TOML.
let mut out = match local_path.strip_prefix(&self.out) {
Ok(path) => self.out.join(path),
Err(_e) => self.out.join(local_path),
};

out.set_extension("");
let _ = std::fs::create_dir_all(&out);
out.push(format!("{}.{}", section.id, self.extension));

let file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(out)?;
let mut file = BufWriter::new(file);

let target = &self.target;

match &self.extension[..] {
"rs" => write_rust(&mut file, target, section, features)?,
"toml" => write_toml(&mut file, target, section, features)?,
ext => unimplemented!("{}", ext),
}
sections.iter().try_for_each(|(section, features)| {
// The specification may be stored alongside the extracted TOML.
let mut out = match local_path.strip_prefix(&self.out) {
Ok(path) => self.out.join(path),
Err(_e) => self.out.join(local_path),
};

out.set_extension("");
let _ = std::fs::create_dir_all(&out);
out.push(format!("{}.{}", section.id, self.extension));

let file = OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(out)?;
let mut file = BufWriter::new(file);

let target = &self.target;

match &self.extension[..] {
"rs" => write_rust(&mut file, target, section, features)?,
"toml" => write_toml(&mut file, target, section, features)?,
ext => unimplemented!("{}", ext),
}

Ok(())
})
.collect::<Result<(), std::io::Error>>()?;
<Result>::Ok(())
})?;
}

Ok(())
Expand All @@ -119,7 +116,7 @@ impl Extract {

fn extract_sections(spec: &Specification) -> Vec<(&Section, Vec<Feature<'_>>)> {
spec.sorted_sections()
.par_iter()
.iter()
.map(|section| extract_section(section))
.filter(|(_section, features)| !features.is_empty())
.collect()
Expand Down Expand Up @@ -339,7 +336,7 @@ fn write_rust<W: std::io::Write>(
target: &TargetPath,
section: &Section,
features: &[Feature],
) -> Result<(), std::io::Error> {
) -> Result {
writeln!(w, "//! {}#{}", target, section.id)?;
writeln!(w, "//!")?;
writeln!(w, "//! {}", section.full_title)?;
Expand Down Expand Up @@ -369,7 +366,7 @@ fn write_toml<W: std::io::Write>(
target: &TargetPath,
section: &Section,
features: &[Feature],
) -> Result<(), std::io::Error> {
) -> Result {
writeln!(w, "target = \"{}#{}\"", target, section.id)?;
writeln!(w)?;
writeln!(w, "# {}", section.full_title)?;
Expand Down
5 changes: 2 additions & 3 deletions duvet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ mod text;
#[cfg(test)]
mod tests;

pub use anyhow::Error;
pub use duvet_core::Result;
pub use duvet_core::{diagnostic::Error, Result};

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Parser)]
Expand All @@ -33,7 +32,7 @@ pub async fn arguments() -> Arc<Arguments> {
}

impl Arguments {
pub async fn exec(&self) -> Result<(), Error> {
pub async fn exec(&self) -> Result {
match self {
Self::Extract(args) => args.exec().await,
Self::Report(args) => args.exec().await,
Expand Down
Loading

0 comments on commit 6ad5395

Please sign in to comment.