Skip to main content

odp_directory/
models.rs

1use std::collections::BTreeMap;
2
3use odp_core::{
4    AdditionalMembers, AuthenticationRequirement, EnrollmentProtocol, Operation,
5    OperationDescriptor, PaymentOption, PaymentProtocol, Protocol, ServiceProtocols, TrustProtocol,
6};
7use serde::{Deserialize, Serialize};
8use serde_json::Value;
9
10#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
11pub enum Environment {
12    #[default]
13    Production,
14    Sandbox,
15}
16
17impl Environment {
18    pub const fn origin(self) -> &'static str {
19        match self {
20            Self::Production => "https://directory.offeringprotocol.org",
21            Self::Sandbox => "https://sandbox.inflowpay.ai",
22        }
23    }
24}
25
26#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
27pub struct ServiceFilters {
28    #[serde(default, skip_serializing_if = "Vec::is_empty")]
29    pub enrollment: Vec<EnrollmentProtocol>,
30    #[serde(default, skip_serializing_if = "Vec::is_empty")]
31    pub keywords: Vec<String>,
32    #[serde(default, skip_serializing_if = "Vec::is_empty")]
33    pub operations: Vec<OperationFilter>,
34    #[serde(default, skip_serializing_if = "Vec::is_empty")]
35    pub payments: Vec<PaymentFilter>,
36    #[serde(skip_serializing_if = "Option::is_none")]
37    pub sources: Option<Vec<SourceType>>,
38    /// A trust filter is either empty or the single-item array `[{"name":"tap"}]`: `tap` is the
39    /// only trust protocol this ODP version names.
40    #[serde(default, skip_serializing_if = "Vec::is_empty")]
41    pub trust: Vec<TrustProtocol>,
42}
43
44#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
45pub struct OperationFilter {
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub authentication: Option<AuthenticationRequirement>,
48    pub name: Operation,
49}
50
51#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
52pub struct PaymentFilter {
53    #[serde(skip_serializing_if = "Option::is_none")]
54    pub authentication: Option<AuthenticationRequirement>,
55    pub name: Protocol,
56    #[serde(default, skip_serializing_if = "Vec::is_empty")]
57    pub options: Vec<PaymentOption>,
58}
59
60#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
61pub struct SearchRequest {
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub filters: Option<ServiceFilters>,
64    #[serde(default, skip_serializing_if = "is_zero")]
65    pub limit: usize,
66    #[serde(default, skip_serializing_if = "String::is_empty")]
67    pub query: String,
68}
69
70fn is_zero(value: &usize) -> bool {
71    *value == 0
72}
73
74#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
75#[serde(rename_all = "lowercase")]
76pub enum ResultType {
77    Service,
78    Collection,
79}
80
81#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
82#[serde(rename_all = "lowercase")]
83pub enum SourceType {
84    Odp,
85    Openapi,
86}
87
88#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
89pub struct DirectorySource {
90    /// Unknown future formats remain readable but are not ODP capabilities.
91    #[serde(rename = "type")]
92    pub source_type: String,
93    pub url: String,
94    pub x402_discovery: bool,
95    #[serde(flatten)]
96    pub additional: AdditionalMembers,
97}
98
99#[derive(Clone, Debug, Deserialize, PartialEq)]
100pub struct DirectoryIndexedService {
101    pub description: Option<String>,
102    pub documentation_url: Option<String>,
103    pub indexed_at: String,
104    #[serde(default)]
105    pub keywords: Vec<String>,
106    pub language: Option<String>,
107    #[serde(default)]
108    pub localizations: Vec<String>,
109    pub name: String,
110    #[serde(default)]
111    pub operations: Vec<OperationDescriptor>,
112    pub protocols: Option<ServiceProtocols>,
113    pub service_id: String,
114    pub service_origin: String,
115    pub source: DirectorySource,
116    pub status_url: Option<String>,
117    pub support_url: Option<String>,
118    pub website_url: Option<String>,
119    #[serde(flatten)]
120    pub additional: AdditionalMembers,
121}
122
123#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize)]
124pub struct ResourceSearchRequest {
125    #[serde(skip_serializing_if = "Option::is_none")]
126    pub filters: Option<ServiceFilters>,
127    #[serde(default, skip_serializing_if = "is_zero")]
128    pub limit: usize,
129    #[serde(default, skip_serializing_if = "String::is_empty")]
130    pub query: String,
131    #[serde(skip_serializing_if = "Option::is_none")]
132    pub types: Option<Vec<ResultType>>,
133}
134
135#[derive(Clone, Debug, PartialEq)]
136pub enum DirectoryResult {
137    Service(Box<ServiceResult>),
138    Collection(Box<CollectionResult>),
139    Unknown { kind: String, raw: Value },
140}
141
142#[derive(Clone, Debug, Deserialize, PartialEq)]
143pub struct ServiceResult {
144    pub service: DirectoryIndexedService,
145    pub indexed_at: String,
146    pub publisher: Option<Publisher>,
147    #[serde(flatten)]
148    pub additional: AdditionalMembers,
149}
150
151#[derive(Clone, Debug, Deserialize, PartialEq)]
152pub struct CollectionResult {
153    pub service: DirectoryIndexedService,
154    pub indexed_at: String,
155    pub collection: CollectionSummary,
156    #[serde(flatten)]
157    pub additional: AdditionalMembers,
158}
159
160#[derive(Clone, Debug, Deserialize, PartialEq)]
161pub struct Publisher {
162    pub publisher_id: String,
163    pub name: String,
164    pub website_url: String,
165    #[serde(flatten)]
166    pub additional: AdditionalMembers,
167}
168
169#[derive(Clone, Debug, Deserialize, PartialEq)]
170pub struct CollectionSummary {
171    pub id: String,
172    pub name: String,
173    pub description: Option<String>,
174    #[serde(flatten)]
175    pub additional: AdditionalMembers,
176}
177
178#[derive(Clone, Debug, PartialEq)]
179pub struct DirectoryIssue {
180    pub index: usize,
181    pub message: String,
182}
183
184#[derive(Clone, Debug, PartialEq)]
185pub struct SearchResponse {
186    pub facets: Option<Facets>,
187    pub items: Vec<DirectoryResult>,
188    pub next: Option<String>,
189    pub issues: Vec<DirectoryIssue>,
190    pub additional: AdditionalMembers,
191}
192
193#[derive(Clone, Debug, Deserialize, PartialEq)]
194pub struct DirectoryService {
195    pub description: String,
196    #[serde(default)]
197    pub documentation_url: String,
198    pub indexed_at: String,
199    #[serde(default)]
200    pub keywords: Vec<String>,
201    pub language: String,
202    pub localizations: Vec<String>,
203    pub name: String,
204    pub operations: Vec<OperationDescriptor>,
205    #[serde(default)]
206    pub protocols: Option<ServiceProtocols>,
207    pub service_origin: String,
208    #[serde(default)]
209    pub status_url: String,
210    #[serde(default)]
211    pub support_url: String,
212    #[serde(default)]
213    pub website_url: String,
214    #[serde(flatten)]
215    pub additional: AdditionalMembers,
216}
217
218impl DirectoryService {
219    pub fn service_id(&self) -> Option<&str> {
220        self.additional.get("service_id").and_then(Value::as_str)
221    }
222}
223
224#[derive(Clone, Debug, Deserialize, PartialEq)]
225pub struct Facet<T> {
226    pub count: u64,
227    pub value: T,
228}
229
230#[derive(Clone, Debug, Default, Deserialize, PartialEq)]
231pub struct Facets {
232    #[serde(default)]
233    pub enrollment: Vec<Facet<EnrollmentProtocol>>,
234    #[serde(default)]
235    pub keywords: Vec<Facet<String>>,
236    #[serde(default)]
237    pub operations: Vec<Facet<OperationDescriptor>>,
238    #[serde(default)]
239    pub payment_options: Vec<Facet<PaymentOptionFacetValue>>,
240    #[serde(default)]
241    pub payments: Vec<Facet<PaymentProtocol>>,
242    /// A trust facet counts Services by trust protocol. `tap` is the only one this ODP version
243    /// names, so every descriptor carries that name and nothing else.
244    #[serde(default)]
245    pub trust: Vec<Facet<TrustProtocol>>,
246}
247
248#[derive(Clone, Debug, Deserialize, PartialEq)]
249pub struct PaymentOptionFacetValue {
250    pub name: Protocol,
251    pub option: PaymentOption,
252}
253
254/// A record the Directory published that this client would not hand back.
255///
256/// ROLE-03: a Directory result is discovery metadata, so one unusable record is a note about that
257/// record rather than a reason to withhold the page.
258#[derive(Clone, Debug, Eq, PartialEq)]
259pub struct ServiceIssue {
260    /// The record's position in the page the Directory sent.
261    pub index: usize,
262    pub message: String,
263}
264
265#[derive(Clone, Debug, Deserialize, PartialEq)]
266pub struct SearchPage {
267    #[serde(default)]
268    pub facets: Option<Facets>,
269    /// The records this client was able to read. Withheld records appear in `issues`.
270    pub items: Vec<DirectoryService>,
271    #[serde(default, skip)]
272    pub issues: Vec<ServiceIssue>,
273    #[serde(default, deserialize_with = "absent_as_empty")]
274    pub next: String,
275    #[serde(flatten)]
276    pub additional: BTreeMap<String, Value>,
277}
278
279/// A Directory that offers no continuation may omit `next` or send it as null; both mean the same.
280fn absent_as_empty<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<String, D::Error> {
281    Ok(Option::<String>::deserialize(deserializer)?.unwrap_or_default())
282}
283
284#[derive(Clone, Debug, Default, PartialEq, Serialize)]
285pub struct SuggestionRequest {
286    #[serde(skip_serializing_if = "Option::is_none")]
287    pub filters: Option<ServiceFilters>,
288    #[serde(skip_serializing_if = "is_zero")]
289    pub limit: usize,
290    pub prefix: String,
291}
292
293#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
294pub struct IterationOptions {
295    pub max_items: usize,
296    pub max_pages: usize,
297}