#![allow(dead_code)]
use serde_json::Value;
use super::time::iso_secs;
use super::{PageCursor, WINDOW};
use crate::{Error, Result};
pub(crate) fn invalid(what: &str, detail: impl Into<String>) -> Error {
Error::Api {
what: what.to_string(),
status: 422,
detail: detail.into(),
}
}
pub(crate) fn text(v: &Value, key: &str) -> String {
v.get(key)
.and_then(Value::as_str)
.unwrap_or_default()
.to_string()
}
pub(crate) fn opt_text(v: &Value, key: &str) -> Option<String> {
v.get(key)
.and_then(Value::as_str)
.filter(|s| !s.is_empty())
.map(str::to_string)
}
pub(crate) fn check_body(what: &str, body: &str, max: Option<usize>) -> Result<()> {
if body.trim().is_empty() {
return Err(invalid(what, "the comment is empty"));
}
if let Some(max) = max.filter(|m| body.chars().count() > *m) {
return Err(invalid(
what,
format!("the comment is longer than the {max} characters the host takes"),
));
}
Ok(())
}
pub(crate) fn require_issue(tracker: &str, key: &super::ItemKey) -> Result<()> {
if key.kind == super::ItemKind::Issue {
Ok(())
} else {
Err(crate::Error::Unsupported(format!(
"{tracker} has no pull requests"
)))
}
}
pub(crate) fn item_ref(project: &str, key: &super::ItemKey) -> String {
if key.id.trim().is_empty() {
format!("{}-{}", project.trim(), key.number)
} else {
key.id.trim().to_string()
}
}
pub(crate) fn window_of<T>(
mut all: Vec<T>,
cursor: Option<&PageCursor>,
) -> Result<(Vec<T>, Option<PageCursor>)> {
let end = match cursor {
None => all.len(),
Some(c) => {
c.0.parse::<usize>()
.map_err(|_| invalid("reading a conversation", "bad page cursor"))?
.min(all.len())
}
};
let start = end.saturating_sub(WINDOW);
all.truncate(end);
let window = all.split_off(start);
let older = (start > 0).then(|| PageCursor(start.to_string()));
Ok((window, older))
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct Span {
pub lower: Option<i64>,
pub upper: Option<i64>,
}
impl Span {
pub fn holds(&self, at: &str) -> bool {
let Some(t) = iso_secs(at) else {
return true;
};
self.lower.is_none_or(|l| t >= l) && self.upper.is_none_or(|u| t < u)
}
}
pub(crate) fn encode_cursor(position: &str, upper: Option<i64>) -> PageCursor {
PageCursor(match upper {
Some(u) => format!("{position}|{u}"),
None => position.to_string(),
})
}
pub(crate) fn decode_cursor(cursor: &PageCursor) -> Option<(String, Option<i64>)> {
match cursor.0.rsplit_once('|') {
Some((p, u)) => Some((p.to_string(), Some(u.parse().ok()?))),
None if cursor.0.is_empty() => None,
None => Some((cursor.0.clone(), None)),
}
}
pub(crate) fn span_of<'a>(
created: impl Iterator<Item = &'a str>,
has_older: bool,
upper: Option<i64>,
) -> Span {
let oldest = created.filter_map(iso_secs).min();
Span {
lower: if has_older { oldest } else { None },
upper,
}
}
pub(crate) fn oldest_secs<'a>(created: impl Iterator<Item = &'a str>) -> Option<i64> {
created.filter_map(iso_secs).min()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_short_history_is_one_window() {
let (w, older) = window_of(vec![1, 2, 3], None).unwrap();
assert_eq!(w, [1, 2, 3]);
assert_eq!(older, None);
}
#[test]
fn the_newest_window_comes_first_then_the_one_before() {
let all: Vec<usize> = (0..650).collect();
let (w, older) = window_of(all.clone(), None).unwrap();
assert_eq!((w.len(), w[0], w[299]), (WINDOW, 350, 649));
assert_eq!(older, Some(PageCursor("350".into())));
let (w, older) = window_of(all.clone(), older.as_ref()).unwrap();
assert_eq!((w[0], w[299]), (50, 349));
let (w, older) = window_of(all, older.as_ref()).unwrap();
assert_eq!((w.len(), w[0], w[49]), (50, 0, 49));
assert_eq!(older, None);
}
#[test]
fn a_bad_cursor_is_refused() {
assert!(window_of(vec![1], Some(&PageCursor("x".into()))).is_err());
}
#[test]
fn cursors_round_trip_and_spans_cut_events() {
let c = encode_cursor("abc", Some(100));
assert_eq!(decode_cursor(&c), Some(("abc".into(), Some(100))));
assert_eq!(
decode_cursor(&PageCursor("7".into())),
Some(("7".into(), None))
);
assert_eq!(decode_cursor(&PageCursor(String::new())), None);
let span = Span {
lower: Some(1_714_557_600),
upper: Some(1_714_561_200),
};
assert!(span.holds("2024-05-01T10:00:00Z"));
assert!(!span.holds("2024-05-01T09:59:59Z"));
assert!(!span.holds("2024-05-01T11:00:00Z"));
assert!(span.holds("not a time"));
}
}