rmqtt-http-api 0.23.0

This plugin provides HTTP APIs for integration with external systems, enabling operations like querying client information and publishing messages.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
//! Shared types for the HTTP API plugin.
//!
//! Defines gRPC [`Message`] / [`MessageReply`] enums, API parameter structs
//! ([`ClientSearchParams`], [`PublishParams`], [`SubscribeParams`],
//! [`UnsubscribeParams`]), result types ([`ClientSearchResult`]), and
//! [`PrometheusDataType`].

use std::time::Duration;

use anyhow::anyhow;
use base64::prelude::{Engine, BASE64_STANDARD};
use serde::{de, ser, Deserialize, Serialize};

use rmqtt::types::NodeHealthStatus;
use rmqtt::{
    codec::v5::PublishProperties,
    metrics::Metrics,
    node::{BrokerInfo, NodeInfo, NodeStatus},
    plugin::PluginInfo,
    stats::Stats,
    types::{
        ClientId, From, HashMap, MsgID, NodeId, Publish, QoS, Retain, Timestamp, TopicFilter, TopicName,
        UserName,
    },
    utils::{deserialize_datetime_option, format_timestamp, serialize_datetime_option},
    Result,
};

/// gRPC messages for the HTTP API plugin.
///
/// Each variant corresponds to a request type that can be forwarded to
/// remote nodes.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum Message<'a> {
    BrokerInfo,
    NodeInfo,
    NodeHealthStatus,
    StatsInfo,
    MetricsInfo,
    ClientSearch(Box<ClientSearchParams>),
    ClientGet {
        clientid: &'a str,
    },
    Subscribe(SubscribeParams),
    Unsubscribe(UnsubscribeParams),
    GetPlugins,
    GetPlugin {
        name: &'a str,
    },
    GetPluginConfig {
        name: &'a str,
    },
    ReloadPluginConfig {
        name: &'a str,
    },
    LoadPlugin {
        name: &'a str,
    },
    UnloadPlugin {
        name: &'a str,
    },
    // ── History query messages ─────────────────────────────────────────
    /// Query another node's Stats history
    StatsHistoryQuery(HistoryQuery),
    /// Query another node's Metrics history
    MetricsHistoryQuery(HistoryQuery),
    // ── Feature support query ──────────────────────────────────────────
    /// Query another node's supported features
    Features,
}

impl Message<'_> {
    /// Encodes this message into a byte vector using postcard.
    /// Encodes this message into a byte vector using postcard.
    #[inline]
    pub fn encode(&self) -> Result<Vec<u8>> {
        postcard::to_stdvec(self).map_err(anyhow::Error::new)
    }
    /// Decodes this message from a byte slice.
    #[inline]
    pub fn decode(data: &[u8]) -> Result<Message<'_>> {
        postcard::from_bytes::<Message>(data).map_err(anyhow::Error::new)
    }
}

/// gRPC reply messages for the HTTP API plugin.
#[derive(Serialize, Deserialize, Debug)]
pub enum MessageReply {
    BrokerInfo(BrokerInfo),
    NodeInfo(NodeInfo),
    NodeHealthStatus(NodeHealthStatus),
    StatsInfo(NodeStatus, Box<Stats>),
    MetricsInfo(Box<Metrics>),
    ClientSearch(Vec<ClientSearchResult>),
    ClientGet(Option<ClientSearchResult>),
    Subscribe(HashMap<TopicFilter, (bool, Option<String>)>),
    Unsubscribe,
    GetPlugins(Vec<PluginInfo>),
    GetPlugin(Option<PluginInfo>),
    GetPluginConfig(Vec<u8>),
    ReloadPluginConfig,
    LoadPlugin,
    UnloadPlugin(bool),
    // ── History query replies ──────────────────────────────────────────
    /// Stats history data from a node.
    ///
    /// The [`HistoryData`] is transported as its **JSON string** because
    /// postcard cannot deserialize `serde_json::Value` (it requires
    /// `deserialize_any`, which postcard explicitly does not implement).
    StatsHistoryReply(String),
    /// Metrics history data from a node (same JSON-string transport).
    MetricsHistoryReply(String),
    // ── Feature support reply ──────────────────────────────────────────
    /// Feature support state of a node.
    Features(FeaturesInfo),
}

