aperture_cli/cache/
models.rs

1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
5pub struct CachedSpec {
6    /// Cache format version to detect incompatible changes
7    pub cache_format_version: u32,
8    pub name: String,
9    pub version: String,
10    pub commands: Vec<CachedCommand>,
11    /// Base URL extracted from the first server in the `OpenAPI` spec
12    pub base_url: Option<String>,
13    /// All server URLs from the `OpenAPI` spec for future multi-environment support
14    pub servers: Vec<String>,
15    /// Security schemes defined in the `OpenAPI` spec with `x-aperture-secret` mappings
16    pub security_schemes: HashMap<String, CachedSecurityScheme>,
17    /// Endpoints skipped during validation due to unsupported features (added in v0.1.2)
18    #[serde(default)]
19    pub skipped_endpoints: Vec<SkippedEndpoint>,
20    /// Server variables defined in the `OpenAPI` spec for URL template resolution (added in v0.1.3)
21    #[serde(default)]
22    pub server_variables: HashMap<String, ServerVariable>,
23}
24
25impl CachedSpec {
26    /// Creates a new CachedSpec with default values for testing
27    #[cfg(test)]
28    pub fn new_for_test(name: &str) -> Self {
29        Self {
30            cache_format_version: CACHE_FORMAT_VERSION,
31            name: name.to_string(),
32            version: "1.0.0".to_string(),
33            commands: vec![],
34            base_url: None,
35            servers: vec![],
36            security_schemes: HashMap::new(),
37            skipped_endpoints: vec![],
38            server_variables: HashMap::new(),
39        }
40    }
41}
42
43/// Information about an endpoint that was skipped during spec validation
44#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
45pub struct SkippedEndpoint {
46    pub path: String,
47    pub method: String,
48    pub content_type: String,
49    pub reason: String,
50}
51
52/// Current cache format version - increment when making breaking changes to `CachedSpec`
53///
54/// Version 2: Added `skipped_endpoints` field to track endpoints skipped during validation
55/// Version 3: Added `server_variables` field to support `OpenAPI` server URL template variables
56pub const CACHE_FORMAT_VERSION: u32 = 3;
57
58/// Global cache metadata for all cached specifications
59#[derive(Debug, Deserialize, Serialize, PartialEq, Eq)]
60pub struct GlobalCacheMetadata {
61    /// Cache format version for all specs
62    pub cache_format_version: u32,
63    /// Individual spec metadata
64    pub specs: std::collections::HashMap<String, SpecMetadata>,
65}
66
67/// Metadata for a single cached specification
68#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
69pub struct SpecMetadata {
70    /// When this spec cache was created/updated
71    pub updated_at: String, // Using String for simplicity in serialization
72    /// Size of the cached spec file in bytes
73    pub file_size: u64,
74}
75
76impl Default for GlobalCacheMetadata {
77    fn default() -> Self {
78        Self {
79            cache_format_version: CACHE_FORMAT_VERSION,
80            specs: std::collections::HashMap::new(),
81        }
82    }
83}
84
85#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
86pub struct CachedCommand {
87    pub name: String,
88    pub description: Option<String>,
89    pub summary: Option<String>,
90    pub operation_id: String,
91    pub method: String,
92    pub path: String,
93    pub parameters: Vec<CachedParameter>,
94    pub request_body: Option<CachedRequestBody>,
95    pub responses: Vec<CachedResponse>,
96    /// Security requirements for this operation (references to security scheme names)
97    pub security_requirements: Vec<String>,
98    /// All tags associated with this operation
99    pub tags: Vec<String>,
100    /// Whether this operation is deprecated
101    pub deprecated: bool,
102    /// External documentation URL if available
103    pub external_docs_url: Option<String>,
104}
105
106#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
107pub struct CachedParameter {
108    pub name: String,
109    pub location: String,
110    pub required: bool,
111    pub description: Option<String>,
112    pub schema: Option<String>,
113    pub schema_type: Option<String>,
114    pub format: Option<String>,
115    pub default_value: Option<String>,
116    pub enum_values: Vec<String>,
117    pub example: Option<String>,
118}
119
120#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
121pub struct CachedRequestBody {
122    pub content_type: String,
123    pub schema: String,
124    pub required: bool,
125    pub description: Option<String>,
126    pub example: Option<String>,
127}
128
129#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
130pub struct CachedResponse {
131    pub status_code: String,
132    pub description: Option<String>,
133    pub content_type: Option<String>,
134    pub schema: Option<String>,
135}
136
137/// Cached representation of a security scheme with x-aperture-secret mapping
138#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
139pub struct CachedSecurityScheme {
140    /// Security scheme name from the `OpenAPI` spec
141    pub name: String,
142    /// Type of security scheme (apiKey, http, oauth2, etc.)
143    pub scheme_type: String,
144    /// Subtype for http schemes (bearer, basic, etc.)
145    pub scheme: Option<String>,
146    /// Location for apiKey schemes (header, query, cookie)
147    pub location: Option<String>,
148    /// Parameter name for apiKey schemes (e.g., "Authorization", "X-API-Key")
149    pub parameter_name: Option<String>,
150    /// Description of the security scheme from `OpenAPI` spec
151    pub description: Option<String>,
152    /// Bearer format for HTTP bearer schemes (e.g., "JWT")
153    pub bearer_format: Option<String>,
154    /// x-aperture-secret mapping for environment variable resolution
155    pub aperture_secret: Option<CachedApertureSecret>,
156}
157
158/// Cached representation of x-aperture-secret extension
159#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
160pub struct CachedApertureSecret {
161    /// Source of the secret (currently only "env" supported)
162    pub source: String,
163    /// Environment variable name to read the secret from
164    pub name: String,
165}
166
167/// Cached representation of an `OpenAPI` server variable for URL template resolution
168#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, Eq)]
169pub struct ServerVariable {
170    /// Default value for the variable if not provided via CLI
171    pub default: Option<String>,
172    /// Allowed values for the variable (enum constraint)
173    pub enum_values: Vec<String>,
174    /// Description of the server variable from `OpenAPI` spec
175    pub description: Option<String>,
176}