| 1 | use std::collections::BTreeMap; |
| 2 | use std::fmt; |
| 3 | |
| 4 | #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] |
| 5 | pub enum MonitorType { |
| 6 | #[default] |
| 7 | Unknown, |
| 8 | Lcd, |
| 9 | Crt, |
| 10 | } |
| 11 | |
| 12 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 13 | pub struct VcpEntry { |
| 14 | values: Option<Vec<u16>>, |
| 15 | } |
| 16 | |
| 17 | impl VcpEntry { |
| 18 | pub fn all_values() -> Self { |
| 19 | Self { values: None } |
| 20 | } |
| 21 | |
| 22 | pub fn with_values(values: Vec<u16>) -> Self { |
| 23 | Self { |
| 24 | values: Some(values), |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | pub fn values(&self) -> Option<&[u16]> { |
| 29 | self.values.as_deref() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 34 | pub struct Caps { |
| 35 | monitor_type: MonitorType, |
| 36 | vcp: BTreeMap<u8, VcpEntry>, |
| 37 | } |
| 38 | |
| 39 | impl Caps { |
| 40 | pub fn parse(input: &str) -> Result<Self, ParseError> { |
| 41 | let mut caps = Self::default(); |
| 42 | caps.apply(input, Edit::Add)?; |
| 43 | Ok(caps) |
| 44 | } |
| 45 | |
| 46 | pub fn apply_add(&mut self, input: &str) -> Result<usize, ParseError> { |
| 47 | self.apply(input, Edit::Add) |
| 48 | } |
| 49 | |
| 50 | pub fn apply_remove(&mut self, input: &str) -> Result<usize, ParseError> { |
| 51 | self.apply(input, Edit::Remove) |
| 52 | } |
| 53 | |
| 54 | pub fn monitor_type(&self) -> MonitorType { |
| 55 | self.monitor_type |
| 56 | } |
| 57 | |
| 58 | pub fn is_supported(&self, code: u8) -> bool { |
| 59 | self.vcp.contains_key(&code) |
| 60 | } |
| 61 | |
| 62 | pub fn vcp(&self, code: u8) -> Option<&VcpEntry> { |
| 63 | self.vcp.get(&code) |
| 64 | } |
| 65 | |
| 66 | pub fn vcp_codes(&self) -> impl Iterator<Item = u8> + '_ { |
| 67 | self.vcp.keys().copied() |
| 68 | } |
| 69 | |
| 70 | pub fn vcp_entries(&self) -> impl Iterator<Item = (u8, &VcpEntry)> + '_ { |
| 71 | self.vcp.iter().map(|(&code, entry)| (code, entry)) |
| 72 | } |
| 73 | |
| 74 | pub fn set_monitor_type(&mut self, monitor_type: MonitorType) { |
| 75 | self.monitor_type = monitor_type; |
| 76 | } |
| 77 | |
| 78 | pub fn insert_vcp(&mut self, code: u8, entry: VcpEntry) { |
| 79 | self.vcp.insert(code, entry); |
| 80 | } |
| 81 | |
| 82 | fn apply(&mut self, input: &str, edit: Edit) -> Result<usize, ParseError> { |
| 83 | let parsed = Parser::new(input).parse()?; |
| 84 | |
| 85 | if edit == Edit::Add { |
| 86 | if let Some(monitor_type) = parsed.monitor_type { |
| 87 | self.monitor_type = monitor_type; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | for item in &parsed.vcp { |
| 92 | match edit { |
| 93 | Edit::Add => { |
| 94 | self.vcp.insert( |
| 95 | item.code, |
| 96 | match &item.values { |
| 97 | Some(values) => VcpEntry::with_values(values.clone()), |
| 98 | None => VcpEntry::all_values(), |
| 99 | }, |
| 100 | ); |
| 101 | } |
| 102 | Edit::Remove => match &item.values { |
| 103 | Some(remove_values) => { |
| 104 | if let Some(entry) = self.vcp.get_mut(&item.code) { |
| 105 | if let Some(values) = &mut entry.values { |
| 106 | values.retain(|value| !remove_values.contains(value)); |
| 107 | if values.is_empty() { |
| 108 | self.vcp.remove(&item.code); |
| 109 | } |
| 110 | } else { |
| 111 | self.vcp.remove(&item.code); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | None => { |
| 116 | self.vcp.remove(&item.code); |
| 117 | } |
| 118 | }, |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | Ok(parsed.vcp.len()) |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 127 | pub struct ParseError { |
| 128 | position: usize, |
| 129 | kind: ErrorKind, |
| 130 | } |
| 131 | |
| 132 | impl ParseError { |
| 133 | pub fn position(&self) -> usize { |
| 134 | self.position |
| 135 | } |
| 136 | |
| 137 | pub fn kind(&self) -> ErrorKind { |
| 138 | self.kind |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | impl fmt::Display for ParseError { |
| 143 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 144 | write!(f, "{} at byte {}", self.kind, self.position) |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | impl std::error::Error for ParseError {} |
| 149 | |
| 150 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 151 | pub enum ErrorKind { |
| 152 | InvalidHex, |
| 153 | UnexpectedCloseParen, |
| 154 | UnexpectedEnd, |
| 155 | UnbalancedParens, |
| 156 | ValueWithoutVcpCode, |
| 157 | } |
| 158 | |
| 159 | impl fmt::Display for ErrorKind { |
| 160 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 161 | match self { |
| 162 | Self::InvalidHex => f.write_str("invalid hexadecimal value"), |
| 163 | Self::UnexpectedCloseParen => f.write_str("unexpected closing parenthesis"), |
| 164 | Self::UnexpectedEnd => f.write_str("unexpected end of capability string"), |
| 165 | Self::UnbalancedParens => f.write_str("unbalanced parentheses"), |
| 166 | Self::ValueWithoutVcpCode => f.write_str("value list without VCP code"), |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | #[derive(Clone, Copy, Debug, Eq, PartialEq)] |
| 172 | enum Edit { |
| 173 | Add, |
| 174 | Remove, |
| 175 | } |
| 176 | |
| 177 | #[derive(Clone, Debug, Default, Eq, PartialEq)] |
| 178 | struct ParsedCaps { |
| 179 | monitor_type: Option<MonitorType>, |
| 180 | vcp: Vec<ParsedVcp>, |
| 181 | } |
| 182 | |
| 183 | #[derive(Clone, Debug, Eq, PartialEq)] |
| 184 | struct ParsedVcp { |
| 185 | code: u8, |
| 186 | values: Option<Vec<u16>>, |
| 187 | } |
| 188 | |
| 189 | struct Parser<'a> { |
| 190 | input: &'a str, |
| 191 | bytes: &'a [u8], |
| 192 | pos: usize, |
| 193 | } |
| 194 | |
| 195 | impl<'a> Parser<'a> { |
| 196 | fn new(input: &'a str) -> Self { |
| 197 | Self { |
| 198 | input, |
| 199 | bytes: input.as_bytes(), |
| 200 | pos: 0, |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | fn parse(mut self) -> Result<ParsedCaps, ParseError> { |
| 205 | let mut parsed = ParsedCaps::default(); |
| 206 | self.parse_sections(None, &mut parsed)?; |
| 207 | Ok(parsed) |
| 208 | } |
| 209 | |
| 210 | fn parse_sections( |
| 211 | &mut self, |
| 212 | stop_at: Option<u8>, |
| 213 | parsed: &mut ParsedCaps, |
| 214 | ) -> Result<(), ParseError> { |
| 215 | loop { |
| 216 | self.skip_spaces(); |
| 217 | |
| 218 | match self.peek() { |
| 219 | None if stop_at.is_some() => return Err(self.err(ErrorKind::UnbalancedParens)), |
| 220 | None => return Ok(()), |
| 221 | Some(byte) if Some(byte) == stop_at => { |
| 222 | self.pos += 1; |
| 223 | return Ok(()); |
| 224 | } |
| 225 | Some(b')') => return Err(self.err(ErrorKind::UnexpectedCloseParen)), |
| 226 | Some(b'(') => { |
| 227 | self.pos += 1; |
| 228 | self.parse_sections(Some(b')'), parsed)?; |
| 229 | } |
| 230 | Some(byte) if is_name_byte(byte) => { |
| 231 | let name = self.take_name(); |
| 232 | self.skip_spaces(); |
| 233 | if self.consume(b'(') { |
| 234 | match name { |
| 235 | "type" => self.parse_type(parsed)?, |
| 236 | "vcp" => self.parse_vcp(parsed)?, |
| 237 | _ => self.skip_balanced()?, |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | Some(_) => { |
| 242 | self.pos += 1; |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | fn parse_type(&mut self, parsed: &mut ParsedCaps) -> Result<(), ParseError> { |
| 249 | self.skip_spaces(); |
| 250 | let start = self.pos; |
| 251 | |
| 252 | while let Some(byte) = self.peek() { |
| 253 | if byte == b')' || byte.is_ascii_whitespace() { |
| 254 | break; |
| 255 | } |
| 256 | self.pos += 1; |
| 257 | } |
| 258 | |
| 259 | let value = self.input[start..self.pos].to_ascii_lowercase(); |
| 260 | match value.as_str() { |
| 261 | "lcd" => parsed.monitor_type = Some(MonitorType::Lcd), |
| 262 | "crt" => parsed.monitor_type = Some(MonitorType::Crt), |
| 263 | _ => {} |
| 264 | } |
| 265 | |
| 266 | self.skip_balanced_from_current() |
| 267 | } |
| 268 | |
| 269 | fn parse_vcp(&mut self, parsed: &mut ParsedCaps) -> Result<(), ParseError> { |
| 270 | loop { |
| 271 | self.skip_spaces(); |
| 272 | |
| 273 | match self.peek() { |
| 274 | None => return Err(self.err(ErrorKind::UnbalancedParens)), |
| 275 | Some(b')') => { |
| 276 | self.pos += 1; |
| 277 | return Ok(()); |
| 278 | } |
| 279 | Some(b'(') => return Err(self.err(ErrorKind::ValueWithoutVcpCode)), |
| 280 | Some(byte) if is_name_byte(byte) && !byte.is_ascii_hexdigit() => { |
| 281 | let name_position = self.pos; |
| 282 | let name = self.take_name(); |
| 283 | self.skip_spaces(); |
| 284 | if self.consume(b'(') { |
| 285 | if name.eq_ignore_ascii_case("vcp") { |
| 286 | self.parse_vcp(parsed)?; |
| 287 | } else { |
| 288 | self.skip_balanced()?; |
| 289 | } |
| 290 | } else { |
| 291 | return Err(self.err_at(ErrorKind::InvalidHex, name_position)); |
| 292 | } |
| 293 | } |
| 294 | Some(_) => { |
| 295 | let code_position = self.pos; |
| 296 | let code = self.take_vcp_code(code_position)?; |
| 297 | self.skip_spaces(); |
| 298 | |
| 299 | let values = if self.consume(b'(') { |
| 300 | match self.parse_vcp_values()? { |
| 301 | values if values.is_empty() => None, |
| 302 | values => Some(values), |
| 303 | } |
| 304 | } else { |
| 305 | None |
| 306 | }; |
| 307 | |
| 308 | parsed.vcp.push(ParsedVcp { code, values }); |
| 309 | } |
| 310 | } |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | fn parse_vcp_values(&mut self) -> Result<Vec<u16>, ParseError> { |
| 315 | let mut values = Vec::new(); |
| 316 | |
| 317 | loop { |
| 318 | self.skip_spaces(); |
| 319 | |
| 320 | match self.peek() { |
| 321 | None => return Err(self.err(ErrorKind::UnbalancedParens)), |
| 322 | Some(b')') => { |
| 323 | self.pos += 1; |
| 324 | return Ok(values); |
| 325 | } |
| 326 | Some(b'(') => { |
| 327 | self.pos += 1; |
| 328 | self.skip_balanced()?; |
| 329 | } |
| 330 | Some(_) => { |
| 331 | let value_position = self.pos; |
| 332 | let token = self.take_token()?; |
| 333 | let value = parse_hex_u16(token) |
| 334 | .ok_or_else(|| self.err_at(ErrorKind::InvalidHex, value_position))?; |
| 335 | values.push(value); |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | fn skip_balanced(&mut self) -> Result<(), ParseError> { |
| 342 | let mut depth = 1usize; |
| 343 | |
| 344 | while let Some(byte) = self.peek() { |
| 345 | self.pos += 1; |
| 346 | match byte { |
| 347 | b'(' => depth += 1, |
| 348 | b')' => { |
| 349 | depth -= 1; |
| 350 | if depth == 0 { |
| 351 | return Ok(()); |
| 352 | } |
| 353 | } |
| 354 | _ => {} |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | Err(self.err(ErrorKind::UnbalancedParens)) |
| 359 | } |
| 360 | |
| 361 | fn skip_balanced_from_current(&mut self) -> Result<(), ParseError> { |
| 362 | loop { |
| 363 | match self.peek() { |
| 364 | None => return Err(self.err(ErrorKind::UnbalancedParens)), |
| 365 | Some(b')') => { |
| 366 | self.pos += 1; |
| 367 | return Ok(()); |
| 368 | } |
| 369 | Some(b'(') => { |
| 370 | self.pos += 1; |
| 371 | self.skip_balanced()?; |
| 372 | } |
| 373 | Some(_) => { |
| 374 | self.pos += 1; |
| 375 | } |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | fn take_vcp_code(&mut self, position: usize) -> Result<u8, ParseError> { |
| 381 | if self.bytes.len().saturating_sub(self.pos) < 2 { |
| 382 | return Err(self.err_at(ErrorKind::InvalidHex, position)); |
| 383 | } |
| 384 | |
| 385 | let token = &self.bytes[self.pos..self.pos + 2]; |
| 386 | if !token.iter().all(u8::is_ascii_hexdigit) { |
| 387 | return Err(self.err_at(ErrorKind::InvalidHex, position)); |
| 388 | } |
| 389 | |
| 390 | self.pos += 2; |
| 391 | Ok(hex_nibble(token[0]) * 16 + hex_nibble(token[1])) |
| 392 | } |
| 393 | |
| 394 | fn take_name(&mut self) -> &'a str { |
| 395 | let start = self.pos; |
| 396 | while matches!(self.peek(), Some(byte) if is_name_byte(byte)) { |
| 397 | self.pos += 1; |
| 398 | } |
| 399 | &self.input[start..self.pos] |
| 400 | } |
| 401 | |
| 402 | fn take_token(&mut self) -> Result<&'a str, ParseError> { |
| 403 | let start = self.pos; |
| 404 | while let Some(byte) = self.peek() { |
| 405 | if byte == b'(' || byte == b')' || byte.is_ascii_whitespace() { |
| 406 | break; |
| 407 | } |
| 408 | self.pos += 1; |
| 409 | } |
| 410 | |
| 411 | if start == self.pos { |
| 412 | Err(self.err(ErrorKind::UnexpectedEnd)) |
| 413 | } else { |
| 414 | Ok(&self.input[start..self.pos]) |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | fn skip_spaces(&mut self) { |
| 419 | while matches!(self.peek(), Some(byte) if byte.is_ascii_whitespace()) { |
| 420 | self.pos += 1; |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | fn consume(&mut self, expected: u8) -> bool { |
| 425 | if self.peek() == Some(expected) { |
| 426 | self.pos += 1; |
| 427 | true |
| 428 | } else { |
| 429 | false |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | fn peek(&self) -> Option<u8> { |
| 434 | self.bytes.get(self.pos).copied() |
| 435 | } |
| 436 | |
| 437 | fn err(&self, kind: ErrorKind) -> ParseError { |
| 438 | self.err_at(kind, self.pos) |
| 439 | } |
| 440 | |
| 441 | fn err_at(&self, kind: ErrorKind, position: usize) -> ParseError { |
| 442 | ParseError { position, kind } |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | fn is_name_byte(byte: u8) -> bool { |
| 447 | byte.is_ascii_alphanumeric() || byte == b'_' |
| 448 | } |
| 449 | |
| 450 | fn parse_hex_u16(token: &str) -> Option<u16> { |
| 451 | if token.is_empty() || token.len() > 4 || !token.bytes().all(|byte| byte.is_ascii_hexdigit()) { |
| 452 | return None; |
| 453 | } |
| 454 | u16::from_str_radix(token, 16).ok() |
| 455 | } |
| 456 | |
| 457 | fn hex_nibble(byte: u8) -> u8 { |
| 458 | match byte { |
| 459 | b'0'..=b'9' => byte - b'0', |
| 460 | b'a'..=b'f' => byte - b'a' + 10, |
| 461 | b'A'..=b'F' => byte - b'A' + 10, |
| 462 | _ => unreachable!("validated hex byte"), |
| 463 | } |
| 464 | } |
| 465 | |
| 466 | #[cfg(test)] |
| 467 | mod tests { |
| 468 | use super::*; |
| 469 | |
| 470 | const BASIC_LCD: &str = include_str!("../fixtures/basic_lcd.caps"); |
| 471 | const COMPACT_VCP: &str = include_str!("../fixtures/compact_vcp.caps"); |
| 472 | const FALLBACK_CRT: &str = include_str!("../fixtures/fallback_crt.caps"); |
| 473 | const NESTED_VALUES: &str = include_str!("../fixtures/nested_values.caps"); |
| 474 | const DB_PATCH: &str = include_str!("../fixtures/db_patch_add_remove.caps"); |
| 475 | |
| 476 | #[test] |
| 477 | fn parses_basic_lcd_fixture() { |
| 478 | let caps = Caps::parse(BASIC_LCD).unwrap(); |
| 479 | |
| 480 | assert_eq!(caps.monitor_type(), MonitorType::Lcd); |
| 481 | assert!(caps.is_supported(0x10)); |
| 482 | assert!(caps.is_supported(0x12)); |
| 483 | assert_eq!( |
| 484 | caps.vcp(0x14).unwrap().values(), |
| 485 | Some([0x05, 0x08, 0x0b].as_slice()) |
| 486 | ); |
| 487 | assert_eq!( |
| 488 | caps.vcp(0x60).unwrap().values(), |
| 489 | Some([0x01, 0x03].as_slice()) |
| 490 | ); |
| 491 | } |
| 492 | |
| 493 | #[test] |
| 494 | fn parses_fallback_crt_fixture() { |
| 495 | let caps = Caps::parse(FALLBACK_CRT).unwrap(); |
| 496 | |
| 497 | assert_eq!(caps.monitor_type(), MonitorType::Crt); |
| 498 | assert_eq!( |
| 499 | caps.vcp_codes().collect::<Vec<_>>(), |
| 500 | vec![0x10, 0x12, 0x16, 0x18, 0x1a, 0x50, 0x92] |
| 501 | ); |
| 502 | } |
| 503 | |
| 504 | #[test] |
| 505 | fn parses_compact_vcp_codes() { |
| 506 | let caps = Caps::parse(COMPACT_VCP).unwrap(); |
| 507 | |
| 508 | assert!(caps.is_supported(0x02)); |
| 509 | assert!(caps.is_supported(0x03)); |
| 510 | assert!(caps.is_supported(0x04)); |
| 511 | assert!(caps.is_supported(0x05)); |
| 512 | assert_eq!( |
| 513 | caps.vcp(0x14).unwrap().values(), |
| 514 | Some([0x05, 0x08].as_slice()) |
| 515 | ); |
| 516 | assert_eq!( |
| 517 | caps.vcp(0x60).unwrap().values(), |
| 518 | Some([0x03, 0x04].as_slice()) |
| 519 | ); |
| 520 | } |
| 521 | |
| 522 | #[test] |
| 523 | fn tolerates_nested_value_groups() { |
| 524 | let caps = Caps::parse(NESTED_VALUES).unwrap(); |
| 525 | |
| 526 | assert_eq!(caps.vcp(0xe0).unwrap().values(), Some([0x02].as_slice())); |
| 527 | assert_eq!( |
| 528 | caps.vcp(0xe1).unwrap().values(), |
| 529 | Some([0x03, 0x04].as_slice()) |
| 530 | ); |
| 531 | assert!(caps.is_supported(0xdf)); |
| 532 | } |
| 533 | |
| 534 | #[test] |
| 535 | fn supports_database_style_add_remove() { |
| 536 | let mut caps = Caps::parse(DB_PATCH).unwrap(); |
| 537 | |
| 538 | assert!(caps.is_supported(0x10)); |
| 539 | assert!(caps.is_supported(0x12)); |
| 540 | assert_eq!( |
| 541 | caps.vcp(0x60).unwrap().values(), |
| 542 | Some([0x01, 0x03].as_slice()) |
| 543 | ); |
| 544 | |
| 545 | assert_eq!(caps.apply_remove("(vcp(12 60(01)))").unwrap(), 2); |
| 546 | assert!(!caps.is_supported(0x12)); |
| 547 | assert_eq!(caps.vcp(0x60).unwrap().values(), Some([0x03].as_slice())); |
| 548 | |
| 549 | assert_eq!(caps.apply_add("(vcp(12 60(0f)))").unwrap(), 2); |
| 550 | assert!(caps.is_supported(0x12)); |
| 551 | assert_eq!(caps.vcp(0x60).unwrap().values(), Some([0x0f].as_slice())); |
| 552 | } |
| 553 | |
| 554 | #[test] |
| 555 | fn removing_specific_values_from_all_values_removes_control() { |
| 556 | let mut caps = Caps::parse("(vcp(10))").unwrap(); |
| 557 | |
| 558 | assert_eq!(caps.apply_remove("(vcp(10(01)))").unwrap(), 1); |
| 559 | assert!(!caps.is_supported(0x10)); |
| 560 | } |
| 561 | |
| 562 | #[test] |
| 563 | fn empty_value_list_matches_legacy_all_values_entry() { |
| 564 | let mut caps = Caps::parse("(vcp(10()))").unwrap(); |
| 565 | |
| 566 | assert_eq!(caps.vcp(0x10).unwrap().values(), None); |
| 567 | assert_eq!(caps.apply_remove("(vcp(10()))").unwrap(), 1); |
| 568 | assert!(!caps.is_supported(0x10)); |
| 569 | } |
| 570 | |
| 571 | #[test] |
| 572 | fn accepts_uppercase_type_names() { |
| 573 | assert_eq!( |
| 574 | Caps::parse("(type(LCD))").unwrap().monitor_type(), |
| 575 | MonitorType::Lcd |
| 576 | ); |
| 577 | assert_eq!( |
| 578 | Caps::parse("(type(CRT))").unwrap().monitor_type(), |
| 579 | MonitorType::Crt |
| 580 | ); |
| 581 | } |
| 582 | |
| 583 | #[test] |
| 584 | fn unknown_type_does_not_override_existing_type() { |
| 585 | let mut caps = Caps::parse("(type(lcd))").unwrap(); |
| 586 | |
| 587 | assert_eq!(caps.apply_add("(type(OLED))").unwrap(), 0); |
| 588 | assert_eq!(caps.monitor_type(), MonitorType::Lcd); |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn supports_mixed_controls_and_value_lists() { |
| 593 | let caps = Caps::parse("(vcp(10(01 02) 12 14(0A)))").unwrap(); |
| 594 | |
| 595 | assert_eq!(caps.vcp_codes().collect::<Vec<_>>(), vec![0x10, 0x12, 0x14]); |
| 596 | assert_eq!( |
| 597 | caps.vcp(0x10).unwrap().values(), |
| 598 | Some([0x01, 0x02].as_slice()) |
| 599 | ); |
| 600 | assert_eq!(caps.vcp(0x12).unwrap().values(), None); |
| 601 | assert_eq!(caps.vcp(0x14).unwrap().values(), Some([0x0a].as_slice())); |
| 602 | } |
| 603 | |
| 604 | #[test] |
| 605 | fn accepts_nested_database_vcp_sections() { |
| 606 | let caps = Caps::parse("type(lcd)vcp(vcp(10 14(01 05) C8 C9))").unwrap(); |
| 607 | |
| 608 | assert_eq!(caps.monitor_type(), MonitorType::Lcd); |
| 609 | assert_eq!(caps.vcp_codes().collect::<Vec<_>>(), vec![0x10, 0x14, 0xc8, 0xc9]); |
| 610 | assert_eq!( |
| 611 | caps.vcp(0x14).unwrap().values(), |
| 612 | Some([0x01, 0x05].as_slice()) |
| 613 | ); |
| 614 | } |
| 615 | |
| 616 | #[test] |
| 617 | fn rejects_invalid_vcp_identifier() { |
| 618 | assert_eq!( |
| 619 | Caps::parse("(vcp(GG))").unwrap_err().kind(), |
| 620 | ErrorKind::InvalidHex |
| 621 | ); |
| 622 | assert_eq!( |
| 623 | Caps::parse("(vcp(-1))").unwrap_err().kind(), |
| 624 | ErrorKind::InvalidHex |
| 625 | ); |
| 626 | assert_eq!( |
| 627 | Caps::parse("(vcp(☃))").unwrap_err().kind(), |
| 628 | ErrorKind::InvalidHex |
| 629 | ); |
| 630 | } |
| 631 | |
| 632 | #[test] |
| 633 | fn rejects_invalid_values() { |
| 634 | assert_eq!( |
| 635 | Caps::parse("(vcp(10(0G)))").unwrap_err().kind(), |
| 636 | ErrorKind::InvalidHex |
| 637 | ); |
| 638 | assert_eq!( |
| 639 | Caps::parse("(vcp(10(10000)))").unwrap_err().kind(), |
| 640 | ErrorKind::InvalidHex |
| 641 | ); |
| 642 | } |
| 643 | |
| 644 | #[test] |
| 645 | fn rejects_value_list_without_vcp_code() { |
| 646 | assert_eq!( |
| 647 | Caps::parse("(vcp((01)))").unwrap_err().kind(), |
| 648 | ErrorKind::ValueWithoutVcpCode |
| 649 | ); |
| 650 | } |
| 651 | |
| 652 | #[test] |
| 653 | fn rejects_unbalanced_parentheses() { |
| 654 | assert_eq!( |
| 655 | Caps::parse("(vcp(10)").unwrap_err().kind(), |
| 656 | ErrorKind::UnbalancedParens |
| 657 | ); |
| 658 | assert_eq!( |
| 659 | Caps::parse(")(vcp(10))").unwrap_err().kind(), |
| 660 | ErrorKind::UnexpectedCloseParen |
| 661 | ); |
| 662 | } |
| 663 | |
| 664 | #[test] |
| 665 | fn accepts_nested_unknown_sections_before_vcp() { |
| 666 | let caps = Caps::parse("(prot(monitor)foo(bar(baz))vcp(10))").unwrap(); |
| 667 | |
| 668 | assert!(caps.is_supported(0x10)); |
| 669 | } |
| 670 | |
| 671 | #[test] |
| 672 | fn supports_boundary_control_and_value_ranges() { |
| 673 | let caps = Caps::parse("(vcp(00(0000) FF(FFFF)))").unwrap(); |
| 674 | |
| 675 | assert_eq!(caps.vcp(0x00).unwrap().values(), Some([0x0000].as_slice())); |
| 676 | assert_eq!(caps.vcp(0xff).unwrap().values(), Some([0xffff].as_slice())); |
| 677 | } |
| 678 | } |