Skip to content

Commit

Permalink
chore: Get rid of common::instrament, inline into main crate
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpovel committed Aug 27, 2023
1 parent c640363 commit 7916ed7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 5 deletions.
2 changes: 0 additions & 2 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
#![allow(clippy::cargo_common_metadata)]

pub mod instrament;
6 changes: 6 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ pub use crate::stages::Stage;
use log::{debug, info};
use std::io::{BufRead, Error, Write};

/// Internal macros. Have to live here to be usable in unit, not just integration
/// (`./tests`) tests. Do not [move from
/// here](https://stackoverflow.com/questions/26731243/how-do-i-use-a-macro-across-module-files#comment115383139_63234531)
#[macro_use]
pub mod macros;

/// Main components around [`Stage`]s and their [processing][Stage::substitute].
pub mod stages;

Expand Down
24 changes: 23 additions & 1 deletion common/src/instrament.rs → core/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
/// Sanitize a string for use as a filename.
///
/// A 'best effort' in replacing characters illegal in filenames with a stand-in
/// [`char`]. For Unix, it's easy, for Windows, it's a bit of guessing. Use with
/// caution.
#[must_use]
pub fn sanitize_for_filename_use(filename: &str) -> String {
const REPLACEMENT: char = '_';
filename
Expand All @@ -14,6 +20,22 @@ pub fn sanitize_for_filename_use(filename: &str) -> String {
.join(&REPLACEMENT.to_string())
}

/// This macro allows `rstest` (test case parametrization) and `insta` (snapshot
/// testing) to coexist.
///
/// The gist is that it generates a whole new `struct` from a function signature.
/// Implementing [`std::fmt::Display`] for it, the struct can be used to represent a
/// test case automatically: whatever was passed into the test function by `rstest` and
/// its `case`s will form the `struct` fields, which form the name. As a result, all
/// tests get unique, easily identified names.
///
/// One caveat is that a closure is now required to run a test case. Quite ugly. In
/// general, **do not use this macro** if you can avoid it. Consider this macro as
/// potentially breaking at any point. When using *either* `rstest` *or* `insta`, this
/// macro is not needed.
///
/// For context, see [this
/// issue](https://github.com/la10736/rstest/issues/183#issuecomment-1564021215).
#[macro_export]
macro_rules! instrament {
($(#[$attr:meta])* fn $name:ident ( $( $(#[$arg_attr:meta])* $arg:ident : $type:ty),* ) $body:expr ) => {
Expand All @@ -32,7 +54,7 @@ macro_rules! instrament {
)*
];

let name = $crate::instrament::sanitize_for_filename_use(&items.join("-"));
let name = $crate::macros::sanitize_for_filename_use(&items.join("-"));
write!(f, "{}", name)
}
}
Expand Down
1 change: 0 additions & 1 deletion core/src/stages/german/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ fn is_valid(word: &str, predicate: &impl Fn(&str) -> bool) -> bool {
#[cfg(test)]
mod tests {
use super::*;
use common::instrament;
use rstest::rstest;

#[test]
Expand Down
1 change: 0 additions & 1 deletion core/src/stages/german/words.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ impl Replace for String {
#[cfg(test)]
mod tests {
use super::*;
use common::instrament;
use rstest::rstest;
use serde::Serialize;

Expand Down

0 comments on commit 7916ed7

Please sign in to comment.