Add match_str macro

CandyCorvid
Jul 13, 2023, 10:39 AM
XGDABCJNB4TBNPPDFFWXWU5NIEEDWB26GOETJMMEVEBWVCF2UWHAC

Dependencies

  • [2] ARXVO5XA Make connection generic on the network
  • [3] WGRFJRTE Add tests, rename Status to Header, implement redirect and proper input handling

Change contents

  • edit in src/media.rs at line 64
    [3.1996]
    [3.1996]
    }
    /// match on a string prefix, and bind the remainder to an identifier
    // TODO allow prefix-binding-suffix and binding-suffix as well.
    // No plans for infix strings, that would add ambiguity as well
    // as complicating the parser.
    // TODO Making this a TT muncher would simplify parsing and make it
    // easier to make new cases
    macro_rules! match_str {
    (($name:ident) $($s:expr , $rem:ident => $body:expr ,)* _ => $else:expr) => {
    $(if $name.starts_with($s) {
    let $rem = &$name[$s.len()..];
    $body
    } else)* {$else}
    };
  • edit in src/media.rs at line 80
    [3.1998]
    [3.1998]
  • replacement in src/media.rs at line 84
    [3.2096][2.770:887](),[2.887][3.2096:2653](),[3.2096][3.2096:2653]()
    // TODO make a macro that matches on e.g. `"pre":x => {}` and desugars to
    // the kind of slice below
    if line.starts_with("```") {
    // ignore anything after the lead chars on preformat lines
    return RawLine::Toggle {
    alt: TextLine(&line[3..]),
    };
    } else if line.starts_with("=> ") {
    let line = &line[3..];
    match line.split_once(' ') {
    Some((url, desc)) => {
    let url = url.trim_start_matches(' ');
    Line::Link {
    url: TextLine(url),
    description: Some(TextLine(desc)),
    [3.2096]
    [3.2653]
    match_str! {(line)
    "```", rem => {
    // ignore anything after the lead chars on preformat lines
    return RawLine::Toggle {
    alt: TextLine(rem),
    }
    },
    "=> ", rem => {
    match rem.split_once(' ') {
    Some((url, desc)) => {
    let url = url.trim_start_matches(' ');
    Line::Link {
    url: TextLine(url),
    description: Some(TextLine(desc)),
    }
  • edit in src/media.rs at line 100
    [3.2675]
    [3.2675]
    None => Line::Link {
    url: TextLine(rem),
    description: None,
    },
  • replacement in src/media.rs at line 105
    [3.2693][3.2693:2829]()
    None => Line::Link {
    url: TextLine(line),
    description: None,
    },
    [3.2693]
    [3.2829]
    },
    "* ", rem => {
    Line::ListItem(TextLine(rem))
    },
    "# ", rem => {
    Line::Heading {
    level: 1,
    title: TextLine(rem),
    }
    },
    "## ", rem => {
    Line::Heading {
    level: 2,
    title: TextLine(rem),
    }
    },
    "### ", rem => {
    Line::Heading {
    level: 3,
    title: TextLine(rem),
    }
    },
    ">", rem => {
    Line::Quote(TextLine(rem))
    },
    _ => {
    Line::Text(TextLine(line))
  • edit in src/media.rs at line 133
    [3.2843][3.2843:3542]()
    } else if line.starts_with("* ") {
    Line::ListItem(TextLine(&line[2..]))
    } else if line.starts_with("# ") {
    Line::Heading {
    level: 1,
    title: TextLine(&line[2..]),
    }
    } else if line.starts_with("## ") {
    Line::Heading {
    level: 2,
    title: TextLine(&line[3..]),
    }
    } else if line.starts_with("### ") {
    Line::Heading {
    level: 3,
    title: TextLine(&line[4..]),
    }
    } else if line.starts_with(">") {
    Line::Quote(TextLine(&line[1..]))
    } else {
    Line::Text(string)