The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Basic Link Parsing In Rust's nom Crate

TODO: Pull subtitle into page object
Code
use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_until;
use nom::combinator::eof;
use nom::combinator::rest;
use nom::multi::many_till;
use nom::sequence::delimited;
use nom::IResult;

fn main() {
    let source = r#"some <<link|example|https://www.example.com/>> link"#;
    let (_, parts) = parse(source).unwrap();
    dbg!(parts);
}

fn parse(source: &str) -> IResult<&str, Vec<String>> {
    let (source, texts) = many_till(get_parts, eof)(source)?;
    Ok((source, texts.0))
}

fn get_parts(source: &str) -> IResult<&str, String> {
    dbg!(&source);
    let (source, value) = alt((
        delimited(tag("<<"), take_until(">>"), tag(">>")),
        take_until("<<"),
        rest,
    ))(source)?;
    Ok((source, value.to_string()))
}
Results
[src/bin/nom_examples.rs:22] &source = "some <<link|example|https://www.example.com/>> link"
[src/bin/nom_examples.rs:22] &source = "<<link|example|https://www.example.com/>> link"          
[src/bin/nom_examples.rs:22] &source = " link"
[src/bin/nom_examples.rs:13] parts = [
    "some ",
    "link|example|https://www.example.com/",
    " link",
]


Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title 

Basic Link Parsing In Rust's nom Crate 

This is the start of the code I'm using to parse
out links for Neopolitan. Next up is to send
info about the type along with the code that
gets returned to allow for further processing. 

-- code 
-- rust

use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::bytes::complete::take_until;
use nom::combinator::eof;
use nom::combinator::rest;
use nom::multi::many_till;
use nom::sequence::delimited;
use nom::IResult;

fn main() {
    let source = r#"some <<link|example|https://www.example.com/>> link"#;
    let (_, parts) = parse(source).unwrap();
    dbg!(parts);
}

fn parse(source: &str) -> IResult<&str, Vec<String>> {
    let (source, texts) = many_till(get_parts, eof)(source)?;
    Ok((source, texts.0))
}

fn get_parts(source: &str) -> IResult<&str, String> {
    dbg!(&source);
    let (source, value) = alt((
        delimited(tag("<<"), take_until(">>"), tag(">>")),
        take_until("<<"),
        rest,
    ))(source)?;
    Ok((source, value.to_string()))
}


-- results 

[src/bin/nom_examples.rs:22] &source = "some <<link|example|https://www.example.com/>> link"
[src/bin/nom_examples.rs:22] &source = "<<link|example|https://www.example.com/>> link"          
[src/bin/nom_examples.rs:22] &source = " link"
[src/bin/nom_examples.rs:13] parts = [
    "some ",
    "link|example|https://www.example.com/",
    " link",
]




-- categories 
-- Rust
-- Nom
-- Neopolitan 

-- metadata 
-- date: 2023-03-13 21:45:10
-- id: 2mz6xzsf
-- site: aws
-- type: post
-- status: published