Skip to content

Compile time macro #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.2.6](https://github.com/MidasLamb/non-empty-string/compare/v0.2.5...v0.2.6) - 2025-04-09

### Added

- `non_empty_string!()` macro to create a `NonEmptyString` at compile time , thanks @patskovn in #19 (requires `macros` feature flag)

## [0.2.5](https://github.com/MidasLamb/non-empty-string/compare/v0.2.4...v0.2.5) - 2024-10-15

### Added
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ serde = { version = "1", features = ["derive"] }

[features]
default = []
macros = []
serde = ["dep:serde"]


Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ use delegate::delegate;
#[cfg(feature = "serde")]
mod serde_support;

#[cfg(feature = "macros")]
mod macros;

mod trait_impls;

/// A simple String wrapper type, similar to NonZeroUsize and friends.
Expand Down
57 changes: 57 additions & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#[macro_export]
/// Creates a `NonEmptyString` from a string literal at compile time.
///
/// This macro ensures that the provided string is **not empty** at compile time,
/// preventing runtime errors due to empty strings.
///
/// # Examples
///
/// ```
/// use non_empty_string::{non_empty_string, NonEmptyString};
///
/// let s: NonEmptyString = non_empty_string!("Hello, Rust!");
/// assert_eq!(s, NonEmptyString::new("Hello, Rust!".to_string()).unwrap());
/// ```
///
/// # Compile-time Failure
///
/// If an empty string is provided, this macro will cause a **compile-time error**.
///
/// ```compile_fail
/// use non_empty_string::non_empty_string;
///
/// let s = non_empty_string!("");
/// ```
macro_rules! non_empty_string {
($s:expr) => {{
// Compile-time assertion to ensure the string is non-empty
const _: () = assert!(!$s.is_empty(), "String cannot be empty");

// Create a NonEmptyString, unsafely wrapping since we've checked it's valid
unsafe { $crate::NonEmptyString::new_unchecked($s.to_string()) }
}};
}

#[cfg(test)]
mod tests {
// We explicitely DO NOT do `use crate::NonEmptyString` or anything of the sorts to ensure the macro has proper hygiene.
// Otherwise tests might pass, but if a user does `non_empty_string::non_empty_string!("A")`, they might get compilation
// errors that `NonEmptyString` is not in scope.

const NON_EMPTY_STRING: &'static str = "non-empty-string";

#[test]
fn test_const_non_empty_string_macro_valid() {
let s = non_empty_string!(NON_EMPTY_STRING);
assert_eq!(
s,
crate::NonEmptyString::try_from(NON_EMPTY_STRING).unwrap()
);
}

#[test]
fn test_inline_non_empty_string_macro_valid() {
let s = non_empty_string!("Test String");
assert_eq!(s, crate::NonEmptyString::try_from("Test String").unwrap());
}
}
Loading