Macro serde_yml::macro_check_directory

source ·
macro_rules! macro_check_directory {
    ($_dir:expr, $_name:expr) => { ... };
}
Expand description

§macro_check_directory Macro

Check if a directory exists and create it if necessary.

§Usage

use serde_yml::macro_check_directory;
use std::path::Path;
use tempfile::tempdir;

let temp_dir = tempdir().unwrap();
let path = temp_dir.path().join("logs");
macro_check_directory!(&path, "logs");

§Arguments

  • _dir - The path to check, as a std::path::Path.
  • _name - A string literal representing the directory name. This is used in error messages.

§Behaviour

The macro_check_directory macro checks if the directory specified by _dir exists. If it exists and is not a directory, a panic with an error message is triggered. If the directory doesn’t exist, the macro attempts to create it using std::fs::create_dir_all(_dir). If the creation is successful, no action is taken. If an error occurs during the directory creation, a panic is triggered with an error message indicating the failure.

Please note that the macro panics on failure. Consider using this macro in scenarios where panicking is an acceptable behaviour, such as during application startup or setup.