basic_usage/
basic_usage.rs

1//! Basic usage example of marisa-ffi
2//!
3//! This example demonstrates the core functionality of the Marisa trie:
4//! - Building a trie from a set of keys
5//! - Exact lookup
6//! - Predictive search
7//! - Reverse lookup
8//! - Common prefix search
9
10use marisa_ffi::{version, Agent, Keyset, Trie};
11
12fn main() -> Result<(), Box<dyn std::error::Error>> {
13    println!("Marisa FFI Example");
14    println!("Version: {}", version());
15    println!();
16
17    // Create a keyset and add some sample keys
18    let mut keyset = Keyset::new()?;
19
20    let sample_keys = vec![
21        "apple",
22        "application",
23        "apply",
24        "app",
25        "banana",
26        "band",
27        "bandit",
28        "cherry",
29        "cheese",
30        "chef",
31        "dog",
32        "doggy",
33        "doggie",
34        "elephant",
35        "eleven",
36        "elevator",
37    ];
38
39    println!("Adding {} keys to keyset...", sample_keys.len());
40    for key in &sample_keys {
41        keyset.push(key)?;
42    }
43
44    // Build the trie
45    println!("Building trie...");
46    let trie = Trie::build(&keyset)?;
47    println!("Trie built successfully!");
48    println!();
49
50    // Test exact lookup
51    println!("=== Exact Lookup Tests ===");
52    let test_keys = vec!["apple", "banana", "nonexistent", "app"];
53    for key in test_keys {
54        match trie.lookup(key) {
55            Some(id) => println!("Found '{}' with ID: {}", key, id),
56            None => println!("'{}' not found", key),
57        }
58    }
59    println!();
60
61    // Test predictive search
62    println!("=== Predictive Search Tests ===");
63    let prefixes = vec!["app", "ban", "che", "dog", "ele"];
64
65    for prefix in prefixes {
66        println!("Keys starting with '{}':", prefix);
67        let mut agent = Agent::new()?;
68
69        if trie.predictive_search(prefix, &mut agent)? {
70            // Note: agent.next() is not implemented yet
71            // For now, we'll show only the first result
72            let key = agent.key()?;
73            let id = agent.id();
74            println!("  - {} (ID: {})", key, id);
75            println!("  Total: 1 key (iteration not implemented)");
76        } else {
77            println!("  No keys found");
78        }
79        println!();
80    }
81
82    // Test reverse lookup
83    println!("=== Reverse Lookup Tests ===");
84    let test_ids = vec![0, 1, 2, 3, 4];
85    let mut agent = Agent::new()?;
86
87    for id in test_ids {
88        match trie.reverse_lookup(id, &mut agent) {
89            Ok(_) => {
90                let key = agent.key()?;
91                println!("ID {} -> '{}'", id, key);
92            }
93            Err(e) => println!("ID {} -> Error: {}", id, e),
94        }
95    }
96    println!();
97
98    // Test common prefix search
99    println!("=== Common Prefix Search Tests ===");
100    let test_strings = vec!["application", "bandit", "chef", "doggie"];
101
102    for test_str in test_strings {
103        println!("Common prefixes of '{}':", test_str);
104        let mut agent = Agent::new()?;
105
106        if trie.common_prefix_search(test_str, &mut agent)? {
107            // Note: agent.next() is not implemented yet
108            // For now, we'll show only the first result
109            let key = agent.key()?;
110            let id = agent.id();
111            println!("  - {} (ID: {})", key, id);
112            println!("  Total: 1 prefix (iteration not implemented)");
113        } else {
114            println!("  No prefixes found");
115        }
116        println!();
117    }
118
119    // Save and load example
120    println!("=== Save/Load Test ===");
121    let filename = "test_trie.marisa";
122
123    // Save the trie
124    trie.save(filename)?;
125    println!("Trie saved to '{}'", filename);
126
127    // Load it back
128    let mut loaded_trie = Trie::new()?;
129    loaded_trie.load(filename)?;
130    println!("Trie loaded from '{}'", filename);
131
132    // Verify it works
133    match loaded_trie.lookup("apple") {
134        Some(id) => println!("Loaded trie lookup 'apple' -> ID: {}", id),
135        None => println!("Loaded trie lookup 'apple' -> Not found"),
136    }
137
138    // Clean up
139    std::fs::remove_file(filename)?;
140    println!("Test file '{}' removed", filename);
141
142    Ok(())
143}