impl MessageReply {
    /// Encodes this reply into a byte vector using postcard.
    /// Encodes this reply into a byte vector using postcard.
    #[inline]
    pub fn encode(&self) -> Result<Vec<u8>> {
        postcard::to_stdvec(self).map_err(anyhow::Error::new)
    }
    /// Decodes this reply from a byte slice.
    #[inline]
    pub fn decode(data: &[u8]) -> Result<MessageReply> {
        postcard::from_bytes::<MessageReply>(data).map_err(anyhow::Error::new)
    }
}

/// Search/filter parameters for listing clients via the HTTP API.
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct ClientSearchParams {
    #[serde(default)]
    pub _limit: usize,
    pub clientid: Option<String>,
    pub username: Option<String>,
    pub ip_address: Option<String>,
    pub connected: Option<bool>,
    pub clean_start: Option<bool>,
    pub session_present: Option<bool>,
    pub proto_ver: Option<u8>,
    pub _like_clientid: Option<String>,
    //Substring fuzzy search
    pub _like_username: Option<String>,
    //Substring fuzzy search
    #[serde(
        default,
        deserialize_with = "deserialize_datetime_option",
        serialize_with = "serialize_datetime_option"
    )]
    pub _gte_created_at: Option<Duration>,
    //Greater than or equal search
    #[serde(
        default,
        deserialize_with = "deserialize_datetime_option",
        serialize_with = "serialize_datetime_option"
    )]
    pub _lte_created_at: Option<Duration>,
    //Less than or equal search
    #[serde(
        default,
        deserialize_with = "deserialize_datetime_option",
        serialize_with = "serialize_datetime_option"
    )]
    pub _gte_connected_at: Option<Duration>,
    //Greater than or equal search
    #[serde(
        default,
        deserialize_with = "deserialize_datetime_option",
        serialize_with = "serialize_datetime_option"
    )]
    pub _lte_connected_at: Option<Duration>,
    //Less than or equal search
    pub _gte_mqueue_len: Option<usize>,
    //Current length of message queue, Greater than or equal search
    pub _lte_mqueue_len: Option<usize>, //Current length of message queue, Less than or equal search
}

/// A single client's information returned by the search API.
#[derive(Deserialize, Serialize, Debug, Default)]
pub struct ClientSearchResult {
    pub node_id: NodeId,
    pub clientid: ClientId,
    pub username: UserName,
    pub superuser: bool,
    pub proto_ver: u8,
    pub ip_address: Option<String>,
    pub port: Option<u16>,
    pub connected: bool,
    pub connected_at: Timestamp,
    pub disconnected_at: Timestamp,
    pub disconnected_reason: String,
    pub keepalive: u16,
    pub clean_start: bool,
    pub session_present: bool,
    pub expiry_interval: i64,
    pub created_at: Timestamp,
    pub subscriptions_cnt: usize,
    pub max_subscriptions: usize,
    // pub extra_attrs: usize,
    #[serde(
        default,
        serialize_with = "ClientSearchResult::serialize_last_will",
        deserialize_with = "ClientSearchResult::deserialize_last_will"
    )]
    pub last_will: serde_json::Value,

    pub inflight: usize,
    pub max_inflight: u16,
    //    pub inflight_dropped: usize,
    pub mqueue_len: usize,
    pub max_mqueue: usize,
    //     pub mqueue_dropped: usize,

    //    pub awaiting_rel:0,
    //    pub max_awaiting_rel:s.listen_cfg.max_awaiting_rel,
    //    pub awaiting_rel_dropped:0,

    //     pub recv_msg:0,	//Number of received PUBLISH packets
    //     pub send_msg:0,	//Number of sent PUBLISH packets
    //     pub resend_msg:0, //Resent message data
    //     pub ackeds:0,  //Number of Acked received
}

impl ClientSearchResult {
    /// Serializes the last will as a JSON byte vector.
    #[inline]
    fn serialize_last_will<S>(last_will: &serde_json::Value, s: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: ser::Serializer,
    {
        serde_json::to_vec(last_will).map_err(ser::Error::custom)?.serialize(s)
    }

    /// Deserializes the last will from a JSON byte vector.
    #[inline]
    pub fn deserialize_last_will<'de, D>(d: D) -> std::result::Result<serde_json::Value, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        serde_json::from_slice(&Vec::deserialize(d)?).map_err(de::Error::custom)
    }

