aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/discord.rs31
-rw-r--r--src/logbag.rs31
2 files changed, 37 insertions, 25 deletions
diff --git a/src/discord.rs b/src/discord.rs
index 4d59580..ea0ea36 100644
--- a/src/discord.rs
+++ b/src/discord.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
use anyhow::Result;
use chrono::prelude::*;
-use evtclib::{Log, Outcome};
+use evtclib::Log;
use serenity::client::bridge::gateway::ShardManager;
use serenity::model::id::*;
use serenity::prelude::*;
@@ -11,6 +11,7 @@ use tokio::runtime::Runtime;
use log::info;
use super::categories::Categorizable;
+use super::logbag::{LogBag, state_emoji};
const MAX_HOURS: i64 = 5;
const MAX_MESSAGE_LENGTH: usize = 2000;
@@ -111,29 +112,9 @@ pub fn post_link(discord_token: &str, channel_id: u64, log: &Log, link: &str) ->
})
}
-fn find_insertion(text: &str, category: &str) -> Option<usize> {
- let cat_pos = text.find(&format!("**{}**", category))?;
- let empty_line = text[cat_pos..].find("\n\n")?;
- Some(cat_pos + empty_line + 1)
-}
-
fn insert_link(text: &str, log: &Log, link: &str) -> String {
- let mut text = format!("\n\n{}\n\n", text);
- let point = find_insertion(&text, log.category());
- let link_line = format!("{} {}\n", state_emoji(log), link);
- if let Some(i) = point {
- text.insert_str(i, &link_line);
- } else {
- text.push_str(&format!("**{}**\n{}", log.category(), link_line));
- }
- text.trim().into()
-}
-
-fn state_emoji(log: &Log) -> &'static str {
- let outcome = log.analyzer().and_then(|a| a.outcome());
- match outcome {
- Some(Outcome::Success) => "✅",
- Some(Outcome::Failure) => "❌",
- None => "❓",
- }
+ let mut logbag = LogBag::parse_markdown(text).unwrap();
+ let line = format!("{} {}", state_emoji(log), link);
+ logbag.insert(log.category(), line);
+ logbag.render_markdown()
}
diff --git a/src/logbag.rs b/src/logbag.rs
index f461007..b08098a 100644
--- a/src/logbag.rs
+++ b/src/logbag.rs
@@ -52,6 +52,14 @@ impl LogBag {
input.parse().ok()
}
+ pub fn parse_markdown(input: &str) -> Option<LogBag> {
+ let plain = input
+ .split('\n')
+ .map(|line| line.trim_matches('*'))
+ .join("\n");
+ LogBag::parse_plain(&plain)
+ }
+
/// Renders the contents of this [`LogBag`] as plain text.
///
/// The output of this can be fed back into [`LogBag::parse_plain`] to round-trip.
@@ -179,6 +187,29 @@ line 2"
}
#[test]
+ fn parse_markdown() {
+ let mut logbag = LogBag::new();
+ logbag.insert("cat 1", "line 1".to_string());
+ logbag.insert("cat 1", "line 2".to_string());
+ logbag.insert("cat 2", "line 1".to_string());
+ logbag.insert("cat 2", "line 2".to_string());
+
+ assert_eq!(
+ LogBag::parse_markdown(
+ "\
+**cat 1**
+line 1
+line 2
+
+**cat 2**
+line 1
+line 2"
+ ),
+ Some(logbag)
+ );
+ }
+
+ #[test]
fn render_plain_single() {
let mut logbag = LogBag::new();
logbag.insert("category", "line 1".to_string());