paging-calculator 0.4.0

CLI utility that helps you to calculate indices into the page table from a virtual address. The tool knows multiple paging implementations, such as x86, x86 with PAE, x86_64 and x86_64 with a 5-level page table.
/*
MIT License

Copyright (c) 2024 Philipp Schuster

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/

//! CLI utility that helps you to calculate indices into the page table from a
//! virtual address. The tool knows multiple paging implementations, such as x86,
//! x86 with PAE, x86_64 and x86_64 with a 5-level page table.

#![deny(
    clippy::all,
    clippy::cargo,
    clippy::nursery,
    // clippy::restriction,
    // clippy::pedantic
)]
// now allow a few rules which are denied by the above statement
// --> they are ridiculous and not necessary
#![allow(
    clippy::suboptimal_flops,
    clippy::redundant_pub_crate,
    clippy::fallible_impl_from,
    clippy::multiple_crate_versions
)]
// allow: required because of derive_more::Display macro
#![allow(clippy::use_self)]
#![deny(missing_docs)]
#![deny(missing_debug_implementations)]
#![deny(rustdoc::all)]

mod addr_width;
mod cli;
mod page_table_index;
mod paging_info;
mod print;

use crate::cli::{CliArgs, ColorOption};
use crate::print::USE_ANSI;
use clap::Parser;
use std::sync::atomic::Ordering;

fn main() {
    // parse the CLI args. parse() is generated by clap.
    let cli: CliArgs = CliArgs::parse();

    configure_ansi_setting(cli.color.unwrap_or_default());

    print::print(&cli);
}

/// Sets the global variable [`USE_ANSI`] depending on the value of
/// [`ColorOption`].
fn configure_ansi_setting(cfg: ColorOption) {
    let use_ansi = match cfg {
        ColorOption::Never => false,
        ColorOption::Auto => ansi_auto_detection(),
        ColorOption::Always => true,
    };
    // according to nu_ansi_doc, this is required
    #[cfg(target_os = "windows")]
    if use_ansi {
        let _ = nu_ansi_term::enable_ansi_support();
    }
    USE_ANSI.store(use_ansi, Ordering::SeqCst);
}

/// Performs the auto-detection to see if stdout points to a TTY. If this is
/// the case, I expect that ANSI escape sequences are supported.
fn ansi_auto_detection() -> bool {
    atty::is(atty::Stream::Stdout)
}

#[cfg(test)]
mod tests {
    // use super::*;
}