#![warn(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc)]

use std::error::Error;

#[cfg(feature = "mimalloc")]
use mimalloc::MiMalloc;

#[cfg(feature = "mimalloc")]
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;

#[cfg(feature = "jemalloc")]
#[cfg(not(target_env = "msvc"))]
use tikv_jemallocator::Jemalloc;

#[cfg(feature = "jemalloc")]
#[cfg(not(target_env = "msvc"))]
#[global_allocator]
static GLOBAL: Jemalloc = Jemalloc;

#[allow(clippy::too_many_lines)]
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    #[cfg(feature = "docs")]
    {
        let markdown: String = clap_markdown::help_markdown::<ordinaryd::Cli>();
        std::fs::write(
            std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("DOCS.md"),
            markdown,
        )?;
    }

    #[cfg(not(feature = "docs"))]
    {
        use clap::Parser;
        use ordinaryd::{run, setup};

        let cli = ordinaryd::Cli::parse();

        let logs_dir = setup(&cli)?;
        run(&cli, &logs_dir.into_boxed_path()).await?;
    }

    Ok(())
}
