| 964 | |
| 965 | #[test] |
| 966 | fn test_realistic_rust_module() { |
| 967 | let mut parser = RustParser::new(); |
| 968 | let source = r#" |
| 969 | //! Repository module for data access. |
| 970 | |
| 971 | use std::collections::HashMap; |
| 972 | use thiserror::Error; |
| 973 | |
| 974 | /// Error type for repository operations. |
| 975 | #[derive(Debug, Error)] |
| 976 | pub enum RepoError { |
| 977 | #[error("Not found: {0}")] |
| 978 | NotFound(String), |
| 979 | #[error("Duplicate key: {0}")] |
| 980 | DuplicateKey(String), |
| 981 | } |
| 982 | |
| 983 | /// A generic in-memory repository. |
| 984 | pub struct Repository<T: Clone> { |
| 985 | data: HashMap<String, T>, |
| 986 | } |
| 987 | |
| 988 | impl<T: Clone> Repository<T> { |
| 989 | /// Create a new empty repository. |
| 990 | pub fn new() -> Self { |
| 991 | Self { |
| 992 | data: HashMap::new(), |
| 993 | } |
| 994 | } |
| 995 | |
| 996 | /// Find an item by key. |
| 997 | pub fn find(&self, key: &str) -> Result<&T, RepoError> { |
| 998 | self.data.get(key).ok_or_else(|| RepoError::NotFound(key.to_string())) |
| 999 | } |
| 1000 | |
| 1001 | /// Insert an item. |
| 1002 | pub fn insert(&mut self, key: String, value: T) -> Result<(), RepoError> { |
| 1003 | if self.data.contains_key(&key) { |
| 1004 | return Err(RepoError::DuplicateKey(key)); |
| 1005 | } |
| 1006 | self.data.insert(key, value); |
| 1007 | Ok(()) |
| 1008 | } |
| 1009 | |
| 1010 | /// Get the count of items. |
| 1011 | pub fn count(&self) -> usize { |
| 1012 | self.data.len() |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | pub type Result<T> = std::result::Result<T, RepoError>; |
| 1017 | |
| 1018 | pub const MAX_ITEMS: usize = 10_000; |
| 1019 | |
| 1020 | #[cfg(test)] |
| 1021 | mod tests { |
| 1022 | use super::*; |
| 1023 | |