Skip to content

Commit 099b2a3

Browse files
committed
Port add meta notification command to Clap
1 parent 5b52242 commit 099b2a3

File tree

2 files changed

+18
-22
lines changed

2 files changed

+18
-22
lines changed

src/zulip.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,8 @@ use crate::zulip::client::ZulipClient;
1717
use crate::zulip::commands::{ChatCommand, LookupCmd, WorkqueueCmd, WorkqueueLimit};
1818
use anyhow::{format_err, Context as _};
1919
use clap::Parser;
20-
use postgres_types::ToSql;
2120
use rust_team_data::v1::TeamKind;
2221
use std::fmt::Write as _;
23-
use std::str::FromStr;
2422
use subtle::ConstantTimeEq;
2523
use tracing as log;
2624

@@ -199,12 +197,15 @@ fn handle_command<'a>(
199197
let cmd = ChatCommand::try_parse_from(words)?;
200198
match cmd {
201199
ChatCommand::Acknowledge { identifier } => {
202-
acknowledge(&ctx, gh_id, identifier.into()).await
200+
acknowledge(&ctx, gh_id, (&identifier).into()).await
203201
}
204202
ChatCommand::Add { url, description } => {
205203
add_notification(&ctx, gh_id, &url, &description.join(" ")).await
206204
}
207205
ChatCommand::Move { from, to } => move_notification(&ctx, gh_id, from, to).await,
206+
ChatCommand::Meta { index, description } => {
207+
add_meta_notification(&ctx, gh_id, index, &description.join(" ")).await
208+
}
208209
ChatCommand::Whoami => whoami_cmd(&ctx, gh_id).await,
209210
ChatCommand::Lookup(cmd) => lookup_cmd(&ctx, cmd).await,
210211
ChatCommand::Work(cmd) => workqueue_commands(ctx, gh_id, cmd).await,
@@ -699,15 +700,15 @@ impl<'a> UpdateMessageApiRequest<'a> {
699700
}
700701
}
701702

702-
async fn acknowledge(
703+
async fn acknowledge<'a>(
703704
ctx: &Context,
704705
gh_id: u64,
705-
ident: Identifier,
706+
ident: Identifier<'a>,
706707
) -> anyhow::Result<Option<String>> {
707708
let mut db = ctx.db.get().await;
708709
let deleted = delete_ping(&mut *db, gh_id, ident)
709710
.await
710-
.map_err(|e| format_err!("Failed to acknowledge {filter}: {e:?}."))?;
711+
.map_err(|e| format_err!("Failed to acknowledge {ident:?}: {e:?}."))?;
711712

712713
let resp = if deleted.is_empty() {
713714
format!("No notifications matched `{ident:?}`, so none were deleted.")
@@ -765,27 +766,16 @@ async fn add_notification(
765766
async fn add_meta_notification(
766767
ctx: &Context,
767768
gh_id: u64,
768-
mut words: impl Iterator<Item = &str>,
769+
idx: u32,
770+
description: &str,
769771
) -> anyhow::Result<Option<String>> {
770-
let idx = match words.next() {
771-
Some(idx) => idx,
772-
None => anyhow::bail!("idx not present"),
773-
};
774772
let idx = idx
775-
.parse::<u32>()
776-
.context("index")?
777773
.checked_sub(1)
778774
.ok_or_else(|| anyhow::anyhow!("1-based indexes"))?;
779-
let mut description = words.fold(String::new(), |mut acc, piece| {
780-
acc.push_str(piece);
781-
acc.push(' ');
782-
acc
783-
});
784775
let description = if description.is_empty() {
785776
None
786777
} else {
787-
assert_eq!(description.pop(), Some(' ')); // pop trailing space
788-
Some(description)
778+
Some(description.to_string())
789779
};
790780
let mut db = ctx.db.get().await;
791781
match add_metadata(&mut db, gh_id, idx, description.as_deref()).await {

src/zulip/commands.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ pub enum ChatCommand {
1414
/// Add a notification
1515
Add {
1616
url: String,
17-
#[clap(trailing_var_args(true))]
17+
#[clap(trailing_var_arg(true))]
1818
description: Vec<String>,
1919
},
2020
/// Move a notification
2121
Move { from: u32, to: u32 },
22+
/// Add meta notification
23+
Meta {
24+
index: u32,
25+
#[clap(trailing_var_arg(true))]
26+
description: Vec<String>,
27+
},
2228
/// Output your membership in Rust teams.
2329
Whoami,
2430
/// Perform lookup of GitHub or Zulip username.
@@ -120,7 +126,7 @@ impl FromStr for IdentifierCli {
120126
"all" | "*" => Ok(Self::All),
121127
v => match v.parse::<u32>() {
122128
Ok(v) => NonZeroU32::new(v)
123-
.ok_or_else(|e| "index must be at least 1".to_string())
129+
.ok_or_else(|| "index must be at least 1".to_string())
124130
.map(Self::Index),
125131
Err(_) => Ok(Self::Url(v.to_string())),
126132
},

0 commit comments

Comments
 (0)