@@ -56,6 +56,98 @@ fn make_room_for_header(buffer: &mut Vec<u8>, start_pos: usize, header_size: usi
5656 buffer. copy_within ( src_start..src_end, dst_start) ;
5757}
5858
59+ /// Builder for [`Variant`] values
60+ ///
61+ /// # Example: create a Primitive Int8
62+ /// ```
63+ /// # use parquet_variant::{Variant, VariantBuilder, VariantMetadata};
64+ /// let mut builder = VariantBuilder::new();
65+ /// builder.append_value(Variant::Int8(42));
66+ /// // Finish the builder to get the metadata and value
67+ /// let (metadata, value) = builder.finish();
68+ /// // use the Variant API to verify the result
69+ /// let metadata = VariantMetadata::try_new(&metadata).unwrap();
70+ /// let variant = Variant::try_new(&metadata, &value).unwrap();
71+ /// assert_eq!(variant, Variant::Int8(42));
72+ /// ```
73+ ///
74+ /// # Example: Create an Object
75+ /// This example shows how to create an object with two fields:
76+ /// ```json
77+ /// {
78+ /// "first_name": "Jiaying",
79+ /// "last_name": "Li"
80+ /// }
81+ /// ```
82+ ///
83+ /// ```
84+ /// # use parquet_variant::{Variant, VariantBuilder, VariantMetadata};
85+ /// let mut builder = VariantBuilder::new();
86+ /// // Create an object builder that will write fields to the object
87+ /// let mut object_builder = builder.new_object();
88+ /// object_builder.append_value("first_name", "Jiaying");
89+ /// object_builder.append_value("last_name", "Li");
90+ /// object_builder.finish();
91+ /// // Finish the builder to get the metadata and value
92+ /// let (metadata, value) = builder.finish();
93+ /// // use the Variant API to verify the result
94+ /// let metadata = VariantMetadata::try_new(&metadata).unwrap();
95+ /// let variant = Variant::try_new(&metadata, &value).unwrap();
96+ /// let Variant::Object(variant_object) = variant else {
97+ /// panic!("unexpected variant type")
98+ /// };
99+ /// /* TODO: uncomment this, but now VariantObject:field is not implemented
100+ /// assert_eq!(
101+ /// variant_object.field("first_name").unwrap(),
102+ /// Variant::String("Jiaying")
103+ /// );
104+ /// assert_eq!(
105+ /// variant_object.field("last_name").unwrap(),
106+ /// Variant::String("Li")
107+ /// );
108+ /// */
109+ /// ```
110+ ///
111+ /// # Example: Create an Array
112+ ///
113+ /// This example shows how to create an array of integers: `[1, 2, 3]`.
114+ /// (this test actually fails at the moment)
115+ /// ```
116+ /// # use parquet_variant::{Variant, VariantBuilder, VariantMetadata};
117+ /// let mut builder = VariantBuilder::new();
118+ /// // Create an array builder that will write elements to the array
119+ /// let mut array_builder = builder.new_array();
120+ /// array_builder.append_value(1i8);
121+ /// array_builder.append_value(2i8);
122+ /// array_builder.append_value(3i8);
123+ /// // Finish the builder to get the metadata and value
124+ /// let (metadata, value) = builder.finish();
125+ /// // use the Variant API to verify the result
126+ /// let metadata = VariantMetadata::try_new(&metadata).unwrap();
127+ /// let variant = Variant::try_new(&metadata, &value).unwrap();
128+ /// let Variant::Object(variant_object) = variant else {
129+ /// panic!("unexpected variant type")
130+ /// };
131+ /// // TODO: VERIFY THE RESULT this, but now VariantObject:field is not implemented
132+ /// ```
133+ ///
134+ /// # Example: Array of objects
135+ ///
136+ /// THis example shows how to create an array of objects:
137+ /// ```json
138+ /// [
139+ /// {
140+ /// "first_name": "Jiaying",
141+ /// "last_name": "Li"
142+ /// },
143+ /// {
144+ /// "first_name": "Malthe",
145+ /// "last_name": "Karbo"
146+ /// }
147+ /// ```
148+ ///
149+ /// TODO
150+ ///
59151pub struct VariantBuilder {
60152 buffer : Vec < u8 > ,
61153 dict : HashMap < String , u32 > ,
@@ -122,10 +214,16 @@ impl VariantBuilder {
122214 self . buffer . len ( )
123215 }
124216
217+ /// Create an [`ArrayBuilder`] for creating [`Variant::Array`] values.
218+ ///
219+ /// See the examples on [`VariantBuilder`] for usage.
125220 pub fn new_array ( & mut self ) -> ArrayBuilder {
126221 ArrayBuilder :: new ( self )
127222 }
128223
224+ /// Create an [`ObjectBuilder`] for creating [`Variant::Object`] values.
225+ ///
226+ /// See the examples on [`VariantBuilder`] for usage.
129227 pub fn new_object ( & mut self ) -> ObjectBuilder {
130228 ObjectBuilder :: new ( self )
131229 }
@@ -198,6 +296,9 @@ impl Default for VariantBuilder {
198296 }
199297}
200298
299+ /// A builder for creating [`Variant::Array`] values.
300+ ///
301+ /// See the examples on [`VariantBuilder`] for usage.
201302pub struct ArrayBuilder < ' a > {
202303 parent : & ' a mut VariantBuilder ,
203304 start_pos : usize ,
@@ -255,6 +356,9 @@ impl<'a> ArrayBuilder<'a> {
255356 }
256357}
257358
359+ /// A builder for creating [`Variant::Object`] values.
360+ ///
361+ /// See the examples on [`VariantBuilder`] for usage.
258362pub struct ObjectBuilder < ' a > {
259363 parent : & ' a mut VariantBuilder ,
260364 start_pos : usize ,
0 commit comments