|
1 | 1 | pub mod patch_cli;
|
2 | 2 |
|
| 3 | +use regex::Regex; |
3 | 4 | use std::borrow::Cow;
|
4 | 5 | use std::fs::File;
|
5 | 6 | use std::io::{Read, Write};
|
@@ -476,7 +477,10 @@ fn modify_dim_element<T: Clone>(
|
476 | 477 |
|
477 | 478 | fn make_field(fadd: &Hash, rpath: Option<&RegisterPath>) -> Result<FieldInfoBuilder> {
|
478 | 479 | let mut fnew = FieldInfo::builder()
|
479 |
| - .description(opt_interpolate(&rpath, fadd.get_str("description")?)) |
| 480 | + .description(opt_interpolate( |
| 481 | + &rpath, |
| 482 | + fadd.get_str("description").unwrap_or(None), |
| 483 | + )) |
480 | 484 | .derived_from(opt_interpolate(&rpath, fadd.get_str("derivedFrom")?))
|
481 | 485 | .access(fadd.get_str("access")?.and_then(Access::parse_str))
|
482 | 486 | .modified_write_values(
|
@@ -509,7 +513,10 @@ fn make_field(fadd: &Hash, rpath: Option<&RegisterPath>) -> Result<FieldInfoBuil
|
509 | 513 | fn make_register(radd: &Hash, path: Option<&BlockPath>) -> Result<RegisterInfoBuilder> {
|
510 | 514 | let mut rnew = RegisterInfo::builder()
|
511 | 515 | .display_name(radd.get_string("displayName")?)
|
512 |
| - .description(opt_interpolate(&path, radd.get_str("description")?)) |
| 516 | + .description(opt_interpolate( |
| 517 | + &path, |
| 518 | + radd.get_str("description").unwrap_or(None), |
| 519 | + )) |
513 | 520 | .derived_from(opt_interpolate(&path, radd.get_str("derivedFrom")?))
|
514 | 521 | .alternate_group(radd.get_string("alternateGroup")?)
|
515 | 522 | .alternate_register(radd.get_string("alternateRegister")?)
|
@@ -576,7 +583,10 @@ fn get_write_constraint(h: &Hash) -> Result<Option<WriteConstraint>> {
|
576 | 583 |
|
577 | 584 | fn make_cluster(cadd: &Hash, path: Option<&BlockPath>) -> Result<ClusterInfoBuilder> {
|
578 | 585 | let mut cnew = ClusterInfo::builder()
|
579 |
| - .description(opt_interpolate(&path, cadd.get_str("description")?)) |
| 586 | + .description(opt_interpolate( |
| 587 | + &path, |
| 588 | + cadd.get_str("description").unwrap_or(None), |
| 589 | + )) |
580 | 590 | .derived_from(opt_interpolate(&path, cadd.get_str("derivedFrom")?))
|
581 | 591 | .default_register_properties(get_register_properties(cadd)?);
|
582 | 592 |
|
@@ -622,7 +632,7 @@ fn make_peripheral(padd: &Hash, modify: bool) -> Result<PeripheralInfoBuilder> {
|
622 | 632 | let mut pnew = PeripheralInfo::builder()
|
623 | 633 | .display_name(padd.get_string("displayName")?)
|
624 | 634 | .version(padd.get_string("version")?)
|
625 |
| - .description(padd.get_string("description")?) |
| 635 | + .description(padd.get_string("description").unwrap_or(None)) |
626 | 636 | .derived_from(padd.get_string("derivedFrom")?)
|
627 | 637 | .group_name(padd.get_string("groupName")?)
|
628 | 638 | .interrupt(if !modify {
|
@@ -767,6 +777,30 @@ fn check_offsets(offsets: &[u32], dim_increment: u32) -> bool {
|
767 | 777 | true
|
768 | 778 | }
|
769 | 779 |
|
| 780 | +fn do_replacements<'a>(s: &'a str, h: &Hash) -> Result<Cow<'a, str>> { |
| 781 | + let mut curr = Cow::Borrowed(s); |
| 782 | + for (pat, rep) in h { |
| 783 | + let pat = pat.str()?; |
| 784 | + let re = Regex::new(pat).unwrap(); |
| 785 | + match rep { |
| 786 | + Yaml::String(rep) => { |
| 787 | + curr = re.replace_all(curr.as_ref(), rep).to_string().into(); |
| 788 | + } |
| 789 | + Yaml::Array(v) if v.len() == 2 => { |
| 790 | + let n = v[0].i64()?; |
| 791 | + let rep = v[1].str()?; |
| 792 | + curr = re.replacen(curr.as_ref(), n as _, rep).to_string().into(); |
| 793 | + } |
| 794 | + _ => { |
| 795 | + return Err(anyhow!( |
| 796 | + "Replacer should be string or number of replacements and string" |
| 797 | + )) |
| 798 | + } |
| 799 | + } |
| 800 | + } |
| 801 | + Ok(curr) |
| 802 | +} |
| 803 | + |
770 | 804 | /// Tries to get common description (or displayNames) for register/field array with "%s" in index position.
|
771 | 805 | /// Returns `None` if incoming descriptions have more then 1 difference
|
772 | 806 | fn common_description(descs: &[Option<&str>], dim_index: &[String]) -> Option<Option<String>> {
|
|
0 commit comments