Skip to content

Commit

Permalink
Add function to parse filename.
Browse files Browse the repository at this point in the history
  • Loading branch information
vikpe committed Jun 9, 2024
1 parent a884b90 commit e646467
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/filename.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use anyhow::{anyhow as e, Result};

use crate::serverinfo;

pub fn filename(data: &[u8]) -> Result<String> {
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(())
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +43,7 @@ mod client;
mod clientinfo;
mod clients;
mod duration;
mod filename;
mod flags;
mod frags;
mod ktxstats;
Expand Down

0 comments on commit e646467

Please sign in to comment.