aboutsummaryrefslogtreecommitdiff
path: root/src/commands/discocaml.rs
diff options
context:
space:
mode:
authorNathan Ringo <nathan@remexre.com>2024-01-19 23:06:59 -0600
committerNathan Ringo <nathan@remexre.com>2024-01-19 23:06:59 -0600
commit6bea013cc69b8d11c24fd1e6041677e8e9310f66 (patch)
tree36587659c57a21a2ded67a8f3cb28e4decc9d709 /src/commands/discocaml.rs
parente9ce0f5ca752f044716c17384bd7ef2486d74805 (diff)
"Run mode" done.
Diffstat (limited to 'src/commands/discocaml.rs')
-rw-r--r--src/commands/discocaml.rs63
1 files changed, 61 insertions, 2 deletions
diff --git a/src/commands/discocaml.rs b/src/commands/discocaml.rs
index ed00185..6defac0 100644
--- a/src/commands/discocaml.rs
+++ b/src/commands/discocaml.rs
@@ -72,10 +72,32 @@ pub enum DiscocamlCommand {
///
/// assert_eq!(out, expected);
/// ```
+///
+/// ```
+/// # use lambo::{commands::discocaml::*, utils::EnumAsArray};
+/// # use serde::Deserialize;
+/// # use serde_json::Deserializer;
+///
+/// let example = r#"["Exprs", "A", ["B", "C"]]"#;
+/// let expected = DiscocamlResponse::Exprs(
+/// "A".to_string(),
+/// vec![
+/// "B".to_string(),
+/// "C".to_string(),
+/// ],
+/// );
+///
+/// let mut de = Deserializer::from_str(&example);
+/// let out = DiscocamlResponse::deserialize(EnumAsArray(&mut de)).unwrap();
+/// de.end().unwrap();
+///
+/// assert_eq!(out, expected);
+/// ```
#[derive(Debug, Deserialize, PartialEq)]
#[serde(deny_unknown_fields)]
pub enum DiscocamlResponse {
Expr(DiscocamlResponseExpr),
+ Exprs(Option<String>, Vec<String>),
Error(String),
Graphviz(String),
}
@@ -196,6 +218,39 @@ where
send(CreateInteractionResponse::Message(res)).await?;
Ok(())
}
+ DiscocamlResponse::Exprs(note, exprs) => {
+ let mut msg = String::new();
+ if let Some(note) = &note {
+ msg.push_str(note);
+ msg.push('\n');
+ }
+
+ let mut msg_chars = msg.chars().count();
+ for expr in exprs {
+ let mut bullet = String::new();
+ bullet.push_str("1. ```ocaml\n");
+ for line in escape_code(&expr).lines() {
+ bullet.push_str(" ");
+ bullet.push_str(line);
+ bullet.push('\n');
+ }
+ bullet.push_str(" ```\n");
+ let bullet_chars = bullet.chars().count();
+ if msg_chars + bullet_chars > 1995 {
+ msg.push_str("1. …\n");
+ msg_chars += 5;
+ assert!(msg_chars < 2000);
+ break;
+ }
+ msg.push_str(&bullet);
+ msg_chars += bullet_chars;
+ }
+ assert_eq!(msg.chars().count(), msg_chars);
+
+ let msg = CreateInteractionResponseMessage::new().content(msg);
+ send(CreateInteractionResponse::Message(msg)).await?;
+ Ok(())
+ }
DiscocamlResponse::Graphviz(dot) => {
let png = match run_dot(&dot).await {
Ok(png) => png,
@@ -290,6 +345,11 @@ async fn run_dot(dot: &str) -> Result<BString> {
Ok(BString::new(output.stdout))
}
+fn escape_code(s: &str) -> String {
+ // TODO
+ s.to_string()
+}
+
fn expr_response_message(
expr: &DiscocamlResponseExpr,
rowid: i64,
@@ -300,8 +360,7 @@ fn expr_response_message(
msg.push('\n');
}
msg.push_str("```ocaml\n");
- // TODO: Escaping
- msg.push_str(&expr.expr);
+ msg.push_str(&escape_code(&expr.expr));
msg.push_str("```\n");
CreateInteractionResponseMessage::new()