From 41a49aaf11ab44df824e957d4947d4c427163c16 Mon Sep 17 00:00:00 2001 From: Sayan Nandan Date: Tue, 7 Sep 2021 09:32:02 -0700 Subject: [PATCH] Add `POP` subaction to `lmod` --- server/src/actions/lists/mod.rs | 39 +++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/server/src/actions/lists/mod.rs b/server/src/actions/lists/mod.rs index 0ad79efa..a3f7190e 100644 --- a/server/src/actions/lists/mod.rs +++ b/server/src/actions/lists/mod.rs @@ -39,6 +39,7 @@ const CLEAR: &[u8] = "CLEAR".as_bytes(); const PUSH: &[u8] = "PUSH".as_bytes(); const REMOVE: &[u8] = "REMOVE".as_bytes(); const INSERT: &[u8] = "INSERT".as_bytes(); +const POP: &[u8] = "POP".as_bytes(); macro_rules! listmap { ($tbl:expr, $con:expr) => { @@ -285,6 +286,44 @@ action! { conwrite!(con, groups::SERVER_ERR)?; } } + POP => { + err_if_len_is!(act, con, gt 1); + let idx = if act.len() == 1 { + // we have an idx + Some(get_numeric_count!()) + } else { + // no idx + None + }; + if registry::state_okay() { + let maybe_pop = listmap.get(listname).map(|list| { + let mut wlock = list.write(); + if let Some(idx) = idx { + if idx < wlock.len() { + // so we can pop + Some(wlock.remove(idx)) + } else { + None + } + } else { + wlock.pop() + } + }); + match maybe_pop { + Some(Some(val)) => { + unsafe { + writer::write_raw_mono(con, listmap.get_payload_tsymbol(), &val).await?; + } + } + Some(None) => { + conwrite!(con, groups::LISTMAP_BAD_INDEX)?; + } + None => conwrite!(con, groups::NIL)?, + } + } else { + conwrite!(con, groups::SERVER_ERR)?; + } + } _ => conwrite!(con, groups::UNKNOWN_ACTION)?, } Ok(())