Skip to content

Commit

Permalink
improve
Browse files Browse the repository at this point in the history
  • Loading branch information
h-a-n-a committed Dec 18, 2023
1 parent 34a3dc8 commit a915742
Show file tree
Hide file tree
Showing 13 changed files with 47 additions and 30 deletions.
4 changes: 2 additions & 2 deletions crates/rspack_core/src/context_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{
use itertools::Itertools;
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use rspack_error::{miette::IntoDiagnostic, Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, miette::IntoDiagnostic, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rspack_regex::RspackRegex;
Expand Down Expand Up @@ -649,7 +649,7 @@ impl Module for ContextModule {
}
}

impl Diagnosable for ContextModule {}
impl_empty_diagnosable_trait!(ContextModule);

impl Identifiable for ContextModule {
fn identifier(&self) -> Identifier {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/external_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::borrow::Cow;
use std::hash::Hash;
use std::iter;

use rspack_error::{internal_error, Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, internal_error, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};
use rustc_hash::FxHashMap as HashMap;
Expand Down Expand Up @@ -429,7 +429,7 @@ impl Module for ExternalModule {
}
}

impl Diagnosable for ExternalModule {}
impl_empty_diagnosable_trait!(ExternalModule);

impl Hash for ExternalModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/missing_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::hash::Hash;

use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::{RawSource, Source, SourceExt};
use serde_json::json;
Expand Down Expand Up @@ -104,7 +104,7 @@ impl Identifiable for MissingModule {
}
}

impl Diagnosable for MissingModule {}
impl_empty_diagnosable_trait!(MissingModule);

impl PartialEq for MissingModule {
fn eq(&self, other: &Self) -> bool {
Expand Down
7 changes: 2 additions & 5 deletions crates/rspack_core/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ mod test {
use std::borrow::Cow;
use std::hash::Hash;

use rspack_error::{Diagnosable, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, Result};
use rspack_identifier::{Identifiable, Identifier};
use rspack_sources::Source;

Expand Down Expand Up @@ -425,10 +425,7 @@ mod test {
(stringify!($ident).to_owned() + self.0).into()
}

async fn build(
&mut self,
_build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
async fn build(&mut self, _build_context: BuildContext<'_>) -> Result<BuildResult> {
unreachable!()
}

Expand Down
7 changes: 2 additions & 5 deletions crates/rspack_core/src/module_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ impl ModuleGraph {
mod test {
use std::borrow::Cow;

use rspack_error::{Diagnosable, Result, TWithDiagnosticArray};
use rspack_error::{Diagnosable, Result};
use rspack_identifier::Identifiable;
use rspack_sources::Source;

Expand Down Expand Up @@ -835,10 +835,7 @@ mod test {
unreachable!()
}

async fn build(
&mut self,
_build_context: BuildContext<'_>,
) -> Result<TWithDiagnosticArray<BuildResult>> {
async fn build(&mut self, _build_context: BuildContext<'_>) -> Result<BuildResult> {
unreachable!()
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_core/src/raw_module.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::hash::Hash;

use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_hash::RspackHash;
use rspack_identifier::Identifiable;
use rspack_sources::{BoxSource, RawSource, Source, SourceExt};
Expand Down Expand Up @@ -120,7 +120,7 @@ impl Module for RawModule {
}
}

impl Diagnosable for RawModule {}
impl_empty_diagnosable_trait!(RawModule);

impl Hash for RawModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
23 changes: 23 additions & 0 deletions crates/rspack_error/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,29 @@ pub trait Diagnosable {
}
}

#[macro_export]
macro_rules! impl_empty_diagnosable_trait {
($ty:ty) => {
impl $crate::Diagnosable for $ty {
fn add_diagnostic(&self, _diagnostic: $crate::Diagnostic) {
unimplemented!(
"`<{ty} as Diagnostable>::add_diagnostic` is not implemented",
ty = stringify!($ty)
)
}
fn add_diagnostics(&self, _diagnostics: Vec<$crate::Diagnostic>) {
unimplemented!(
"`<{ty} as Diagnostable>::add_diagnostics` is not implemented",
ty = stringify!($ty)
)
}
fn clone_diagnostics(&self) -> Vec<$crate::Diagnostic> {
vec![]
}
}
};
}

pub fn errors_to_diagnostics(errs: Vec<Error>) -> Vec<Diagnostic> {
errs.into_iter().map(Diagnostic::from).collect()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rspack_core::{
LibIdentOptions, Module, ModuleDependency, ModuleIdentifier, ModuleType, RuntimeGlobals,
RuntimeSpec, SourceType,
};
use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};

Expand Down Expand Up @@ -224,7 +224,7 @@ impl Module for ContainerEntryModule {
}
}

impl Diagnosable for ContainerEntryModule {}
impl_empty_diagnosable_trait!(ContainerEntryModule);

impl Hash for ContainerEntryModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_mf/src/container/fallback_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rspack_core::{
CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions,
Module, ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType,
};
use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};

Expand Down Expand Up @@ -172,7 +172,7 @@ module.exports = loop();
}
}

impl Diagnosable for FallbackModule {}
impl_empty_diagnosable_trait!(FallbackModule);

impl Hash for FallbackModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_mf/src/container/remote_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rspack_core::{
CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions,
Module, ModuleIdentifier, ModuleType, RuntimeSpec, SourceType,
};
use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};

Expand Down Expand Up @@ -169,7 +169,7 @@ impl Module for RemoteModule {
}
}

impl Diagnosable for RemoteModule {}
impl_empty_diagnosable_trait!(RemoteModule);

impl Hash for RemoteModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_mf/src/sharing/consume_shared_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rspack_core::{
CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions,
Module, ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType,
};
use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};

Expand Down Expand Up @@ -217,7 +217,7 @@ impl Module for ConsumeSharedModule {
}
}

impl Diagnosable for ConsumeSharedModule {}
impl_empty_diagnosable_trait!(ConsumeSharedModule);

impl Hash for ConsumeSharedModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_mf/src/sharing/provide_shared_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rspack_core::{
CodeGenerationResult, Compilation, Context, DependenciesBlock, DependencyId, LibIdentOptions,
Module, ModuleIdentifier, ModuleType, RuntimeGlobals, RuntimeSpec, SourceType,
};
use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_hash::RspackHash;
use rspack_identifier::{Identifiable, Identifier};

Expand Down Expand Up @@ -182,7 +182,7 @@ impl Module for ProvideSharedModule {
}
}

impl Diagnosable for ProvideSharedModule {}
impl_empty_diagnosable_trait!(ProvideSharedModule);

impl Hash for ProvideSharedModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down
4 changes: 2 additions & 2 deletions crates/rspack_plugin_runtime/src/lazy_compilation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rspack_core::{
PluginNormalModuleFactoryCreateModuleHookOutput, RuntimeGlobals, RuntimeSpec, SourceType,
};
use rspack_core::{CodeGenerationResult, Context, ModuleIdentifier};
use rspack_error::{Diagnosable, Result};
use rspack_error::{impl_empty_diagnosable_trait, Result};
use rspack_identifier::Identifiable;

#[derive(Debug)]
Expand Down Expand Up @@ -91,7 +91,7 @@ impl Identifiable for LazyCompilationProxyModule {
}
}

impl Diagnosable for LazyCompilationProxyModule {}
impl_empty_diagnosable_trait!(LazyCompilationProxyModule);

impl Hash for LazyCompilationProxyModule {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
Expand Down

0 comments on commit a915742

Please sign in to comment.