Skip to content

Commit 1f88a25

Browse files
committed
Small cleanup of macro and prepare for release
The majority of the work has been done by @patskovn in #19, I'm just cleaning up and preparing a release, since he has used his branch to publish his own package, so I can't just merge it in unfortunately
1 parent 5cad42c commit 1f88a25

File tree

4 files changed

+31
-12
lines changed

4 files changed

+31
-12
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
## [0.2.6](https://github.com/MidasLamb/non-empty-string/compare/v0.2.5...v0.2.6) - 2025-04-09
11+
12+
### Added
13+
14+
- `non_empty_string!()` macro to create a `NonEmptyString` at compile time , thanks @patskovn in #19 (requires `macros` feature flag)
15+
1016
## [0.2.5](https://github.com/MidasLamb/non-empty-string/compare/v0.2.4...v0.2.5) - 2024-10-15
1117

1218
### Added

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ serde = { version = "1", features = ["derive"] }
2525

2626
[features]
2727
default = []
28-
macro = []
28+
macros = []
2929
serde = ["dep:serde"]
3030

3131

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use delegate::delegate;
1717
#[cfg(feature = "serde")]
1818
mod serde_support;
1919

20-
#[cfg(feature = "macro")]
20+
#[cfg(feature = "macros")]
2121
mod macros;
2222

2323
mod trait_impls;

src/macros.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
/// # Examples
88
///
99
/// ```
10-
/// use non_empty_string::{non_empty, NonEmptyString};
10+
/// use non_empty_string::{non_empty_string, NonEmptyString};
1111
///
12-
/// let s: NonEmptyString = non_empty!("Hello, Rust!");
12+
/// let s: NonEmptyString = non_empty_string!("Hello, Rust!");
1313
/// assert_eq!(s, NonEmptyString::new("Hello, Rust!".to_string()).unwrap());
1414
/// ```
1515
///
@@ -18,27 +18,40 @@
1818
/// If an empty string is provided, this macro will cause a **compile-time error**.
1919
///
2020
/// ```compile_fail
21-
/// use non_empty_string::non_empty;
21+
/// use non_empty_string::non_empty_string;
2222
///
23-
/// let s = non_empty!("");
23+
/// let s = non_empty_string!("");
2424
/// ```
25-
macro_rules! non_empty {
25+
macro_rules! non_empty_string {
2626
($s:expr) => {{
2727
// Compile-time assertion to ensure the string is non-empty
2828
const _: () = assert!(!$s.is_empty(), "String cannot be empty");
2929

3030
// Create a NonEmptyString, unsafely wrapping since we've checked it's valid
31-
unsafe { NonEmptyString::new_unchecked($s.to_string()) }
31+
unsafe { $crate::NonEmptyString::new_unchecked($s.to_string()) }
3232
}};
3333
}
3434

3535
#[cfg(test)]
3636
mod tests {
37-
use crate::NonEmptyString;
37+
// We explicitely DO NOT do `use crate::NonEmptyString` or anything of the sorts to ensure the macro has proper hygiene.
38+
// Otherwise tests might pass, but if a user does `non_empty_string::non_empty_string!("A")`, they might get compilation
39+
// errors that `NonEmptyString` is not in scope.
40+
41+
const NON_EMPTY_STRING: &'static str = "non-empty-string";
42+
43+
#[test]
44+
fn test_const_non_empty_string_macro_valid() {
45+
let s = non_empty_string!(NON_EMPTY_STRING);
46+
assert_eq!(
47+
s,
48+
crate::NonEmptyString::try_from(NON_EMPTY_STRING).unwrap()
49+
);
50+
}
3851

3952
#[test]
40-
fn test_non_empty_string_macro_valid() {
41-
let s = non_empty!("Test String");
42-
assert_eq!(s, NonEmptyString::try_from("Test String").unwrap());
53+
fn test_inline_non_empty_string_macro_valid() {
54+
let s = non_empty_string!("Test String");
55+
assert_eq!(s, crate::NonEmptyString::try_from("Test String").unwrap());
4356
}
4457
}

0 commit comments

Comments
 (0)