steam-user 0.1.0

Steam User web client for Rust - HTTP-based Steam Community interactions
Documentation
/// Notification counts from Steam.
#[derive(Debug, Clone, Default, serde::Serialize, serde::Deserialize)]
pub struct Notifications {
    /// Trade offer notifications.
    pub trades: u32,
    /// Game turns (for turn-based games).
    pub game_turns: u32,
    /// Moderator messages.
    pub moderator_messages: u32,
    /// Profile comments.
    pub comments: u32,
    /// New items.
    pub items: u32,
    /// Group/friend invites.
    pub invites: u32,
    /// Gift notifications.
    pub gifts: u32,
    /// Chat messages.
    pub chat: u32,
    /// Help request replies.
    pub help_request_replies: u32,
    /// Account alerts.
    pub account_alerts: u32,
}

/// Raw shape returned by Steam's `GetNotificationCounts` endpoint.
///
/// Steam sends a JSON object with **string keys** like `"1"`, `"2"`, … `"11"`
/// mapping to integer counts. This struct mirrors that wire format with
/// `#[serde(rename = "...")]` and feeds [`NotificationCountsEnvelope`] which
/// is then narrowed into the public [`Notifications`] type.
///
/// Missing keys default to `0`. Counts are stored as `u64` so that values
/// exceeding `u32::MAX` (extremely unlikely but technically possible on the
/// wire) can be detected and saturated, rather than silently truncated.
#[derive(Debug, Clone, Default, serde::Deserialize)]
#[serde(default)]
pub struct RawNotificationCounts {
    #[serde(rename = "1")]
    pub trades: u64,
    #[serde(rename = "2")]
    pub game_turns: u64,
    #[serde(rename = "3")]
    pub moderator_messages: u64,
    #[serde(rename = "4")]
    pub comments: u64,
    #[serde(rename = "5")]
    pub items: u64,
    #[serde(rename = "6")]
    pub invites: u64,
    #[serde(rename = "8")]
    pub gifts: u64,
    #[serde(rename = "9")]
    pub chat: u64,
    #[serde(rename = "10")]
    pub help_request_replies: u64,
    #[serde(rename = "11")]
    pub account_alerts: u64,
}

impl From<RawNotificationCounts> for Notifications {
    fn from(raw: RawNotificationCounts) -> Self {
        // Saturate at u32::MAX rather than wrapping/truncating (no `as` casts).
        let narrow = |v: u64| u32::try_from(v).unwrap_or(u32::MAX);
        Self {
            trades: narrow(raw.trades),
            game_turns: narrow(raw.game_turns),
            moderator_messages: narrow(raw.moderator_messages),
            comments: narrow(raw.comments),
            items: narrow(raw.items),
            invites: narrow(raw.invites),
            gifts: narrow(raw.gifts),
            chat: narrow(raw.chat),
            help_request_replies: narrow(raw.help_request_replies),
            account_alerts: narrow(raw.account_alerts),
        }
    }
}

/// Envelope wrapping the `notifications` object inside the
/// `GetNotificationCounts` response.
#[derive(Debug, Clone, Default, serde::Deserialize)]
#[serde(default)]
pub struct NotificationCountsEnvelope {
    pub notifications: RawNotificationCounts,
}