vika_cli/templates/context/
zod_context.rs

1use serde::Serialize;
2
3/// Context for Zod schema generation.
4#[derive(Debug, Clone, Serialize)]
5pub struct ZodContext {
6    pub schema_name: String,
7    pub zod_expr: String,
8    pub is_enum: bool,
9    pub enum_values: Option<Vec<String>>,
10    pub description: Option<String>,
11    pub needs_type_annotation: bool,
12    /// Spec name (for multi-spec mode)
13    pub spec_name: Option<String>,
14}
15
16impl ZodContext {
17    /// Create a new ZodContext for a regular schema.
18    pub fn schema(
19        schema_name: String,
20        zod_expr: String,
21        description: Option<String>,
22        spec_name: Option<String>,
23    ) -> Self {
24        Self {
25            schema_name,
26            zod_expr,
27            is_enum: false,
28            enum_values: None,
29            description,
30            needs_type_annotation: false,
31            spec_name,
32        }
33    }
34
35    /// Create a new ZodContext for a schema that needs type annotation (e.g., circular references).
36    pub fn schema_with_annotation(
37        schema_name: String,
38        zod_expr: String,
39        description: Option<String>,
40        spec_name: Option<String>,
41    ) -> Self {
42        Self {
43            schema_name,
44            zod_expr,
45            is_enum: false,
46            enum_values: None,
47            description,
48            needs_type_annotation: true,
49            spec_name,
50        }
51    }
52
53    /// Create a new ZodContext for an enum schema.
54    pub fn enum_schema(
55        schema_name: String,
56        enum_values: Vec<String>,
57        spec_name: Option<String>,
58    ) -> Self {
59        let enum_values_str = enum_values
60            .iter()
61            .map(|v| format!("\"{}\"", v))
62            .collect::<Vec<_>>()
63            .join(", ");
64        let zod_expr = format!("z.enum([{}])", enum_values_str);
65
66        Self {
67            schema_name,
68            zod_expr,
69            is_enum: true,
70            enum_values: Some(enum_values),
71            description: None,
72            needs_type_annotation: false,
73            spec_name,
74        }
75    }
76}