diff --git a/src/filename.rs b/src/filename.rs new file mode 100644 index 0000000..5c46b19 --- /dev/null +++ b/src/filename.rs @@ -0,0 +1,68 @@ +use anyhow::{anyhow as e, Result}; + +use crate::serverinfo; + +pub fn filename(data: &[u8]) -> Result { + let Some(serverdemo) = serverinfo(data)?.serverdemo else { + return Err(e!("Filename not found")); + }; + Ok(serverdemo) +} + +#[cfg(test)] +mod tests { + use std::fs::read; + + use anyhow::Result; + use pretty_assertions::assert_eq; + + use super::*; + + #[test] + fn test_filename() -> Result<()> { + assert_eq!( + filename(&read( + "tests/files/2on2_sf_vs_red[frobodm2]220104-0915.mvd" + )?)?, + "2on2_sf_vs_red[frobodm2]220104-0915.mvd".to_string() + ); + + assert_eq!( + filename(&read("tests/files/4on4_oeks_vs_tsq[dm2]20240426-1716.mvd")?)?, + "4on4_oeks_vs_tsq[dm2]20240426-1716.mvd".to_string() + ); + + assert_eq!( + filename(&read("tests/files/ctf_blue_vs_red[ctf5]20240520-1925.mvd")?)?, + "ctf_blue_vs_red[ctf5]20240520-1925.mvd".to_string() + ); + + assert_eq!( + filename(&read( + "tests/files/duel_equ_vs_kaboom[povdmm4]20240422-1038.mvd" + )?)?, + "duel_equ_vs_kaboom[povdmm4]20240422-1038.mvd".to_string() + ); + + assert_eq!( + filename(&read( + "tests/files/duel_holy_vs_dago[bravado]20240426-1659.mvd" + )?)?, + "duel_holy_vs_dago[bravado]20240426-1659.mvd".to_string() + ); + + assert_eq!( + filename(&read("tests/files/ffa_5[dm4]20240501-1229.mvd")?)?, + "ffa_5[dm4]20240501-1229.mvd".to_string() + ); + + assert_eq!( + filename(&read( + "tests/files/wipeout_red_vs_blue[q3dm6qw]20240406-2028.mvd" + )?)?, + "wipeout_red_vs_blue[q3dm6qw]20240406-2028.mvd".to_string() + ); + + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs index 722c1b4..5c930ec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,6 +8,7 @@ pub use team::Team; pub use crate::clientinfo::clientinfo; pub use crate::duration::{countdown_duration, demo_duration, match_duration}; +pub use crate::filename::filename; pub use crate::frags::frags_per_player_name; pub use crate::ktxstats::{ktxstats_string, ktxstats_v3}; pub use crate::players::players; @@ -42,6 +43,7 @@ mod client; mod clientinfo; mod clients; mod duration; +mod filename; mod flags; mod frags; mod ktxstats;