    /// Converts this search result to a JSON value for API responses.
    #[inline]
    pub fn to_json(&self) -> serde_json::Value {
        let data = serde_json::json!({
            "node_id": self.node_id,
            "clientid": self.clientid,
            "username": self.username,
            "superuser": self.superuser,
            "proto_ver": self.proto_ver,
            "ip_address": self.ip_address,
            "port": self.port,
            "connected": self.connected,
            "connected_at": format_timestamp(self.connected_at),
            "disconnected_at": format_timestamp(self.disconnected_at),
            "disconnected_reason": self.disconnected_reason,
            "keepalive": self.keepalive,
            "clean_start": self.clean_start,
            "session_present": self.session_present,
            "expiry_interval": self.expiry_interval,
            "created_at": format_timestamp(self.created_at),
            "subscriptions_cnt": self.subscriptions_cnt,
            "max_subscriptions": self.max_subscriptions,
            // "extra_attrs": self.extra_attrs,
            "last_will": self.last_will,

            "inflight": self.inflight,
            "max_inflight": self.max_inflight,
            //"inflight_dropped": 0,

            "mqueue_len": self.mqueue_len,
            "max_mqueue": self.max_mqueue,
            // "mqueue_dropped": 0,

            //"awaiting_rel": 0,
            //"max_awaiting_rel": s.listen_cfg.max_awaiting_rel,
            //"awaiting_rel_dropped": 0,

            // "recv_msg": 0,	//Number of received PUBLISH packets
            // "send_msg": 0,	//Number of sent PUBLISH packets
            // "resend_msg": 0, //Resent message data
            // "ackeds": 0,  //Number of Acked received

        });
        data
    }
}

/// Parameters for publishing an MQTT message via the HTTP API.
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct PublishParams {
    //For topic and topics, with at least one of them specified
    pub topic: Option<TopicName>,
    //Multiple topics separated by ,. This field is used to publish messages to multiple topics at the same time
    pub topics: Option<TopicName>,
    //Client identifier. Default: system
    #[serde(default = "PublishParams::clientid_default")]
    pub clientid: ClientId,
    //Message body
    pub payload: String,
    //The encoding used in the message body. Currently only plain and base64 are supported. Default: plain
    #[serde(default = "PublishParams::encoding_default")]
    pub encoding: String,
    //QoS level, Default: 0
    #[serde(default = "PublishParams::qos_default")]
    pub qos: u8,
    //Whether it is a retained message, Default: false
    #[serde(default = "PublishParams::retain_default")]
    pub retain: bool,
    //Publish Properties
    pub properties: Option<PublishProperties>,
}

impl PublishParams {
    fn clientid_default() -> ClientId {
        "system".into()
    }

    fn encoding_default() -> String {
        "plain".into()
    }

    fn qos_default() -> u8 {
        0
    }

    fn retain_default() -> bool {
        false
    }
}

/// Parameters for subscribing to MQTT topics via the HTTP API.
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct SubscribeParams {
    //For topic and topics, with at least one of them specified
    pub topic: Option<TopicFilter>,
    //Multiple topics separated by,. This field is used to subscribe to multiple topics at the same time
    pub topics: Option<TopicFilter>,
    //Client identifier, Required
    pub clientid: ClientId,
    //QoS level, Default: 0
    #[serde(default = "SubscribeParams::qos_default")]
    pub qos: u8,
}

impl SubscribeParams {
    fn qos_default() -> u8 {
        0
    }

    /// Returns the list of topic filters from the `topic` and/or `topics`
    /// fields.
    /// Returns the list of topic filters from the `topic` and/or `topics`
    /// fields.
    #[inline]
    pub fn topics(&self) -> Result<Vec<TopicFilter>> {
        let mut topics = if let Some(topics) = &self.topics {
            topics.split(',').collect::<Vec<_>>().iter().map(|t| TopicName::from(t.trim())).collect()
        } else {
            Vec::new()
        };
        if let Some(topic) = &self.topic {
            topics.push(topic.clone());
        }
        if topics.is_empty() {
            return Err(anyhow!("topics or topic is empty"));
        }
        Ok(topics)
    }

    /// Returns the QoS level parsed from the raw u8 value.
    #[inline]
    pub fn qos(&self) -> Result<QoS> {
        QoS::try_from(self.qos).map_err(|e| anyhow!(e))
    }
}

/// Parameters for unsubscribing from an MQTT topic via the HTTP API.
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct UnsubscribeParams {
    pub topic: TopicFilter,
    pub clientid: ClientId,
}

/// Feature support state of a single node.
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct FeaturesInfo {
    pub node_id: NodeId,
    pub node_name: String,
    pub features: Features,
}

