Artemis 4 months ago
commit e6730a1226
Signed by: ArtemisNeko
GPG Key ID: 4813CB8D9C3F48AC
  1. 1
      .gitignore
  2. 49
      Cargo.lock
  3. 10
      Cargo.toml
  4. 123
      src/main.rs

1
.gitignore vendored

@ -0,0 +1 @@
/target

49
Cargo.lock generated

@ -0,0 +1,49 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "aho-corasick"
version = "0.7.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
dependencies = [
"memchr",
]
[[package]]
name = "gemrs"
version = "0.1.0"
dependencies = [
"lazy_static",
"regex",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "memchr"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
[[package]]
name = "regex"
version = "1.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d83f127d94bdbcda4c8cc2e50f6f84f4b611f69c902699ca385a39c3a75f9ff1"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.6.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49b3de9ec5dc0a3417da371aab17d729997c15010e7fd24ff707773a33bddb64"

@ -0,0 +1,10 @@
[package]
name = "gemrs"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
regex = "1"
lazy_static = "1.4.0"

@ -0,0 +1,123 @@
#![feature(assert_matches)]
extern crate lazy_static;
extern crate regex;
use lazy_static::lazy_static;
use regex::Regex;
#[derive(Debug)]
enum Modifier {
Normal,
Quoted,
ListItem,
}
#[derive(Debug)]
enum LineType {
Blank,
Text(String, Modifier),
Link { label: String, href: String },
PreToggle,
Header(String, usize),
}
fn get_type(input: &str) -> LineType {
if input.is_empty() {
return LineType::Blank;
}
if input.starts_with("```") {
return LineType::PreToggle;
}
lazy_static! {
static ref LINK: Regex = Regex::new(r"^=>[ \t]*([^ ]+)([ \t]+(.+))?").unwrap();
static ref HEADING: Regex = Regex::new(r"^(#{1,3}) (.+)").unwrap();
}
if let Some(captures) = LINK.captures(input) {
return LineType::Link {
label: String::from(captures.get(3).map_or("", |v| v.as_str().trim())),
href: String::from(captures.get(1).unwrap().as_str().trim()),
};
}
if let Some(captures) = HEADING.captures(input) {
return LineType::Header(
String::from(captures.get(2).unwrap().as_str().trim()),
captures.get(1).unwrap().as_str().len(),
);
}
LineType::Text(
String::from(input),
if input.starts_with("> ") {
Modifier::Quoted
} else if input.starts_with("* ") {
Modifier::ListItem
} else {
Modifier::Normal
},
)
}
#[cfg(test)]
mod tests {
use std::assert_matches::assert_matches;
use crate::{get_type, LineType, Modifier};
#[test]
fn heading() {
let res = get_type("#Not a heading");
assert_matches!(res, LineType::Text(_, _));
let res = get_type("#### Not a heading");
assert_matches!(res, LineType::Text(_, _));
let res = get_type("# H1");
assert_matches!(res, LineType::Header(_, 1));
let res = get_type("## H but 2");
assert_matches!(res, LineType::Header(_, 2));
let res = get_type("### H3");
assert_matches!(res, LineType::Header(_, 3));
}
#[test]
fn line() {
let res = get_type("Test bla");
assert_matches!(res, LineType::Text(x, Modifier::Normal) if x == "Test bla");
}
#[test]
fn empty() {
let res = get_type("");
assert_matches!(res, LineType::Blank);
}
#[test]
fn quote() {
let res = get_type("> This is a wise quote");
assert_matches!(res, LineType::Text(_, Modifier::Quoted));
let res = get_type(">this isn't");
assert_matches!(res, LineType::Text(_, Modifier::Normal));
}
#[test]
fn list() {
let res = get_type("* This is a list item");
assert_matches!(res, LineType::Text(_, Modifier::ListItem));
let res = get_type("*this isn't");
assert_matches!(res, LineType::Text(_, Modifier::Normal));
}
#[test]
fn link() {
let res = get_type("=> main.gmi");
assert_matches!(res, LineType::Link { label: x, href: y } if x.is_empty() && y == "main.gmi");
let res = get_type("=>main.gmi");
assert_matches!(res, LineType::Link { label: x, href: y } if x.is_empty() && y == "main.gmi");
let res = get_type("=> main.gmi");
assert_matches!(res, LineType::Link { label: x, href: y } if x.is_empty() && y == "main.gmi");
let res = get_type("=> main.gmi Test label");
assert_matches!(res, LineType::Link { label: x, href: y } if x == "Test label" && y == "main.gmi");
let res = get_type("=> main.gmi Test");
assert_matches!(res, LineType::Link { label: x, href: y } if x == "Test" && y == "main.gmi");
}
}
fn main() {}
Loading…
Cancel
Save