22# FYI: Build
33*/
44
5- #![ allow( unused_mut, reason = "It is conditionally used." ) ]
6-
7- use fyi_ansi:: ansi;
85use std:: {
96 fmt,
107 fs:: File ,
@@ -14,35 +11,6 @@ use std::{
1411
1512
1613
17- #[ cfg( feature = "bin_kinds" ) ]
18- /// # Total Kinds.
19- const NUM_KINDS : usize = 17 ;
20-
21- #[ cfg( not( feature = "bin_kinds" ) ) ]
22- /// # Total Kinds.
23- const NUM_KINDS : usize = 15 ;
24-
25- /// # Message Kinds.
26- static KINDS : [ ( & str , & str ) ; NUM_KINDS ] = [
27- ( "None" , "" ) ,
28- ( "Aborted" , concat ! ( ansi!( ( bold, light_red) "Aborted:" ) , " " ) ) ,
29- ( "Confirm" , concat ! ( ansi!( ( bold, dark_orange) "Confirm:" ) , " " ) ) ,
30- ( "Crunched" , concat ! ( ansi!( ( bold, light_green) "Crunched:" ) , " " ) ) ,
31- ( "Debug" , concat ! ( ansi!( ( bold, light_cyan) "Debug:" ) , " " ) ) ,
32- ( "Done" , concat ! ( ansi!( ( bold, light_green) "Done:" ) , " " ) ) ,
33- ( "Error" , concat ! ( ansi!( ( bold, light_red) "Error:" ) , " " ) ) ,
34- ( "Found" , concat ! ( ansi!( ( bold, light_green) "Found:" ) , " " ) ) ,
35- ( "Info" , concat ! ( ansi!( ( bold, light_magenta) "Info:" ) , " " ) ) ,
36- ( "Notice" , concat ! ( ansi!( ( bold, light_magenta) "Notice:" ) , " " ) ) ,
37- ( "Review" , concat ! ( ansi!( ( bold, light_cyan) "Review:" ) , " " ) ) ,
38- ( "Skipped" , concat ! ( ansi!( ( bold, light_yellow) "Skipped:" ) , " " ) ) ,
39- ( "Success" , concat ! ( ansi!( ( bold, light_green) "Success:" ) , " " ) ) ,
40- ( "Task" , concat ! ( ansi!( ( bold, 199 ) "Task:" ) , " " ) ) ,
41- ( "Warning" , concat ! ( ansi!( ( bold, light_yellow) "Warning:" ) , " " ) ) ,
42- #[ cfg( feature = "bin_kinds" ) ] ( "Blank" , "" ) ,
43- #[ cfg( feature = "bin_kinds" ) ] ( "Custom" , "" ) ,
44- ] ;
45-
4614// COLORS: [(name, hex)].
4715include ! ( "skel/ansi256.rs" ) ;
4816
@@ -52,7 +20,6 @@ include!("skel/ansi256.rs");
5220fn main ( ) {
5321 println ! ( "cargo:rerun-if-env-changed=CARGO_PKG_VERSION" ) ;
5422 build_ansi_color ( ) ;
55- build_msg_kinds ( ) ;
5623}
5724
5825/// # Build ANSI Color Enum.
@@ -190,160 +157,6 @@ impl AnsiColor {\n");
190157 . expect ( "Unable to save ansi-color.rs" ) ;
191158}
192159
193- /// # Build/Save `MsgKind`.
194- ///
195- /// The `MsgKind` enum doesn't feel all that complicated, but there are a lot
196- /// of little ways inconsistencies can creep in. Building the trickier pieces
197- /// programmatically helps ensure nothing is missed.
198- ///
199- /// This generates code for the definition, an `ALL` constant,
200- /// `MsgKind::as_str_prefix`, and the `Msg::kind` helpers.
201- fn build_msg_kinds ( ) {
202- use std:: fmt:: Write ;
203-
204- /// # Hidden Kinds.
205- const HIDDEN : [ & str ; 2 ] = [ "Blank" , "Custom" ] ;
206-
207- let mut out = String :: with_capacity ( 8192 ) ;
208- out. push_str ( r#"#[expect(missing_docs, reason = "Redudant.")]
209- #[derive(Debug, Copy, Clone, Eq, Hash, PartialEq)]
210- /// # Message Kind.
211- ///
212- /// This enum contains built-in prefixes for [`Msg`](crate::Msg). These are
213- /// generally only used to initiate a new message with this prefix, like:
214- ///
215- /// ## Examples
216- ///
217- /// ```
218- /// use fyi_msg::{Msg, MsgKind};
219- ///
220- /// // Error: Oh no!
221- /// assert_eq!(
222- /// Msg::new(MsgKind::Error, "Oh no!"),
223- /// MsgKind::Error.into_msg("Oh no!"),
224- /// );
225- /// ```
226- ///
227- /// Most kinds have their own dedicated [`Msg`] helper method which, unlike the
228- /// previous examples, comes with a line break at the end.
229- ///
230- /// ```
231- /// use fyi_msg::{Msg, MsgKind};
232- ///
233- /// // Error: Oh no!\n
234- /// assert_eq!(
235- /// Msg::error("Oh no!"),
236- /// Msg::new(MsgKind::Error, "Oh no!").with_newline(true),
237- /// );
238- /// ```
239- pub enum MsgKind {"# ) ;
240- for ( kind, _) in KINDS {
241- if kind != "None" { out. push ( '\n' ) ; }
242- if HIDDEN . contains ( & kind) { out. push_str ( "\t #[doc(hidden)]\n " ) ; }
243- writeln ! ( & mut out, "\t {kind}," ) . unwrap ( ) ;
244- }
245- out. push_str ( "}\n " ) ;
246-
247- // Add a constant containing all kinds to make iteration easier.
248- writeln ! (
249- & mut out,
250- "impl MsgKind {{
251- /// # All Variants.
252- ///
253- /// This array can be used to cheaply iterate through all message kinds.
254- pub const ALL: [Self; {NUM_KINDS}] = ["
255- ) . unwrap ( ) ;
256- for chunk in KINDS . chunks ( 8 ) {
257- out. push_str ( "\t \t " ) ;
258- for ( kind, _) in chunk { write ! ( & mut out, "Self::{kind}, " ) . unwrap ( ) ; }
259- out. push ( '\n' ) ;
260- }
261- out. push_str ( "\t ];\n " ) ;
262-
263- // And a crate-wide method to expose the preformatted prefix string.
264- #[ cfg( feature = "bin_kinds" ) ] let wild = "_" ;
265- #[ cfg( not( feature = "bin_kinds" ) ) ] let wild = "Self::None" ;
266-
267- out. push_str ( "\t #[inline]
268- #[must_use]
269- /// # As String Slice (Prefix).
270- ///
271- /// Return the kind as a string slice, formatted and with a trailing `\" : \" `,
272- /// same as [`Msg`] uses for prefixes.
273- pub(crate) const fn as_str_prefix(self) -> &'static str {
274- match self {\n " ) ;
275- for ( kind, prefix) in KINDS {
276- // Skip empties.
277- if prefix. is_empty ( ) { continue ; }
278-
279- // While we're here, check the predictable parts were typed correctly.
280- assert ! (
281- prefix. starts_with( "\x1b [1;" ) &&
282- prefix. ends_with( & format!( "m{kind}:\x1b [0m " ) ) ,
283- "BUG: {kind}::as_str_prefix is wrong!" ,
284- ) ;
285-
286- writeln ! ( & mut out, "\t \t \t Self::{kind} => {prefix:?}," ) . unwrap ( ) ;
287- }
288- writeln ! (
289- & mut out,
290- "\t \t \t {wild} => \" \" ,
291- }}
292- }}
293- }}" ) . unwrap ( ) ;
294-
295- // Generate helper methods for (most) of the kinds. (Might as well do this
296- // here.)
297- out. push_str ( "/// ## [`MsgKind`] One-Shots.
298- impl Msg {\n " ) ;
299- for ( kind, prefix) in KINDS {
300- // Skip the empties and "Confirm" (since it has a macro).
301- if prefix. is_empty ( ) || kind == "Confirm" { continue ; }
302-
303- let prefix_len = prefix. len ( ) ;
304- writeln ! (
305- & mut out,
306- "\t #[must_use]
307- /// # New {kind}.
308- ///
309- /// Create a new [`Msg`] with a built-in [`MsgKind::{kind}`] prefix _and_ trailing line break.
310- ///
311- /// ## Examples.
312- ///
313- /// ```
314- /// use fyi_msg::{{Msg, MsgKind}};
315- ///
316- /// assert_eq!(
317- /// Msg::{kind_low}(\" Hello World\" ),
318- /// Msg::new(MsgKind::{kind}, \" Hello World\" ).with_newline(true),
319- /// );
320- /// ```
321- pub fn {kind_low}<S: AsRef<str>>(msg: S) -> Self {{
322- // Glue it all together.
323- let msg = msg.as_ref();
324- let m_end = {prefix_len} + msg.len();
325- let mut inner = String::with_capacity(m_end + 1);
326- inner.push_str({prefix:?});
327- inner.push_str(msg);
328- inner.push('\\ n');
329-
330- // Done!
331- Self {{
332- inner,
333- toc: super::toc!({prefix_len}, m_end, true),
334- }}
335- }}" ,
336- kind=kind,
337- kind_low=kind. to_ascii_lowercase( ) ,
338- ) . unwrap ( ) ;
339- }
340- out. push_str ( "}\n " ) ;
341-
342- File :: create ( out_path ( "msg-kinds.rs" ) )
343- . and_then ( |mut f| f. write_all ( out. as_bytes ( ) ) . and_then ( |( ) | f. flush ( ) ) )
344- . expect ( "Unable to save msg-kinds.rs" ) ;
345- }
346-
347160/// # Output Path.
348161///
349162/// Append the sub-path to OUT_DIR and return it.
0 commit comments