From 3cc1856d669eac8097fc064325d4fe53cbdb8175 Mon Sep 17 00:00:00 2001 From: Daniel Schadt Date: Sat, 6 Mar 2021 12:23:51 +0100 Subject: rework discord text insertion logic Equipped with the LogBag, we can now rework the "madness" that is the Discord `find_insertion` thing, even though it wasn't all too bad (and probably faster than the current version). --- src/discord.rs | 31 ++++++------------------------- src/logbag.rs | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 25 deletions(-) (limited to 'src') 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 { - 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 { + 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. @@ -178,6 +186,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(); -- cgit v1.2.3