~julienxx/castor

4a5b575001a50036d2d8230c23ac20e4ac53ca46 — Julien Blanchard 16 days ago 2451228
Simpler parsing
2 files changed, 25 insertions(+), 45 deletions(-)

M src/gemini/parser.rs
M src/gopher/parser.rs
M src/gemini/parser.rs => src/gemini/parser.rs +19 -38
@@ 1,8 1,3 @@
extern crate regex;
use regex::Regex;

use crate::colors::*;

use std::str::FromStr;

#[derive(Debug)]


@@ 20,47 15,33 @@ pub enum TextElement {
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ParseError;

const H1_REGEX: &str = r"^#\s+(.*)$";
const H2_REGEX: &str = r"^##\s+(.*)$";
const H3_REGEX: &str = r"^###\s+(.*)$";
const LIST_ITEM_REGEX: &str = r"^\*\s+([^*]*)$";
const LINK_ITEM_REGEX: &str = r"^=>\s*(\S*)\s*(.*)?$";

impl FromStr for TextElement {
    type Err = ParseError;

    // Parses a &str into an instance of 'TextElement'
    fn from_str(line: &str) -> Result<TextElement, ParseError> {
        let h1_regexp = Regex::new(H1_REGEX).unwrap();
        let h2_regexp = Regex::new(H2_REGEX).unwrap();
        let h3_regexp = Regex::new(H3_REGEX).unwrap();
        let list_item_regexp = Regex::new(LIST_ITEM_REGEX).unwrap();
        let link_item_regexp = Regex::new(LINK_ITEM_REGEX).unwrap();

        if h1_regexp.is_match(&line) {
            let caps = h1_regexp.captures(&line).unwrap();
            let header = caps.get(1).map_or("", |m| m.as_str());
            Ok(TextElement::H1(String::from(header)))
        } else if h2_regexp.is_match(&line) {
            let caps = h2_regexp.captures(&line).unwrap();
            let header = caps.get(1).map_or("", |m| m.as_str());
            Ok(TextElement::H2(String::from(header)))
        } else if h3_regexp.is_match(&line) {
            let caps = h3_regexp.captures(&line).unwrap();
            let header = caps.get(1).map_or("", |m| m.as_str());
            Ok(TextElement::H3(String::from(header)))
        } else if list_item_regexp.is_match(&line) {
            let caps = list_item_regexp.captures(&line).unwrap();
            let header = caps.get(1).map_or("", |m| m.as_str());
            Ok(TextElement::ListItem(String::from(header)))
        } else if link_item_regexp.is_match(&line) {
        if line.starts_with("###") {
            let text = line.split_at(3).1.trim();
            Ok(TextElement::H3(String::from(text)))
        } else if line.starts_with("##") {
            let text = line.split_at(2).1.trim();
            Ok(TextElement::H2(String::from(text)))
        } else if line.starts_with("#") {
            let text = line.split_at(1).1.trim();
            Ok(TextElement::H1(String::from(text)))
        } else if line.starts_with("*") {
            let text = line.split_at(1).1.trim();
            Ok(TextElement::ListItem(String::from(text)))
        } else if line.starts_with("=>") {
            Ok(TextElement::LinkItem(String::from(line)))
        } else if line.starts_with("```") {
            Ok(TextElement::MonoText(String::from(line)))
        } else if line.starts_with("> ") {
            Ok(TextElement::Quote(String::from(line)))
            let text = line.split_at(3).1.trim();
            Ok(TextElement::MonoText(String::from(text)))
        } else if line.starts_with(">") {
            let text = line.split_at(1).1.trim();
            Ok(TextElement::Quote(String::from(text)))
        } else {
            Ok(TextElement::Text(colors::colorize(line)))
            Ok(TextElement::Text(String::from(line)))
        }
    }
}

M src/gopher/parser.rs => src/gopher/parser.rs +6 -7
@@ 1,4 1,3 @@
use crate::colors::*;
use std::str::FromStr;

#[derive(Debug)]


@@ 22,21 21,21 @@ impl FromStr for TextElement {
        if els.count() >= 2 {
            // Gophermap line
            if line.starts_with('0') || line.starts_with('1') {
                Ok(TextElement::LinkItem(colors::cleanup(line)))
                Ok(TextElement::LinkItem(String::from(line)))
            } else if line.starts_with('i') {
                let mut els = line.split('\t');
                let text = els.next().expect("");
                let mut text = String::from(text);
                text.remove(0);
                Ok(TextElement::Text(colors::colorize(&text)))
                Ok(TextElement::Text(text))
            } else if line.starts_with('h') {
                Ok(TextElement::ExternalLinkItem(colors::cleanup(line)))
                Ok(TextElement::ExternalLinkItem(String::from(line)))
            } else if line.starts_with('I') || line.starts_with('g') {
                Ok(TextElement::Image(colors::cleanup(line)))
                Ok(TextElement::Image(String::from(line)))
            } else if line.starts_with('9') {
                Ok(TextElement::Binary(String::from(line)))
            } else {
                Ok(TextElement::Text(colors::colorize(line)))
                Ok(TextElement::Text(String::from(line)))
            }
        } else {
            // Text line


@@ 50,7 49,7 @@ impl FromStr for TextElement {
            {
                Ok(TextElement::ExternalLinkItem(String::from(line)))
            } else {
                Ok(TextElement::Text(colors::colorize(line)))
                Ok(TextElement::Text(String::from(line)))
            }
        }
    }