/// Runtime capability flags of a broker node.
///
/// A feature is `true` when the backing implementation is loaded and
/// enabled at runtime (e.g. the corresponding plugin is started).
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct Features {
    /// Retained messages (`rmqtt-retainer` plugin).
    pub retain: bool,
    /// Persistent message storage (`rmqtt-message-storage` plugin).
    pub message_storage: bool,
    /// Persistent session storage (`rmqtt-session-storage` plugin).
    pub session_storage: bool,
    /// Delayed message publishing (`$delayed/...` topics).
    pub delayed: bool,
    /// Shared subscriptions `$share` (`rmqtt-shared-subscription` plugin).
    pub shared_subscription: bool,
    /// Automatic subscriptions (`rmqtt-auto-subscription` plugin).
    pub auto_subscription: bool,
}

/// Aggregated feature support state across all cluster nodes.
///
/// `consistent` is `false` when at least one feature field differs between
/// nodes; the differing fields are reported in `conflicts` so operators can
/// locate mis-configured / partially-failed nodes.
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
pub struct FeaturesSummary {
    /// Whether all successfully-reached nodes report identical feature flags.
    pub consistent: bool,
    /// Number of nodes that successfully reported their features.
    pub node_count: usize,
    /// Feature fields whose values differ across nodes (empty when `consistent`).
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub conflicts: Vec<FeatureConflict>,
    /// Per-node feature details. Unreachable nodes are represented as an
    /// error string and do not participate in the consistency check.
    pub nodes: Vec<FeaturesInfoOrError>,
}

/// A feature field whose reported value differs across cluster nodes.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FeatureConflict {
    pub feature: String,
    /// Nodes grouped by their reported value of this feature field.
    pub values: Vec<FeatureValueGroup>,
}

/// Nodes that reported the same value for a conflicting feature field.
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct FeatureValueGroup {
    pub value: bool,
    pub node_ids: Vec<NodeId>,
}

/// Per-node entry of the features summary: either the feature state of a
/// reachable node, or an error string for an unreachable one.
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(untagged)]
pub enum FeaturesInfoOrError {
    Info(FeaturesInfo),
    Error(String),
}

/// Query parameters for listing retained messages.
///
/// - When `topic_filter` is empty or `#`, the storage backend's paginated
///   snapshot is used directly (`RetainStorage::get_all_paginated`), which
///   includes the remaining TTL of each message.
/// - Otherwise all messages matching the filter are fetched via
///   `RetainStorage::get` and paginated in memory (no TTL information).
#[derive(Deserialize, Serialize, Debug, Clone, Default)]
pub struct RetainQueryParams {
    /// Topic filter, supports `#` / `+` wildcards. Default: `#` (all messages).
    #[serde(default = "RetainQueryParams::topic_filter_default")]
    pub topic_filter: TopicFilter,
    /// Pagination offset. Default: 0.
    #[serde(default)]
    pub offset: usize,
    /// Page size. `0` or values above `max_row_limit` are capped by the caller.
    #[serde(default)]
    pub limit: usize,
}

impl RetainQueryParams {
    fn topic_filter_default() -> TopicFilter {
        "#".into()
    }
}

/// A single retained message entry returned by the HTTP API.
#[derive(Serialize, Deserialize, Debug)]
pub struct RetainInfo {
    pub topic: TopicName,
    pub msg_id: Option<MsgID>,
    pub from: From,
    pub publish: RetainPublishInfo,
    /// Remaining time-to-live in seconds. `null` when the storage backend
    /// does not expose TTL information (the topic-filtered `get()` path).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub remaining_ttl: Option<u64>,
    /// Client ID of the publisher, extracted from `from.id.client_id`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub client_id: Option<String>,
}

impl RetainInfo {
    /// Build an entry from a `(TopicName, Retain)` pair (no TTL info).
    #[inline]
    pub fn from_get(topic: TopicName, retain: Retain) -> Self {
        Self {
            topic,
            msg_id: retain.msg_id,
            from: retain.from.clone(),
            publish: RetainPublishInfo::from_publish(retain.publish),
            remaining_ttl: None,
            client_id: publisher_client_id(&retain.from),
        }
    }

    /// Build an entry from a `(TopicName, Retain, Option<Duration>)` triple.
    #[inline]
    pub fn from_paginated(topic: TopicName, retain: Retain, remaining: Option<Duration>) -> Self {
        Self {
            topic,
            msg_id: retain.msg_id,
            from: retain.from.clone(),
            publish: RetainPublishInfo::from_publish(retain.publish),
            remaining_ttl: remaining.map(|d| d.as_secs()),
            client_id: publisher_client_id(&retain.from),
        }
    }
}

