| 1 | use std::collections::{HashMap, HashSet}; |
| 2 | |
| 3 | use crate::cli; |
| 4 | use crate::color; |
| 5 | use crate::fatal; |
| 6 | use crate::git_config::GitConfig; |
| 7 | use crate::style::{self, Style}; |
| 8 | |
| 9 | #[derive(Debug, Clone)] |
| 10 | enum StyleReference { |
| 11 | Style(Style), |
| 12 | Reference(String), |
| 13 | } |
| 14 | |
| 15 | fn is_style_reference(style_string: &str) -> bool { |
| 16 | style_string.ends_with("-style") && !style_string.chars().any(|c| c == ' ') |
| 17 | } |
| 18 | |
| 19 | pub fn parse_styles(opt: &cli::Opt) -> HashMap<String, Style> { |
| 20 | let mut styles: HashMap<&str, StyleReference> = HashMap::new(); |
| 21 | |
| 22 | make_hunk_styles(opt, &mut styles); |
| 23 | make_commit_file_hunk_header_styles(opt, &mut styles); |
| 24 | make_line_number_styles(opt, &mut styles); |
| 25 | make_blame_styles(opt, &mut styles); |
| 26 | make_grep_styles(opt, &mut styles); |
| 27 | make_merge_conflict_styles(opt, &mut styles); |
| 28 | make_misc_styles(opt, &mut styles); |
| 29 | |
| 30 | let mut resolved_styles = resolve_style_references(styles, opt); |
| 31 | resolved_styles |
| 32 | .get_mut("minus-emph-style") |
| 33 | .unwrap_or_else(|| panic!("minus-emph-style not found in resolved styles")) |
| 34 | .is_emph = true; |
| 35 | |
| 36 | resolved_styles |
| 37 | .get_mut("plus-emph-style") |
| 38 | .unwrap_or_else(|| panic!("plus-emph-style not found in resolved styles")) |
| 39 | .is_emph = true; |
| 40 | resolved_styles |
| 41 | } |
| 42 | |
| 43 | pub fn parse_styles_map(opt: &cli::Opt) -> Option<HashMap<style::AnsiTermStyleEqualityKey, Style>> { |
| 44 | if let Some(styles_map_str) = &opt.map_styles { |
| 45 | let mut styles_map = HashMap::new(); |
| 46 | for pair_str in styles_map_str.split(',') { |
| 47 | let mut style_strs = pair_str.split("=>").map(|s| s.trim()); |
| 48 | if let (Some(from_str), Some(to_str)) = (style_strs.next(), style_strs.next()) { |
| 49 | let from_style = parse_as_style_or_reference_to_git_config(from_str, opt); |
| 50 | let to_style = parse_as_style_or_reference_to_git_config(to_str, opt); |
| 51 | styles_map.insert( |
| 52 | style::ansi_term_style_equality_key(from_style.ansi_term_style), |
| 53 | to_style, |
| 54 | ); |
| 55 | } |
| 56 | } |
| 57 | Some(styles_map) |
| 58 | } else { |
| 59 | None |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | fn resolve_style_references( |
| 64 | edges: HashMap<&str, StyleReference>, |
| 65 | opt: &cli::Opt, |
| 66 | ) -> HashMap<String, Style> { |
| 67 | let mut resolved_styles = HashMap::new(); |
| 68 | |
| 69 | for starting_node in edges.keys() { |
| 70 | if resolved_styles.contains_key(*starting_node) { |
| 71 | continue; |
| 72 | } |
| 73 | let mut visited = HashSet::new(); |
| 74 | let mut node = *starting_node; |
| 75 | loop { |
| 76 | if !visited.insert(node) { |
| 77 | #[cfg(not(test))] |
| 78 | fatal(format!("Your delta styles form a cycle! {visited:?}")); |
| 79 | #[cfg(test)] |
| 80 | return [("__cycle__", Style::default())] |
| 81 | .iter() |
| 82 | .map(|(a, b)| (a.to_string(), *b)) |
| 83 | .collect(); |
| 84 | } |
| 85 | match &edges.get(&node) { |
| 86 | Some(StyleReference::Reference(child_node)) => node = child_node, |
| 87 | Some(StyleReference::Style(style)) => { |
| 88 | resolved_styles.extend(visited.iter().map(|node| (node.to_string(), *style))); |
| 89 | break; |
| 90 | } |
| 91 | None => { |
| 92 | let style = parse_as_reference_to_git_config(node, opt); |
| 93 | resolved_styles.extend(visited.iter().map(|node| (node.to_string(), style))); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | resolved_styles |
| 99 | } |
| 100 | |
| 101 | fn parse_as_style_or_reference_to_git_config(style_string: &str, opt: &cli::Opt) -> Style { |
| 102 | match style_from_str(style_string, None, None, true, opt.git_config()) { |
| 103 | StyleReference::Reference(style_ref) => parse_as_reference_to_git_config(&style_ref, opt), |
| 104 | StyleReference::Style(style) => style, |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | fn parse_as_reference_to_git_config(style_string: &str, opt: &cli::Opt) -> Style { |
| 109 | if let Some(git_config) = opt.git_config() { |
| 110 | let git_config_key = format!("delta.{style_string}"); |
| 111 | match git_config.get::<String>(&git_config_key) { |
| 112 | Some(s) => Style::from_git_str(&s), |
| 113 | _ => fatal(format!( |
| 114 | "Style key not found in git config: {git_config_key}", |
| 115 | )), |
| 116 | } |
| 117 | } else { |
| 118 | fatal(format!( |
| 119 | "Style not found (git config unavailable): {style_string}", |
| 120 | )); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn make_hunk_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { |
| 125 | let color_mode = opt.computed.color_mode; |
| 126 | let true_color = opt.computed.true_color; |
| 127 | let minus_style = style_from_str( |
| 128 | &opt.minus_style, |
| 129 | Some(Style::from_colors( |
| 130 | None, |
| 131 | Some(color::get_minus_background_color_default( |
| 132 | color_mode, true_color, |
| 133 | )), |
| 134 | )), |
| 135 | None, |
| 136 | true_color, |
| 137 | opt.git_config(), |
| 138 | ); |
| 139 | |
| 140 | let minus_emph_style = style_from_str( |
| 141 | &opt.minus_emph_style, |
| 142 | Some(Style::from_colors( |
| 143 | None, |
| 144 | Some(color::get_minus_emph_background_color_default( |
| 145 | color_mode, true_color, |
| 146 | )), |
| 147 | )), |
| 148 | None, |
| 149 | true_color, |
| 150 | opt.git_config(), |
| 151 | ); |
| 152 | |
| 153 | let minus_non_emph_style = style_from_str( |
| 154 | &opt.minus_non_emph_style, |
| 155 | None, |
| 156 | None, |
| 157 | true_color, |
| 158 | opt.git_config(), |
| 159 | ); |
| 160 | |
| 161 | // The style used to highlight a removed empty line when otherwise it would be invisible due to |
| 162 | // lack of background color in minus-style. |
| 163 | let minus_empty_line_marker_style = style_from_str( |
| 164 | &opt.minus_empty_line_marker_style, |
| 165 | Some(Style::from_colors( |
| 166 | None, |
| 167 | Some(color::get_minus_background_color_default( |
| 168 | color_mode, true_color, |
| 169 | )), |
| 170 | )), |
| 171 | None, |
| 172 | true_color, |
| 173 | opt.git_config(), |
| 174 | ); |
| 175 | |
| 176 | let zero_style = style_from_str(&opt.zero_style, None, None, true_color, opt.git_config()); |
| 177 | |
| 178 | let plus_style = style_from_str( |
| 179 | &opt.plus_style, |
| 180 | Some(Style::from_colors( |
| 181 | None, |
| 182 | Some(color::get_plus_background_color_default( |
| 183 | color_mode, true_color, |
| 184 | )), |
| 185 | )), |
| 186 | None, |
| 187 | true_color, |
| 188 | opt.git_config(), |
| 189 | ); |
| 190 | |
| 191 | let plus_emph_style = style_from_str( |
| 192 | &opt.plus_emph_style, |
| 193 | Some(Style::from_colors( |
| 194 | None, |
| 195 | Some(color::get_plus_emph_background_color_default( |
| 196 | color_mode, true_color, |
| 197 | )), |
| 198 | )), |
| 199 | None, |
| 200 | true_color, |
| 201 | opt.git_config(), |
| 202 | ); |
| 203 | |
| 204 | let plus_non_emph_style = style_from_str( |
| 205 | &opt.plus_non_emph_style, |
| 206 | None, |
| 207 | None, |
| 208 | true_color, |
| 209 | opt.git_config(), |
| 210 | ); |
| 211 | |
| 212 | // The style used to highlight an added empty line when otherwise it would be invisible due to |
| 213 | // lack of background color in plus-style. |
| 214 | let plus_empty_line_marker_style = style_from_str( |
| 215 | &opt.plus_empty_line_marker_style, |
| 216 | Some(Style::from_colors( |
| 217 | None, |
| 218 | Some(color::get_plus_background_color_default( |
| 219 | color_mode, true_color, |
| 220 | )), |
| 221 | )), |
| 222 | None, |
| 223 | true_color, |
| 224 | opt.git_config(), |
| 225 | ); |
| 226 | |
| 227 | let whitespace_error_style = style_from_str( |
| 228 | &opt.whitespace_error_style, |
| 229 | None, |
| 230 | None, |
| 231 | true_color, |
| 232 | opt.git_config(), |
| 233 | ); |
| 234 | |
| 235 | styles.extend([ |
| 236 | ("minus-style", minus_style), |
| 237 | ("minus-emph-style", minus_emph_style), |
| 238 | ("minus-non-emph-style", minus_non_emph_style), |
| 239 | ( |
| 240 | "minus-empty-line-marker-style", |
| 241 | minus_empty_line_marker_style, |
| 242 | ), |
| 243 | ("zero-style", zero_style), |
| 244 | ("plus-style", plus_style), |
| 245 | ("plus-emph-style", plus_emph_style), |
| 246 | ("plus-non-emph-style", plus_non_emph_style), |
| 247 | ("plus-empty-line-marker-style", plus_empty_line_marker_style), |
| 248 | ("whitespace-error-style", whitespace_error_style), |
| 249 | ]) |
| 250 | } |
| 251 | |
| 252 | fn make_line_number_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { |
| 253 | let true_color = opt.computed.true_color; |
| 254 | let line_numbers_left_style = style_from_str( |
| 255 | &opt.line_numbers_left_style, |
| 256 | None, |
| 257 | None, |
| 258 | true_color, |
| 259 | opt.git_config(), |
| 260 | ); |
| 261 | |
| 262 | let line_numbers_minus_style = style_from_str( |
| 263 | &opt.line_numbers_minus_style, |
| 264 | None, |
| 265 | None, |
| 266 | true_color, |
| 267 | opt.git_config(), |
| 268 | ); |
| 269 | |
| 270 | let line_numbers_zero_style = style_from_str( |
| 271 | &opt.line_numbers_zero_style, |
| 272 | None, |
| 273 | None, |
| 274 | true_color, |
| 275 | opt.git_config(), |
| 276 | ); |
| 277 | |
| 278 | let line_numbers_plus_style = style_from_str( |
| 279 | &opt.line_numbers_plus_style, |
| 280 | None, |
| 281 | None, |
| 282 | true_color, |
| 283 | opt.git_config(), |
| 284 | ); |
| 285 | |
| 286 | let line_numbers_right_style = style_from_str( |
| 287 | &opt.line_numbers_right_style, |
| 288 | None, |
| 289 | None, |
| 290 | true_color, |
| 291 | opt.git_config(), |
| 292 | ); |
| 293 | |
| 294 | styles.extend([ |
| 295 | ("line-numbers-minus-style", line_numbers_minus_style), |
| 296 | ("line-numbers-zero-style", line_numbers_zero_style), |
| 297 | ("line-numbers-plus-style", line_numbers_plus_style), |
| 298 | ("line-numbers-left-style", line_numbers_left_style), |
| 299 | ("line-numbers-right-style", line_numbers_right_style), |
| 300 | ]) |
| 301 | } |
| 302 | |
| 303 | fn make_commit_file_hunk_header_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { |
| 304 | let true_color = opt.computed.true_color; |
| 305 | styles.extend([ |
| 306 | ( |
| 307 | "commit-style", |
| 308 | style_from_str_with_handling_of_special_decoration_attributes( |
| 309 | &opt.commit_style, |
| 310 | None, |
| 311 | Some(&opt.commit_decoration_style), |
| 312 | true_color, |
| 313 | opt.git_config(), |
| 314 | ), |
| 315 | ), |
| 316 | ( |
| 317 | "file-style", |
| 318 | style_from_str_with_handling_of_special_decoration_attributes( |
| 319 | &opt.file_style, |
| 320 | None, |
| 321 | Some(&opt.file_decoration_style), |
| 322 | true_color, |
| 323 | opt.git_config(), |
| 324 | ), |
| 325 | ), |
| 326 | ( |
| 327 | "classic-grep-header-style", |
| 328 | style_from_str_with_handling_of_special_decoration_attributes( |
| 329 | opt.hunk_header_style.as_str(), |
| 330 | None, |
| 331 | opt.grep_header_decoration_style |
| 332 | .as_deref() |
| 333 | .or(Some(opt.hunk_header_decoration_style.as_str())), |
| 334 | true_color, |
| 335 | opt.git_config(), |
| 336 | ), |
| 337 | ), |
| 338 | ( |
| 339 | "ripgrep-header-style", |
| 340 | style_from_str_with_handling_of_special_decoration_attributes( |
| 341 | "file", |
| 342 | None, |
| 343 | opt.grep_header_decoration_style.as_deref().or(Some("none")), |
| 344 | true_color, |
| 345 | opt.git_config(), |
| 346 | ), |
| 347 | ), |
| 348 | ( |
| 349 | "hunk-header-style", |
| 350 | style_from_str_with_handling_of_special_decoration_attributes( |
| 351 | &opt.hunk_header_style, |
| 352 | None, |
| 353 | Some(&opt.hunk_header_decoration_style), |
| 354 | true_color, |
| 355 | opt.git_config(), |
| 356 | ), |
| 357 | ), |
| 358 | ( |
| 359 | "hunk-header-file-style", |
| 360 | style_from_str_with_handling_of_special_decoration_attributes( |
| 361 | &opt.hunk_header_file_style, |
| 362 | None, |
| 363 | None, |
| 364 | true_color, |
| 365 | opt.git_config(), |
| 366 | ), |
| 367 | ), |
| 368 | ( |
| 369 | "classic-grep-header-file-style", |
| 370 | style_from_str_with_handling_of_special_decoration_attributes( |
| 371 | opt.grep_header_file_style |
| 372 | .as_deref() |
| 373 | .unwrap_or(opt.hunk_header_file_style.as_str()), |
| 374 | None, |
| 375 | None, |
| 376 | true_color, |
| 377 | opt.git_config(), |
| 378 | ), |
| 379 | ), |
| 380 | ( |
| 381 | "ripgrep-header-file-style", |
| 382 | style_from_str_with_handling_of_special_decoration_attributes( |
| 383 | opt.grep_header_file_style.as_deref().unwrap_or("magenta"), |
| 384 | None, |
| 385 | None, |
| 386 | true_color, |
| 387 | opt.git_config(), |
| 388 | ), |
| 389 | ), |
| 390 | ( |
| 391 | "hunk-header-line-number-style", |
| 392 | style_from_str_with_handling_of_special_decoration_attributes( |
| 393 | &opt.hunk_header_line_number_style, |
| 394 | None, |
| 395 | None, |
| 396 | true_color, |
| 397 | opt.git_config(), |
| 398 | ), |
| 399 | ), |
| 400 | ]); |
| 401 | } |
| 402 | |
| 403 | fn make_blame_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { |
| 404 | if let Some(style_string) = &opt.blame_code_style { |
| 405 | styles.insert( |
| 406 | "blame-code-style", |
| 407 | style_from_str( |
| 408 | style_string, |
| 409 | None, |
| 410 | None, |
| 411 | opt.computed.true_color, |
| 412 | opt.git_config(), |
| 413 | ), |
| 414 | ); |
| 415 | }; |
| 416 | if let Some(style_string) = &opt.blame_separator_style { |
| 417 | styles.insert( |
| 418 | "blame-separator-style", |
| 419 | style_from_str( |
| 420 | style_string, |
| 421 | None, |
| 422 | None, |
| 423 | opt.computed.true_color, |
| 424 | opt.git_config(), |
| 425 | ), |
| 426 | ); |
| 427 | }; |
| 428 | } |
| 429 | |
| 430 | fn make_grep_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { |
| 431 | styles.extend([ |
| 432 | ( |
| 433 | "grep-match-line-style", |
| 434 | if let Some(s) = &opt.grep_match_line_style { |
| 435 | style_from_str(s, None, None, opt.computed.true_color, opt.git_config()) |
| 436 | } else { |
| 437 | StyleReference::Reference("zero-style".to_owned()) |
| 438 | }, |
| 439 | ), |
| 440 | ( |
| 441 | "grep-match-word-style", |
| 442 | if let Some(s) = &opt.grep_match_word_style { |
| 443 | style_from_str(s, None, None, opt.computed.true_color, opt.git_config()) |
| 444 | } else { |
| 445 | StyleReference::Reference("plus-emph-style".to_owned()) |
| 446 | }, |
| 447 | ), |
| 448 | ( |
| 449 | "grep-context-line-style", |
| 450 | if let Some(s) = &opt.grep_context_line_style { |
| 451 | style_from_str(s, None, None, opt.computed.true_color, opt.git_config()) |
| 452 | } else { |
| 453 | StyleReference::Reference("zero-style".to_owned()) |
| 454 | }, |
| 455 | ), |
| 456 | ( |
| 457 | "grep-file-style", |
| 458 | style_from_str( |
| 459 | &opt.grep_file_style, |
| 460 | None, |
| 461 | None, |
| 462 | opt.computed.true_color, |
| 463 | opt.git_config(), |
| 464 | ), |
| 465 | ), |
| 466 | ( |
| 467 | "grep-line-number-style", |
| 468 | style_from_str( |
| 469 | &opt.grep_line_number_style, |
| 470 | None, |
| 471 | None, |
| 472 | opt.computed.true_color, |
| 473 | opt.git_config(), |
| 474 | ), |
| 475 | ), |
| 476 | ]) |
| 477 | } |
| 478 | |
| 479 | fn make_merge_conflict_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { |
| 480 | styles.insert( |
| 481 | "merge-conflict-ours-diff-header-style", |
| 482 | style_from_str_with_handling_of_special_decoration_attributes( |
| 483 | &opt.merge_conflict_ours_diff_header_style, |
| 484 | None, |
| 485 | Some(&opt.merge_conflict_ours_diff_header_decoration_style), |
| 486 | opt.computed.true_color, |
| 487 | opt.git_config(), |
| 488 | ), |
| 489 | ); |
| 490 | styles.insert( |
| 491 | "merge-conflict-theirs-diff-header-style", |
| 492 | style_from_str_with_handling_of_special_decoration_attributes( |
| 493 | &opt.merge_conflict_theirs_diff_header_style, |
| 494 | None, |
| 495 | Some(&opt.merge_conflict_theirs_diff_header_decoration_style), |
| 496 | opt.computed.true_color, |
| 497 | opt.git_config(), |
| 498 | ), |
| 499 | ); |
| 500 | } |
| 501 | |
| 502 | fn make_misc_styles(opt: &cli::Opt, styles: &mut HashMap<&str, StyleReference>) { |
| 503 | styles.insert( |
| 504 | "inline-hint-style", |
| 505 | style_from_str( |
| 506 | &opt.inline_hint_style, |
| 507 | None, |
| 508 | None, |
| 509 | opt.computed.true_color, |
| 510 | opt.git_config(), |
| 511 | ), |
| 512 | ); |
| 513 | styles.insert( |
| 514 | "git-minus-style", |
| 515 | StyleReference::Style( |
| 516 | match opt |
| 517 | .git_config() |
| 518 | .and_then(|cfg| cfg.get::<String>("color.diff.old")) |
| 519 | { |
| 520 | Some(s) => Style::from_git_str(&s), |
| 521 | None => *style::GIT_DEFAULT_MINUS_STYLE, |
| 522 | }, |
| 523 | ), |
| 524 | ); |
| 525 | styles.insert( |
| 526 | "git-plus-style", |
| 527 | StyleReference::Style( |
| 528 | match opt |
| 529 | .git_config() |
| 530 | .and_then(|cfg| cfg.get::<String>("color.diff.new")) |
| 531 | { |
| 532 | Some(s) => Style::from_git_str(&s), |
| 533 | None => *style::GIT_DEFAULT_PLUS_STYLE, |
| 534 | }, |
| 535 | ), |
| 536 | ); |
| 537 | } |
| 538 | |
| 539 | fn style_from_str( |
| 540 | style_string: &str, |
| 541 | default: Option<Style>, |
| 542 | decoration_style_string: Option<&str>, |
| 543 | true_color: bool, |
| 544 | git_config: Option<&GitConfig>, |
| 545 | ) -> StyleReference { |
| 546 | if is_style_reference(style_string) { |
| 547 | StyleReference::Reference(style_string.to_owned()) |
| 548 | } else { |
| 549 | StyleReference::Style(Style::from_str( |
| 550 | style_string, |
| 551 | default, |
| 552 | decoration_style_string, |
| 553 | true_color, |
| 554 | git_config, |
| 555 | )) |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | fn style_from_str_with_handling_of_special_decoration_attributes( |
| 560 | style_string: &str, |
| 561 | default: Option<Style>, |
| 562 | decoration_style_string: Option<&str>, |
| 563 | true_color: bool, |
| 564 | git_config: Option<&GitConfig>, |
| 565 | ) -> StyleReference { |
| 566 | if is_style_reference(style_string) { |
| 567 | StyleReference::Reference(style_string.to_owned()) |
| 568 | } else { |
| 569 | StyleReference::Style( |
| 570 | Style::from_str_with_handling_of_special_decoration_attributes( |
| 571 | style_string, |
| 572 | default, |
| 573 | decoration_style_string, |
| 574 | true_color, |
| 575 | git_config, |
| 576 | ), |
| 577 | ) |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | #[cfg(test)] |
| 582 | mod tests { |
| 583 | use super::*; |
| 584 | use crate::tests::integration_test_utils; |
| 585 | |
| 586 | fn resolve_style_references(edges: HashMap<&str, StyleReference>) -> HashMap<String, Style> { |
| 587 | let opt = integration_test_utils::make_options_from_args(&[]); |
| 588 | super::resolve_style_references(edges, &opt) |
| 589 | } |
| 590 | |
| 591 | #[test] |
| 592 | fn test_resolve_style_references_1() { |
| 593 | let style_1 = Style::default(); |
| 594 | let style_2 = style::Style { |
| 595 | is_syntax_highlighted: !style_1.is_syntax_highlighted, |
| 596 | ..Default::default() |
| 597 | }; |
| 598 | |
| 599 | let edges: HashMap<&str, StyleReference> = [ |
| 600 | ("a", StyleReference::Style(style_1)), |
| 601 | ("b", StyleReference::Reference("c".to_string())), |
| 602 | ("c", StyleReference::Style(style_2)), |
| 603 | ] |
| 604 | .iter() |
| 605 | .map(|(a, b)| (*a, b.clone())) |
| 606 | .collect(); |
| 607 | |
| 608 | let expected = [("a", style_1), ("b", style_2), ("c", style_2)] |
| 609 | .iter() |
| 610 | .map(|(a, b)| (a.to_string(), *b)) |
| 611 | .collect(); |
| 612 | |
| 613 | assert_eq!(resolve_style_references(edges), expected); |
| 614 | } |
| 615 | |
| 616 | #[test] |
| 617 | fn test_resolve_style_references_2() { |
| 618 | let style_1 = Style::default(); |
| 619 | let style_2 = style::Style { |
| 620 | is_syntax_highlighted: !style_1.is_syntax_highlighted, |
| 621 | ..Default::default() |
| 622 | }; |
| 623 | |
| 624 | let edges: HashMap<&str, StyleReference> = [ |
| 625 | ("a", StyleReference::Reference("b".to_string())), |
| 626 | ("b", StyleReference::Reference("c".to_string())), |
| 627 | ("c", StyleReference::Style(style_1)), |
| 628 | ("d", StyleReference::Reference("b".to_string())), |
| 629 | ("e", StyleReference::Reference("a".to_string())), |
| 630 | ("f", StyleReference::Style(style_2)), |
| 631 | ("g", StyleReference::Reference("f".to_string())), |
| 632 | ("h", StyleReference::Reference("g".to_string())), |
| 633 | ("i", StyleReference::Reference("g".to_string())), |
| 634 | ] |
| 635 | .iter() |
| 636 | .map(|(a, b)| (*a, b.clone())) |
| 637 | .collect(); |
| 638 | |
| 639 | let expected = [ |
| 640 | ("a", style_1), |
| 641 | ("b", style_1), |
| 642 | ("c", style_1), |
| 643 | ("d", style_1), |
| 644 | ("e", style_1), |
| 645 | ("f", style_2), |
| 646 | ("g", style_2), |
| 647 | ("h", style_2), |
| 648 | ("i", style_2), |
| 649 | ] |
| 650 | .iter() |
| 651 | .map(|(a, b)| (a.to_string(), *b)) |
| 652 | .collect(); |
| 653 | |
| 654 | assert_eq!(resolve_style_references(edges), expected); |
| 655 | } |
| 656 | |
| 657 | #[test] |
| 658 | fn test_resolve_style_references_cycle() { |
| 659 | let edges: HashMap<&str, StyleReference> = [ |
| 660 | ("a", StyleReference::Reference("b".to_string())), |
| 661 | ("b", StyleReference::Reference("c".to_string())), |
| 662 | ("c", StyleReference::Reference("a".to_string())), |
| 663 | ] |
| 664 | .iter() |
| 665 | .map(|(a, b)| (*a, b.clone())) |
| 666 | .collect(); |
| 667 | |
| 668 | assert_eq!( |
| 669 | resolve_style_references(edges).keys().next().unwrap(), |
| 670 | "__cycle__" |
| 671 | ); |
| 672 | } |
| 673 | } |