Skip to main content

relay_knowledge/api/
metadata.rs

1use serde::{Deserialize, Serialize};
2
3use crate::domain::GraphVersion;
4
5use super::RequestContext;
6
7/// Common metadata that every successful API response must carry.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
9pub struct ApiMetadata {
10    pub trace_id: String,
11    pub request_id: String,
12    pub graph_version: u64,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    pub index_version: Option<u64>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub indexed_graph_version: Option<u64>,
17    pub stale: bool,
18}
19
20impl ApiMetadata {
21    /// Builds response metadata for graph-only operations.
22    pub fn graph_only(context: &RequestContext, graph_version: GraphVersion) -> Self {
23        Self {
24            trace_id: context.trace_id.clone(),
25            request_id: context.request_id.clone(),
26            graph_version: graph_version.get(),
27            index_version: None,
28            indexed_graph_version: None,
29            stale: false,
30        }
31    }
32
33    /// Builds response metadata for operations that used derived indexes.
34    pub fn indexed(
35        context: &RequestContext,
36        graph_version: GraphVersion,
37        index_version: Option<u64>,
38        indexed_graph_version: Option<GraphVersion>,
39        stale: bool,
40    ) -> Self {
41        Self {
42            trace_id: context.trace_id.clone(),
43            request_id: context.request_id.clone(),
44            graph_version: graph_version.get(),
45            index_version,
46            indexed_graph_version: indexed_graph_version.map(GraphVersion::get),
47            stale,
48        }
49    }
50}