/// Extract the publisher client ID from a `From` (empty -> `None`).
#[inline]
fn publisher_client_id(from: &From) -> Option<String> {
    let client_id = from.id.client_id.to_string();
    if client_id.is_empty() {
        None
    } else {
        Some(client_id)
    }
}

/// Serialized form of a retained publish packet for the HTTP API response.
#[derive(Serialize, Deserialize, Debug)]
pub struct RetainPublishInfo {
    pub topic: TopicName,
    pub qos: u8,
    pub retain: bool,
    pub dup: bool,
    /// Message payload encoded as base64.
    pub payload: String,
    pub create_time: Option<i64>,
    pub properties: Option<PublishProperties>,
}

impl RetainPublishInfo {
    #[inline]
    fn from_publish(p: Publish) -> Self {
        let inner = p.inner;
        Self {
            topic: inner.topic,
            qos: inner.qos as u8,
            retain: inner.retain,
            dup: inner.dup,
            payload: BASE64_STANDARD.encode(inner.payload.as_ref()),
            create_time: p.create_time,
            properties: inner.properties,
        }
    }
}

/// Specifies which nodes' Prometheus data to include.
///
/// - `All`: data from every node.
/// - `Sum`: aggregated sum across all nodes.
/// - `Node(id)`: data from a specific node.
#[derive(Deserialize, Serialize, Debug, Copy, Clone, Hash, Eq, PartialEq)]
pub enum PrometheusDataType {
    All,
    Node(NodeId),
    Sum,
}

/// Query parameters for fetching history data from a remote node via gRPC.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HistoryQuery {
    /// Start timestamp (milliseconds, inclusive)
    pub start_ts: u64,
    /// End timestamp (milliseconds, inclusive)
    pub end_ts: u64,
    /// Maximum number of data points to return
    pub limit: usize,
    /// Merge window in seconds — returns data merged at this granularity.
    /// When `None`, uses the node's `flush_interval`.
    pub merge_window: Option<u64>,
}

/// History data returned by a node for a history query.
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct HistoryData {
    /// Node that produced this data
    pub node: NodeId,
    /// Start of the query window
    pub from: u64,
    /// End of the query window
    pub to: u64,
    /// Number of data points returned
    pub count: usize,
    /// JSON data points, each is a flattened Stats/Metrics snapshot
    /// with a "ts" field for the timestamp.
    pub data: Vec<serde_json::Value>,
}

// #[inline]
// fn format_timestamp(t: i64) -> String {
//     if t <= 0 {
//         "".into()
//     } else {
//         use chrono::TimeZone;
//         if let LocalResult::Single(t) = chrono::Local.timestamp_opt(t, 0) {
//             t.format("%Y-%m-%d %H:%M:%S").to_string()
//         } else {
//             "".into()
//         }
//     }
// }

// ════════════════════════════════════════════════════════════════════════
//  LRU Cache types for history optimisation
// ════════════════════════════════════════════════════════════════════════

/// Bit flags for cache entry persistence state.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct EntryFlags(u8);

impl EntryFlags {
    pub const NONE: u8 = 0b00;
    pub const PENDING: u8 = 0b01;
    pub const FAILED: u8 = 0b10;

    #[inline]
    pub const fn new(bits: u8) -> Self {
        Self(bits)
    }

    #[inline]
    #[allow(dead_code)]
    pub fn is_pending(self) -> bool {
        self.0 & Self::PENDING != 0
    }
    #[inline]
    #[allow(dead_code)]
    pub fn is_failed(self) -> bool {
        self.0 & Self::FAILED != 0
    }

    /// Returns `true` if the entry needs recovery attention (has FAILED bit set).
    #[inline]
    pub fn needs_recovery(self) -> bool {
        self.0 & Self::FAILED != 0
    }
}

/// A single history data point in the LRU cache.
#[derive(Clone, Debug)]
pub(crate) struct CacheEntry {
    /// JSON-serialised Stats or Metrics snapshot.
    pub json: String,
    /// Persistence state flags.
    pub flags: EntryFlags,
}

impl CacheEntry {
    #[inline]
    pub fn new(json: String) -> Self {
        Self { json, flags: EntryFlags::new(EntryFlags::PENDING) }
    }
}