1use std::ffi::c_void;
7
8use super::{RawObject, RawObjectTrait};
9use crate::{baml_unreachable, error::BamlError, proto::baml_cffi_v1::BamlObjectType};
10
11define_raw_object_wrapper! {
16 TypeDef => ObjectType
18}
19
20impl TypeDef {
21 #[must_use]
23 pub fn print(&self) -> String {
24 self.raw.call_method("__display__", ())
25 }
26
27 #[must_use]
29 pub fn list(&self) -> TypeDef {
30 self.raw
31 .call_method_for_object("list", ())
32 .unwrap_or_else(|e| baml_unreachable!("Failed to create list type: {}", e))
33 }
34
35 #[must_use]
37 pub fn optional(&self) -> TypeDef {
38 self.raw
39 .call_method_for_object("optional", ())
40 .unwrap_or_else(|e| baml_unreachable!("Failed to create optional type: {}", e))
41 }
42}
43
44define_raw_object_wrapper! {
49 EnumValueBuilder => ObjectEnumValueBuilder
54}
55
56impl EnumValueBuilder {
57 pub fn name(&self) -> Result<String, BamlError> {
59 self.raw.try_call_method("name", ())
60 }
61
62 pub fn set_description(&self, description: &str) -> Result<(), BamlError> {
64 self.raw
65 .try_call_method("set_description", ("description", description))
66 }
67
68 pub fn description(&self) -> Result<Option<String>, BamlError> {
70 self.raw.try_call_method("description", ())
71 }
72
73 pub fn set_alias(&self, alias: &str) -> Result<(), BamlError> {
75 self.raw.try_call_method("set_alias", ("alias", alias))
76 }
77
78 pub fn alias(&self) -> Result<Option<String>, BamlError> {
80 self.raw.try_call_method("alias", ())
81 }
82
83 pub fn set_skip(&self, skip: bool) -> Result<(), BamlError> {
85 self.raw.try_call_method("set_skip", ("skip", skip))
86 }
87
88 pub fn skip(&self) -> Result<bool, BamlError> {
90 self.raw.try_call_method("skip", ())
91 }
92}
93
94define_raw_object_wrapper! {
99 EnumBuilder => ObjectEnumBuilder
104}
105
106impl EnumBuilder {
107 pub fn add_value(&self, value: &str) -> Result<EnumValueBuilder, BamlError> {
109 self.raw
110 .call_method_for_object("add_value", ("value", value))
111 }
112
113 pub fn get_value(&self, name: &str) -> Option<EnumValueBuilder> {
115 self.raw
116 .call_method_for_object("value", ("name", name))
117 .ok()
118 }
119
120 pub fn list_values(&self) -> Result<Vec<EnumValueBuilder>, BamlError> {
122 self.raw.call_method_for_objects("list_values", ())
123 }
124
125 pub fn as_type(&self) -> Result<TypeDef, BamlError> {
127 self.raw.call_method_for_object("type_", ())
128 }
129
130 pub fn name(&self) -> Result<String, BamlError> {
132 self.raw.try_call_method("name", ())
133 }
134
135 pub fn set_description(&self, description: &str) -> Result<(), BamlError> {
137 self.raw
138 .try_call_method("set_description", ("description", description))
139 }
140
141 pub fn description(&self) -> Result<Option<String>, BamlError> {
143 self.raw.try_call_method("description", ())
144 }
145
146 pub fn set_alias(&self, alias: &str) -> Result<(), BamlError> {
148 self.raw.try_call_method("set_alias", ("alias", alias))
149 }
150
151 pub fn alias(&self) -> Result<Option<String>, BamlError> {
153 self.raw.try_call_method("alias", ())
154 }
155}
156
157define_raw_object_wrapper! {
162 ClassPropertyBuilder => ObjectClassPropertyBuilder
167}
168
169impl ClassPropertyBuilder {
170 pub fn name(&self) -> Result<String, BamlError> {
172 self.raw.try_call_method("name", ())
173 }
174
175 pub fn set_type(&self, field_type: &TypeDef) -> Result<(), BamlError> {
177 self.raw
178 .try_call_method("set_type", ("field_type", field_type))
179 }
180
181 pub fn get_type(&self) -> Result<TypeDef, BamlError> {
183 self.raw.call_method_for_object("type_", ())
184 }
185
186 pub fn set_description(&self, description: &str) -> Result<(), BamlError> {
188 self.raw
189 .try_call_method("set_description", ("description", description))
190 }
191
192 pub fn description(&self) -> Result<Option<String>, BamlError> {
194 self.raw.try_call_method("description", ())
195 }
196
197 pub fn set_alias(&self, alias: &str) -> Result<(), BamlError> {
199 self.raw.try_call_method("set_alias", ("alias", alias))
200 }
201
202 pub fn alias(&self) -> Result<Option<String>, BamlError> {
204 self.raw.try_call_method("alias", ())
205 }
206}
207
208define_raw_object_wrapper! {
213 ClassBuilder => ObjectClassBuilder
218}
219
220impl ClassBuilder {
221 pub fn add_property(
223 &self,
224 name: &str,
225 field_type: &TypeDef,
226 ) -> Result<ClassPropertyBuilder, BamlError> {
227 self.raw
228 .call_method_for_object("add_property", (("name", name), ("field_type", field_type)))
229 }
230
231 pub fn get_property(&self, name: &str) -> Option<ClassPropertyBuilder> {
233 self.raw
234 .call_method_for_object("property", ("name", name))
235 .ok()
236 }
237
238 pub fn list_properties(&self) -> Result<Vec<ClassPropertyBuilder>, BamlError> {
240 self.raw.call_method_for_objects("list_properties", ())
241 }
242
243 pub fn as_type(&self) -> Result<TypeDef, BamlError> {
245 self.raw.call_method_for_object("type_", ())
246 }
247
248 pub fn name(&self) -> Result<String, BamlError> {
250 self.raw.try_call_method("name", ())
251 }
252
253 pub fn set_description(&self, description: &str) -> Result<(), BamlError> {
255 self.raw
256 .try_call_method("set_description", ("description", description))
257 }
258
259 pub fn description(&self) -> Result<Option<String>, BamlError> {
261 self.raw.try_call_method("description", ())
262 }
263
264 pub fn set_alias(&self, alias: &str) -> Result<(), BamlError> {
266 self.raw.try_call_method("set_alias", ("alias", alias))
267 }
268
269 pub fn alias(&self) -> Result<Option<String>, BamlError> {
271 self.raw.try_call_method("alias", ())
272 }
273}
274
275define_raw_object_wrapper! {
280 TypeBuilder => ObjectTypeBuilder
282}
283
284impl TypeBuilder {
285 pub fn new(runtime: *const c_void) -> Self {
287 let raw = RawObject::new(runtime, BamlObjectType::ObjectTypeBuilder, ())
288 .unwrap_or_else(|e| baml_unreachable!("Failed to create TypeBuilder: {}", e));
289 Self { raw }
290 }
291
292 pub fn string(&self) -> TypeDef {
298 self.raw
299 .call_method_for_object("string", ())
300 .unwrap_or_else(|e| baml_unreachable!("Failed to get string type: {}", e))
301 }
302
303 pub fn int(&self) -> TypeDef {
305 self.raw
306 .call_method_for_object("int", ())
307 .unwrap_or_else(|e| baml_unreachable!("Failed to get int type: {}", e))
308 }
309
310 pub fn float(&self) -> TypeDef {
312 self.raw
313 .call_method_for_object("float", ())
314 .unwrap_or_else(|e| baml_unreachable!("Failed to get float type: {}", e))
315 }
316
317 pub fn bool(&self) -> TypeDef {
319 self.raw
320 .call_method_for_object("bool", ())
321 .unwrap_or_else(|e| baml_unreachable!("Failed to get bool type: {}", e))
322 }
323
324 pub fn null(&self) -> TypeDef {
326 self.raw
327 .call_method_for_object("null", ())
328 .unwrap_or_else(|e| baml_unreachable!("Failed to get null type: {}", e))
329 }
330
331 pub fn literal_string(&self, value: &str) -> TypeDef {
337 self.raw
338 .call_method_for_object("literal_string", ("value", value))
339 .unwrap_or_else(|e| baml_unreachable!("Failed to get literal string type: {}", e))
340 }
341
342 pub fn literal_int(&self, value: i64) -> TypeDef {
344 self.raw
345 .call_method_for_object("literal_int", ("value", value))
346 .unwrap_or_else(|e| baml_unreachable!("Failed to get literal int type: {}", e))
347 }
348
349 pub fn literal_bool(&self, value: bool) -> TypeDef {
351 self.raw
352 .call_method_for_object("literal_bool", ("value", value))
353 .unwrap_or_else(|e| baml_unreachable!("Failed to get literal bool type: {}", e))
354 }
355
356 pub fn list(&self, inner: &TypeDef) -> TypeDef {
362 self.raw
363 .call_method_for_object("list", ("inner", inner))
364 .unwrap_or_else(|e| baml_unreachable!("Failed to get list type: {}", e))
365 }
366
367 pub fn optional(&self, inner: &TypeDef) -> TypeDef {
369 self.raw
370 .call_method_for_object("optional", ("inner", inner))
371 .unwrap_or_else(|e| baml_unreachable!("Failed to get optional type: {}", e))
372 }
373
374 pub fn map(&self, key: &TypeDef, value: &TypeDef) -> TypeDef {
376 self.raw
377 .call_method_for_object("map", (("key", key), ("value", value)))
378 .unwrap_or_else(|e| baml_unreachable!("Failed to get map type: {}", e))
379 }
380
381 pub fn union(&self, types: &[&TypeDef]) -> TypeDef {
383 self.raw
384 .call_method_for_object("union", ("types", types))
385 .unwrap_or_else(|e| baml_unreachable!("Failed to get union type: {}", e))
386 }
387
388 pub fn add_baml(&self, baml: &str) -> Result<(), BamlError> {
394 self.raw.try_call_method("add_baml", ("baml", baml))
395 }
396
397 pub fn print(&self) -> String {
399 self.raw.call_method("__display__", ())
400 }
401
402 pub fn add_enum(&self, name: &str) -> Result<EnumBuilder, BamlError> {
408 self.raw.call_method_for_object("add_enum", ("name", name))
409 }
410
411 pub fn get_enum(&self, name: &str) -> Option<EnumBuilder> {
413 self.raw
414 .call_method_for_object_optional("enum_", ("name", name))
415 .ok()
416 .flatten()
417 }
418
419 pub fn list_enums(&self) -> Vec<EnumBuilder> {
421 self.raw
422 .call_method_for_objects("list_enums", ())
423 .unwrap_or_else(|e| baml_unreachable!("Failed to list enums: {}", e))
424 }
425
426 pub fn add_class(&self, name: &str) -> Result<ClassBuilder, BamlError> {
432 self.raw.call_method_for_object("add_class", ("name", name))
433 }
434
435 pub fn get_class(&self, name: &str) -> Option<ClassBuilder> {
437 self.raw
438 .call_method_for_object_optional("class", ("name", name))
439 .ok()
440 .flatten()
441 }
442
443 pub fn list_classes(&self) -> Vec<ClassBuilder> {
445 self.raw
446 .call_method_for_objects("list_classes", ())
447 .unwrap_or_else(|e| baml_unreachable!("Failed to list classes: {}", e))
448 }
449}