steam-user 0.1.0

Steam User web client for Rust - HTTP-based Steam Community interactions
Documentation
//! QR code utility functions.

/// Decode a Steam login QR URL to extract client ID and version.
///
/// Expected format: `https://s.team/q/{client_id}/{version}`
pub fn decode_login_qr_url(qr_url: &str) -> Option<(u64, i32)> {
    if !qr_url.starts_with("https://s.team/q/") && !qr_url.starts_with("http://s.team/q/") {
        return None;
    }

    let parts: Vec<&str> = qr_url.split('/').collect();
    // https://s.team/q/CLIENT_ID/VERSION
    // ["https:", "", "s.team", "q", "CLIENT_ID", "VERSION"] -> len 6
    // http://s.team/q/CLIENT_ID/VERSION -> len 6
    // trailing slash?

    // Let's look for "q" and take the next two
    let mut iter = parts.iter().skip_while(|&&part| part != "q");
    iter.next()?; // skip "q"

    let client_id_str = iter.next()?;
    let version_str = iter.next()?;

    // Clean up version string (might have query params or generic "1?foo=bar")
    let version_clean: String = version_str.chars().take_while(|c| c.is_ascii_digit()).collect();

    let client_id = client_id_str.parse::<u64>().ok()?;
    let version = version_clean.parse::<i32>().ok()?;

    Some((client_id, version))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_decode_login_qr_url() {
        let url = "https://s.team/q/12345/1";
        assert_eq!(decode_login_qr_url(url), Some((12345, 1)));

        let url_with_query = "https://s.team/q/12345/1?foo=bar";
        assert_eq!(decode_login_qr_url(url_with_query), Some((12345, 1)));

        let invalid = "https://google.com";
        assert_eq!(decode_login_qr_url(invalid), None);
    }
}