#![doc = include_str!("../README.md")]
#![doc(
html_favicon_url = "https://kura.pro/dtt/images/favicon.ico",
html_logo_url = "https://kura.pro/dtt/images/logos/dtt.svg",
html_root_url = "https://docs.rs/dtt"
)]
#![deny(
clippy::all,
clippy::pedantic,
clippy::cargo,
clippy::nursery,
rustdoc::broken_intra_doc_links,
missing_docs,
unsafe_code
)]
#![allow(clippy::module_name_repetitions)]
#![cfg_attr(docsrs, feature(doc_cfg))]
use std::env;
pub mod constants {
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
pub const TEST_MODE_ENV: &str = "DTT_TEST_MODE";
pub const TEST_MODE_ENABLED: &str = "1";
pub const WELCOME_MSG: &str = "Welcome to `DTT` 👋!";
pub const DESCRIPTION: &str = "A Rust library for parsing, validating,manipulating, and formatting dates and times.";
}
#[doc(inline)]
pub use crate::datetime::DateTime;
#[doc(inline)]
pub use crate::error::AppError;
pub mod datetime;
pub mod error;
pub mod macros;
pub mod prelude {
pub use crate::datetime::DateTime;
pub use crate::error::{AppError, DateTimeError};
}
pub fn run() -> Result<(), AppError> {
if is_test_mode() {
return Err(AppError::SimulatedError);
}
display_welcome_message();
Ok(())
}
fn is_test_mode() -> bool {
env::var(constants::TEST_MODE_ENV)
.map(|val| val == constants::TEST_MODE_ENABLED)
.unwrap_or(false)
}
fn display_welcome_message() {
println!("{}", constants::WELCOME_MSG);
println!("{}", constants::DESCRIPTION);
println!("Version: {}", constants::VERSION);
}
#[cfg(test)]
mod tests {
use super::*;
mod initialization {
use super::*;
#[test]
fn test_normal_run() {
env::remove_var(constants::TEST_MODE_ENV);
assert!(run().is_ok());
}
#[test]
fn test_simulated_error() {
env::set_var(
constants::TEST_MODE_ENV,
constants::TEST_MODE_ENABLED,
);
assert!(matches!(run(), Err(AppError::SimulatedError)));
}
}
mod configuration {
use super::*;
#[test]
#[allow(clippy::const_is_empty)]
fn test_version_constant() {
assert!(
!constants::VERSION.is_empty(),
"Version string should not be empty"
);
}
#[test]
fn test_is_test_mode() {
env::remove_var(constants::TEST_MODE_ENV);
let first_check = is_test_mode();
assert!(
!first_check,
"Should not be in test mode by default"
);
env::set_var(
constants::TEST_MODE_ENV,
constants::TEST_MODE_ENABLED,
);
let second_check = is_test_mode();
assert!(
second_check,
"Should be in test mode after enabling it"
);
}
}
}