//
// lib.rs
//
// Copyright (C) 2022 Posit Software, PBC. All rights reserved.
//
//

pub mod all;
pub mod any;
pub mod case;
pub mod cell;
pub mod env;
pub mod event;
pub mod join;
pub mod local;
pub mod ok;
pub mod panic;
pub mod push;
pub mod result;
pub mod sorted_vec;
pub mod spawn;
pub mod testing;
pub mod unwrap;

pub use crate::cell::DebugRefCell;
pub use crate::env::env_flag;
pub use crate::env::env_flag_opt;
pub use crate::env::is_ci;
pub use crate::join::Joined;
pub use crate::ok::Ok;
pub use crate::panic::panic_message;
pub use crate::push::Push;
pub use crate::sorted_vec::SortedVec;
pub use crate::spawn::DEFAULT_STACK_SIZE;
pub use crate::spawn::SMALL_STACK_SIZE;
pub use crate::spawn::TINY_STACK_SIZE;
pub use crate::testing::assert_testing;
pub use crate::testing::IS_TESTING;
pub use crate::unwrap::IntoOption;
pub use crate::unwrap::IntoResult;

/// Asserts that the given expression matches the given pattern
/// and optionally some further assertions.
///
/// To use until `assert_matches()` stabilises
///
/// # Examples
///
/// ```
/// #[macro_use] extern crate stdext;
/// # fn main() {
/// assert_match!(1 + 1, 2);
/// assert_match!(1 + 1, 2 => {
///    assert_eq!(40 + 2, 42)
/// });
/// # }
/// ```
#[macro_export]
macro_rules! assert_match {
    ($expression:expr, $pattern:pat_param => $code:block) => {
        match $expression {
            $pattern => $code,
            _ => panic!("Expected {}", stringify!($pattern)),
        }
    };

    ($expression:expr, $pattern:pat_param) => {
        #[allow(clippy::redundant_pattern_matching)]
        {
            assert!(matches!($expression, $pattern))
        }
    };
}

// Useful for debugging
pub fn log_trace() {
    log::error!("{}", std::backtrace::Backtrace::force_capture());
}
