use crate::types::StreamCategory;
const RETRYABLE_STATUSES: &[u16] = &[408, 425, 429, 500, 502, 503, 504];
const GEOBLOCK_STATUSES: &[u16] = &[403, 426, 451];
const SECONDARY_GEOBLOCK_STATUSES: &[u16] = &[401, 423, 451];
pub fn categorize_status(status_code: u16) -> StreamCategory {
if (200..300).contains(&status_code) {
return StreamCategory::Alive;
}
if GEOBLOCK_STATUSES.contains(&status_code) {
return StreamCategory::Geoblocked;
}
if RETRYABLE_STATUSES.contains(&status_code) {
return StreamCategory::Retry;
}
if SECONDARY_GEOBLOCK_STATUSES.contains(&status_code) {
return StreamCategory::Geoblocked;
}
StreamCategory::Dead
}
pub fn meets_data_threshold(bytes_received: u64, min_bytes: u64) -> bool {
bytes_received >= min_bytes
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn categorize_200_as_alive() {
assert_eq!(categorize_status(200), StreamCategory::Alive);
}
#[test]
fn categorize_204_as_alive() {
assert_eq!(categorize_status(204), StreamCategory::Alive);
}
#[test]
fn categorize_403_as_geoblocked() {
assert_eq!(categorize_status(403), StreamCategory::Geoblocked);
}
#[test]
fn categorize_451_as_geoblocked() {
assert_eq!(categorize_status(451), StreamCategory::Geoblocked);
}
#[test]
fn categorize_426_as_geoblocked() {
assert_eq!(categorize_status(426), StreamCategory::Geoblocked);
}
#[test]
fn categorize_401_as_geoblocked_secondary() {
assert_eq!(categorize_status(401), StreamCategory::Geoblocked);
}
#[test]
fn categorize_423_as_geoblocked_secondary() {
assert_eq!(categorize_status(423), StreamCategory::Geoblocked);
}
#[test]
fn categorize_429_as_retry() {
assert_eq!(categorize_status(429), StreamCategory::Retry);
}
#[test]
fn categorize_500_as_retry() {
assert_eq!(categorize_status(500), StreamCategory::Retry);
}
#[test]
fn categorize_502_as_retry() {
assert_eq!(categorize_status(502), StreamCategory::Retry);
}
#[test]
fn categorize_503_as_retry() {
assert_eq!(categorize_status(503), StreamCategory::Retry);
}
#[test]
fn categorize_504_as_retry() {
assert_eq!(categorize_status(504), StreamCategory::Retry);
}
#[test]
fn categorize_404_as_dead() {
assert_eq!(categorize_status(404), StreamCategory::Dead);
}
#[test]
fn categorize_410_as_dead() {
assert_eq!(categorize_status(410), StreamCategory::Dead);
}
#[test]
fn data_threshold_met() {
assert!(meets_data_threshold(512_000, 512_000));
assert!(meets_data_threshold(600_000, 512_000));
}
#[test]
fn data_threshold_not_met() {
assert!(!meets_data_threshold(100_000, 512_000));
assert!(!meets_data_threshold(0, 131_072));
}
}