keymaps 1.2.0

A rust crate which provides a standardized encoding for key codes
Documentation
// keymaps - A rust crate which provides a standardized encoding for key codes
//
// Copyright (C) 2025 Benedikt Peetz <benedikt.peetz@b-peetz.de>
// SPDX-License-Identifier: LGPL-3.0-or-later
//
// This file is part of the keymaps crate.
//
// You should have received a copy of the License along with this program.
// If not, see <https://www.gnu.org/licenses/lgpl-3.0.txt>.

use std::str::FromStr;

use crate::error;

use super::{Key, Keys};

pub(crate) mod parser;

impl FromStr for Key {
    type Err = error::KeyParse;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parser = parser::Parser::new(s);
        let first = parser.next_key()?;

        if parser.is_empty() {
            Ok(first)
        } else {
            Err(error::KeyParse::TooManyKeys(s.to_owned()))
        }
    }
}

impl Key {
    /// Parse multiple keys from a string.
    /// The string should be a concatenation of [`Key::to_string_repr`].
    ///
    /// This is comparable to [`Keys::from_str`], but avoids the construction of a [`Keys`] wrapper
    /// struct.
    ///
    /// # Errors
    /// This will return the underlying key parse error when parsing on of the keys fails.
    pub fn parse_multiple(input: &str) -> Result<Vec<Self>, <Self as FromStr>::Err> {
        let mut parser = parser::Parser::new(input);
        let mut output = vec![];

        while !parser.is_empty() {
            output.push(parser.next_key()?);
        }

        Ok(output)
    }
}

impl FromStr for Keys {
    type Err = error::KeyParse;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let mut parser = parser::Parser::new(s);
        let mut output = vec![];

        while !parser.is_empty() {
            output.push(parser.next_key()?);
        }

        Ok(Keys(output))
    }
}