Skip to content

Commit

Permalink
twiq: Add EventStream::split_first
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Nov 10, 2022
1 parent 3db4c14 commit 381401c
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions twiq/crates/event_store_core/src/event_stream.rs
Expand Up @@ -104,6 +104,10 @@ impl EventStream {
self.events.push(event);
Ok(())
}

pub fn split_first(&self) -> (&Event, &[Event]) {
self.events.split_first().expect("at least one exists")
}
}

#[cfg(test)]
Expand Down Expand Up @@ -287,4 +291,29 @@ mod tests {
.is_err());
Ok(())
}

#[test]
fn test_split_first() -> anyhow::Result<()> {
let mut stream = EventStream::generate(
EventType::from_str("created")?,
EventPayload::from_str("{}")?,
);
stream.push(
EventType::from_str("updated")?,
EventPayload::from_str("{}")?,
)?;
stream.push(
EventType::from_str("deleted")?,
EventPayload::from_str("{}")?,
)?;
let (head, tail) = stream.split_first();
assert_eq!(head.r#type().as_str(), "created");
assert_eq!(
tail.iter()
.map(|e| e.r#type().as_str())
.collect::<Vec<&str>>(),
vec!["updated", "deleted"]
);
Ok(())
}
}

0 comments on commit 381401c

Please sign in to comment.