diff options
Diffstat (limited to 'src/commands/discocaml.rs')
-rw-r--r-- | src/commands/discocaml.rs | 63 |
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) = ¬e { + 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() |