Skip to main content

zlayer_types/api/
build.rs

1//! Build API DTOs.
2//!
3//! Wire types for the build endpoints. These are the request/response
4//! shapes consumed by both the daemon and SDK clients.
5
6use std::collections::HashMap;
7
8use serde::{Deserialize, Serialize};
9
10/// Build request for JSON API
11#[derive(Debug, Default, Deserialize, utoipa::ToSchema)]
12pub struct BuildRequest {
13    /// Use runtime template instead of Dockerfile
14    #[serde(default)]
15    pub runtime: Option<String>,
16    /// Build arguments (ARG values)
17    #[serde(default)]
18    pub build_args: HashMap<String, String>,
19    /// Target stage for multi-stage builds
20    #[serde(default)]
21    pub target: Option<String>,
22    /// Tags to apply to the image
23    #[serde(default)]
24    pub tags: Vec<String>,
25    /// Disable cache
26    #[serde(default)]
27    pub no_cache: bool,
28    /// Push to registry after build
29    #[serde(default)]
30    pub push: bool,
31    /// Target platforms for a multi-arch manifest-list build (e.g.
32    /// `["linux/amd64", "linux/arm64"]`). More than one builds a manifest
33    /// list. Empty = single-arch (host) build.
34    #[serde(default)]
35    pub platforms: Vec<String>,
36}
37
38/// Build status response
39#[derive(Debug, Clone, Serialize, Deserialize, utoipa::ToSchema)]
40pub struct BuildStatus {
41    /// Unique build ID
42    pub id: String,
43    /// Current build status
44    pub status: BuildStateEnum,
45    /// Image ID (if completed)
46    #[serde(skip_serializing_if = "Option::is_none")]
47    pub image_id: Option<String>,
48    /// Error message (if failed)
49    #[serde(skip_serializing_if = "Option::is_none")]
50    pub error: Option<String>,
51    /// When the build started (ISO 8601)
52    pub started_at: String,
53    /// When the build completed (ISO 8601)
54    #[serde(skip_serializing_if = "Option::is_none")]
55    pub completed_at: Option<String>,
56}
57
58/// Build state enumeration
59#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, utoipa::ToSchema)]
60#[serde(rename_all = "snake_case")]
61pub enum BuildStateEnum {
62    /// Build is queued
63    Pending,
64    /// Build is running
65    Running,
66    /// Build completed successfully
67    Complete,
68    /// Build failed
69    Failed,
70}
71
72/// Runtime template information
73#[derive(Debug, Serialize, utoipa::ToSchema)]
74pub struct TemplateInfo {
75    /// Template name (e.g., "node20")
76    pub name: String,
77    /// Human-readable description
78    pub description: String,
79    /// Files that indicate this runtime should be used
80    pub detect_files: Vec<String>,
81}
82
83/// Trigger build response
84#[derive(Debug, Serialize, utoipa::ToSchema)]
85pub struct TriggerBuildResponse {
86    /// Unique build ID for tracking
87    pub build_id: String,
88    /// Human-readable message
89    pub message: String,
90}
91
92/// Build event wrapper for SSE serialization
93#[derive(Debug, Clone, Serialize)]
94pub struct BuildEventWrapper {
95    /// Event type
96    #[serde(rename = "type")]
97    pub event_type: String,
98    /// Event data
99    pub data: serde_json::Value,
100}
101
102/// Build request with server-side context path
103#[derive(Debug, Deserialize, utoipa::ToSchema)]
104pub struct BuildRequestWithContext {
105    /// Path to the build context on the server
106    pub context_path: String,
107    /// Use runtime template instead of Dockerfile
108    #[serde(default)]
109    pub runtime: Option<String>,
110    /// Build arguments
111    #[serde(default)]
112    pub build_args: HashMap<String, String>,
113    /// Target stage
114    #[serde(default)]
115    pub target: Option<String>,
116    /// Tags to apply
117    #[serde(default)]
118    pub tags: Vec<String>,
119    /// Disable cache
120    #[serde(default)]
121    pub no_cache: bool,
122    /// Push after build
123    #[serde(default)]
124    pub push: bool,
125    /// Target platforms for a multi-arch manifest-list build (e.g.
126    /// `["linux/amd64", "linux/arm64"]`). More than one builds a manifest
127    /// list. Empty = single-arch (host) build.
128    #[serde(default)]
129    pub platforms: Vec<String>,
130}
131
132impl From<BuildRequestWithContext> for BuildRequest {
133    fn from(req: BuildRequestWithContext) -> Self {
134        Self {
135            runtime: req.runtime,
136            build_args: req.build_args,
137            target: req.target,
138            tags: req.tags,
139            no_cache: req.no_cache,
140            push: req.push,
141            platforms: req.platforms,
142        }
143    }
144}