Skip to content

Commit

Permalink
feat: Add compile_fail doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
kdheepak committed May 13, 2024
1 parent c2fcc4f commit 41a93c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
38 changes: 37 additions & 1 deletion src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,42 @@
/// let span = span!(style; "test {:04}", 123);
/// ```
///
/// # Note
///
/// The first parameter must be a formatting specifier followed by a comma OR a [`Style`] followed by a semicolon.
///
/// For example, the following will fail to compile:
///
/// ```compile_fail
/// # use ratatui::prelude::*;
/// use ratatui_macros::span;
/// let span = span!(Modifier::BOLD, "hello world");
/// ```
///
/// But this will work:
///
/// ```rust
/// # use ratatui::prelude::*;
/// use ratatui_macros::span;
/// let span = span!(Modifier::BOLD; "hello world");
/// ```
///
/// The following will fail to compile:
///
/// ```compile_fail
/// # use ratatui::prelude::*;
/// use ratatui_macros::span;
/// let span = span!("hello", "world");
/// ```
///
/// But this will work:
///
/// ```rust
/// # use ratatui::prelude::*;
/// use ratatui_macros::span;
/// let span = span!("hello {}", "world");
/// ```
///
/// [`Color`]: crate::style::Color
/// [`Style`]: crate::style::Style
/// [`Span`]: crate::text::Span
Expand All @@ -53,7 +89,7 @@ macro_rules! span {
ratatui::text::Span::raw(format!($string, $($arg)*))
};
($style:expr, $($arg:tt)*) => {
compile_error!("first parameter must be a formatting specifier followed by a comma OR a `Style` followed by a semi-colon")
compile_error!("first parameter must be a formatting specifier followed by a comma OR a `Style` followed by a semicolon")
};
($style:expr; $($arg:tt)*) => {
ratatui::text::Span::styled(format!($($arg)*), $style)
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/fails.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ note: while trying to match `%`
| (== $token:tt %) => {
| ^

error: first parameter must be a formatting specifier followed by a comma OR a `Style` followed by a semi-colon
error: first parameter must be a formatting specifier followed by a comma OR a `Style` followed by a semicolon
--> tests/ui/fails.rs:17:13
|
17 | let _ = span!(Modifier::BOLD, "hello world");
Expand Down

0 comments on commit 41a93c3

Please sign in to comment.