cargo-kconfig 0.0.4

Kconfig macro library and user interface for the Kconfig file format from the Linux Kernel
Documentation
use std::fmt::Display;

/*
 Cargo KConfig - KConfig parser
 Copyright (C) 2022  Sjoerd van Leent

--------------------------------------------------------------------------------

Copyright Notice: Apache

Licensed under the Apache License, Version 2.0 (the "License"); you may not use
this file except in compliance with the License. You may obtain a copy of the
License at

   https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed
under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.

--------------------------------------------------------------------------------

Copyright Notice: GPLv2

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.

--------------------------------------------------------------------------------

Copyright Notice: MIT

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.
*/

/// Specifies if help is requested, what kind of help is requested.
pub enum HelpType {
    /// Print a generic help text if no exact command is defined, or if an
    /// erroneous command is used.
    None,
    /// Print the help of the "set" command
    Set,
    /// Print the help of the "get" command
    Get,
    /// Print the help of the "list" or "ls" command
    List,
    /// Prints the help of the "info" command
    Info,
    /// Prints the help of the "update" command
    Update,
}

impl HelpType {
    fn print_help(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "πŸ‘‰ Usage: cargo kconfig <subcommand> {{...}}")?;
        writeln!(f, "          cargo kconfig <subcommand> --help")?;
        writeln!(f, "")?;
        writeln!(f, "Cargo Kconfig supports the following subcommands:")?;
        writeln!(f, "")?;
        writeln!(f, "config-set  Sets cargo-kconfig configuration values")?;
        writeln!(f, "config-get  Gets cargo-kconfig configuration values")?;
        writeln!(f, "ls | list   Shows the menu items for a given level")?;
        writeln!(f, "update      Updates a configuration item to a new value")?;
        Ok(())
    }

    fn print_config_set_help(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "πŸ‘‰ Usage: cargo kconfig config-set input <KCONFIG_PATH>")?;
        writeln!(
            f,
            "          cargo kconfig config-set output <DOTCONFIG_PATH>"
        )?;
        writeln!(f, "")?;
        writeln!(
            f,
            "KCONFIG_PATH   - The path of an (alternative) Kconfig configuration file to use"
        )?;
        writeln!(
            f,
            "DOTCONFIG_PATH - The path of an (alternative) .config output file to use"
        )?;
        Ok(())
    }

    fn print_config_get_help(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "πŸ‘‰ Usage: cargo kconfig config-get list")?;
        writeln!(f, "          cargo kconfig config-get input")?;
        writeln!(f, "          cargo kconfig config-get output")?;
        Ok(())
    }

    fn print_list_help(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "πŸ‘‰ Usage: cargo kconfig list {{...NAME}}")?;
        writeln!(f, "          cargo kconfig ls {{...NAME}}")?;
        writeln!(f, "")?;
        writeln!(f, "NAME    - The name of a configuration menu")?;
        Ok(())
    }

    fn print_info_help(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "πŸ‘‰ Usage: cargo kconfig info {{NAME}}")?;
        writeln!(f, "")?;
        writeln!(f, "NAME    - The name of a configuration item")?;
        Ok(())
    }

    fn print_update_help(f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        writeln!(f, "πŸ‘‰ Usage: cargo kconfig update {{NAME}}")?;
        writeln!(f, "")?;
        writeln!(f, "NAME    - The name of a configuration item")?;
        Ok(())
    }
}

impl Display for HelpType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HelpType::None => Self::print_help(f),
            HelpType::Set => Self::print_config_set_help(f),
            HelpType::Get => Self::print_config_get_help(f),
            HelpType::List => Self::print_list_help(f),
            HelpType::Info => Self::print_info_help(f),
            HelpType::Update => Self::print_update_help(f),
        }
    }
}