Parse a keyword node and optionally insert it into a map. If `target_map` is provided, the keyword will be inserted into that map with repeatability handling. If `target_map` is None, returns the key-value pair without inserting.
(
keyword_node: tree_sitter::Node,
input: &str,
input_bytes: &[u8],
target_map: Option<&mut Map<String, Value>>
)
| 149 | /// If `target_map` is provided, the keyword will be inserted into that map with repeatability handling. |
| 150 | /// If `target_map` is None, returns the key-value pair without inserting. |
| 151 | fn parse_and_insert_keyword( |
| 152 | keyword_node: tree_sitter::Node, |
| 153 | input: &str, |
| 154 | input_bytes: &[u8], |
| 155 | target_map: Option<&mut Map<String, Value>> |
| 156 | ) -> Result<(String, Value), SshdConfigError> { |
| 157 | let mut cursor = keyword_node.walk(); |
| 158 | let mut key = None; |
| 159 | let mut value = Value::Null; |
| 160 | let mut keyword_info: Option<KeywordInfo> = None; |
| 161 | let mut operator: Option<String> = None; |
| 162 | |
| 163 | if let Some(keyword) = keyword_node.child_by_field_name("keyword") { |
| 164 | let Ok(text) = keyword.utf8_text(input_bytes) else { |
| 165 | return Err(SshdConfigError::ParserError(t!("parser.failedToParseNode", input = input).to_string())); |
| 166 | }; |
| 167 | let info = KeywordInfo::from_keyword(text); |
| 168 | keyword_info = Some(info); |
| 169 | key = Some(text.to_string()); |
| 170 | } |
| 171 | |
| 172 | // Check for operator field |
| 173 | if let Some(operator_node) = keyword_node.child_by_field_name("operator") { |
| 174 | let Ok(op_text) = operator_node.utf8_text(input_bytes) else { |
| 175 | return Err( |
| 176 | SshdConfigError::ParserError(t!("parser.failedToParseNode", input = input).to_string()) |
| 177 | ); |
| 178 | }; |
| 179 | operator = Some(op_text.to_string()); |
| 180 | } |
| 181 | |
| 182 | // Validate that structured keywords cannot use operators |
| 183 | if let Some(ref info) = keyword_info && !info.allows_operator() && operator.is_some() { |
| 184 | return Err(SshdConfigError::ParserError( |
| 185 | t!("parser.structuredKeywordCannotUseOperator", keyword = &info.name).to_string() |
| 186 | )); |
| 187 | } |
| 188 | |
| 189 | for node in keyword_node.named_children(&mut cursor) { |
| 190 | if node.is_error() { |
| 191 | return Err(SshdConfigError::ParserError(t!("parser.failedToParseNode", input = input).to_string())); |
| 192 | } |
| 193 | if node.kind() == "arguments" && let Some(ref info) = keyword_info { |
| 194 | value = parse_arguments_node(node, input, input_bytes, info)?; |
| 195 | debug!("{}: {:?}", t!("parser.valueDebug").to_string(), value); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | // If operator is present, wrap value in a nested map |
| 200 | if let Some(op) = operator { |
| 201 | let mut operator_map = Map::new(); |
| 202 | operator_map.insert("value".to_string(), value); |
| 203 | operator_map.insert("operator".to_string(), Value::String(op)); |
| 204 | value = Value::Object(operator_map); |
| 205 | } |
| 206 | |
| 207 | if let Some(key) = key { |
| 208 | if value.is_null() { |
nothing calls this directly
no test coverage detected