1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
//! Data processing utilities for parsing, encoding, and serialization.
//!
//! This module provides functions for parsing various data formats,
//! encoding/decoding operations, and JSON manipulation utilities.
//!
//! ## Features
//!
//! - **Configuration Parsing**: Key-value pair parsing from config files
//! - **Data Validation**: Structure and format validation
//! - **Efficient Parsing**: High-performance parsing using optimized algorithms
//! - **Memory Efficient**: Low-overhead data processing
//! - **Thread Safe**: All operations are safe to use across threads
//!
//! ## Examples
//!
//! ### Configuration File Parsing
//! ```rust
//! use trash_utilities::data::parse_key_value;
//! use ahash::AHashMap;
//!
//! // Parse a simple config file format
//! let config_content = r#"
//! app.name=MyApp
//! app.version=1.0.0
//! database.host=localhost
//! database.port=5432
//! features.logging=true
//! features.metrics=false
//! "#;
//!
//! let config = parse_key_value(config_content);
//!
//! assert_eq!(config.get("app.name"), Some(&"MyApp".to_string()));
//! assert_eq!(config.get("database.port"), Some(&"5432".to_string()));
//! assert_eq!(config.get("features.logging"), Some(&"true".to_string()));
//! ```
//!
//! ### Environment Variable Style Parsing
//! ```rust
//! use trash_utilities::data::parse_key_value;
//!
//! // Parse environment variable style content
//! let env_content = "
//! HOME=/home/user
//! PATH=/usr/bin:/bin
//! USER=testuser
//! SHELL=/bin/bash
//! ";
//!
//! let env_vars = parse_key_value(env_content);
//! assert_eq!(env_vars.get("USER"), Some(&"testuser".to_string()));
//! assert_eq!(env_vars.get("SHELL"), Some(&"/bin/bash".to_string()));
//! ```
//!
//! ### Docker Compose Style Parsing
//! ```rust
//! use trash_utilities::data::parse_key_value;
//!
//! // Parse Docker environment file format
//! let docker_env = "
//! POSTGRES_DB=myapp
//! POSTGRES_USER=admin
//! POSTGRES_PASSWORD=secret123
//! DATABASE_URL=postgresql://admin:secret123@localhost:5432/myapp
//! ";
//!
//! let db_config = parse_key_value(docker_env);
//!
//! // Use the parsed configuration
//! let db_url = db_config.get("DATABASE_URL").unwrap();
//! println!("Connecting to: {}", db_url);
//! ```
//!
//! ### Configuration Validation
//! ```rust
//! use trash_utilities::data::parse_key_value;
//!
//! fn validate_config(content: &str) -> Result<(), String> {
//! let config = parse_key_value(content);
//!
//! // Check required fields
//! let required_fields = ["app.name", "database.host", "database.port"];
//! for field in &required_fields {
//! if !config.contains_key(*field) {
//! return Err(format!("Missing required field: {}", field));
//! }
//! }
//!
//! // Validate port is numeric
//! if let Some(port_str) = config.get("database.port") {
//! if port_str.parse::<u16>().is_err() {
//! return Err("database.port must be a valid port number".to_string());
//! }
//! }
//!
//! Ok(())
//! }
//!
//! let valid_config = "
//! app.name=MyApp
//! database.host=localhost
//! database.port=5432
//! ";
//!
//! assert!(validate_config(valid_config).is_ok());
//!
//! let invalid_config = "
//! app.name=MyApp
//! database.host=localhost
//! ";
//!
//! assert!(validate_config(invalid_config).is_err());
//! ```
// Standard library imports
// (none)
// External crate imports
use AHashMap;
use memchr;
/// Parses key-value pairs from a string content, typically from config files.
///
/// This function assumes each line is in the format "key=value". It uses `memchr`
/// for efficient byte-level searching of the '=' character. Lines without '=' are ignored.
/// Leading and trailing whitespace is trimmed from both keys and values.
///
/// # Parameters
/// - `content`: The string content to parse. Can contain multiple lines.
///
/// # Returns
/// An `AHashMap<String, String>` containing the parsed key-value pairs.
/// Keys and values are trimmed of whitespace. Lines without '=' are silently ignored.
///
/// # Performance
/// Uses `memchr` for O(n) byte-level searching, making it very fast for large config files.
///
/// # Examples
/// ```rust
/// use trash_analyzer::data::parse_key_value;
/// use ahash::AHashMap;
///
/// let content = "name=John\nage=30\ncity=New York";
/// let map = parse_key_value(content);
/// assert_eq!(map.get("name"), Some(&"John".to_string()));
/// assert_eq!(map.get("age"), Some(&"30".to_string()));
/// assert_eq!(map.get("city"), Some(&"New York".to_string()));
/// ```
///
/// # Format
/// The expected format is simple: `key=value` per line. Empty lines and lines without
/// '=' are ignored. Keys and values are trimmed of whitespace.
///
/// ```text
/// # This is a comment (ignored)
/// name=John Doe
/// age=30
/// city=New York
/// ```