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 crate::{error::TrieInsert, key_repr::Key};
use pretty_assertions::assert_eq;

use super::Trie as GenericTrie;

type Trie<V> = GenericTrie<Key, V>;

fn i(str: &str) -> Vec<Key> {
    Key::parse_multiple(str).unwrap()
}

#[test]
fn test_insert() {
    let mut trie: Trie<bool> = Trie::new();

    trie.insert(&i("abc"), true).unwrap();
    trie.insert(&i("abd"), true).unwrap();
    trie.insert(&i("aca"), false).unwrap();

    let (_, key) = trie.try_get(&i("abfff"));
    assert_eq!(key, i("ab").clone());
}

#[test]
fn test_duplicate_insert() {
    let mut trie: Trie<bool> = Trie::new();

    trie.insert(&i("abc"), true).unwrap();
    trie.insert(&i("aca"), true).unwrap();
    let output = trie.insert(&i("ab"), false).unwrap_err();

    if let TrieInsert::KeyAlreadySet(key) = output {
        assert_eq!(key, i("ab").clone(),);
    } else {
        unreachable!("")
    }
}

#[test]
fn test_wrong_get() {
    let mut trie: Trie<bool> = Trie::new();

    trie.insert(&i("abc"), true).unwrap();
    trie.insert(&i("abd"), true).unwrap();
    trie.insert(&i("aca"), false).unwrap();

    assert!(trie.get(&i("bb")).is_none());
}