Skip to main content

recoco_core/ops/
sdk.rs

1// ReCoco is a Rust-only fork of CocoIndex, by [CocoIndex](https://CocoIndex)
2// Original code from CocoIndex is copyrighted by CocoIndex
3// SPDX-FileCopyrightText: 2025-2026 CocoIndex (upstream)
4// SPDX-FileContributor: CocoIndex Contributors
5//
6// All modifications from the upstream for ReCoco are copyrighted by Knitli Inc.
7// SPDX-FileCopyrightText: 2026 Knitli Inc. (ReCoco)
8// SPDX-FileContributor: Adam Poulemanos <adam@knit.li>
9//
10// Both the upstream CocoIndex code and the ReCoco modifications are licensed under the Apache-2.0 License.
11// SPDX-License-Identifier: Apache-2.0
12
13pub(crate) use crate::prelude::*;
14
15use crate::builder::plan::AnalyzedFieldReference;
16use crate::builder::plan::AnalyzedLocalFieldReference;
17
18pub use super::factory_bases::*;
19pub use super::interface::*;
20pub use crate::base::schema::*;
21pub use crate::base::spec::*;
22pub use crate::base::value::*;
23
24// Disambiguate the ExportTargetBuildOutput type.
25pub use super::factory_bases::TypedExportDataCollectionBuildOutput;
26pub use super::registry::ExecutorFactoryRegistry;
27
28#[derive(Debug, Serialize, Deserialize)]
29pub struct EmptySpec {}
30
31#[macro_export]
32macro_rules! fields_value {
33    ($($field:expr), +) => {
34        $crate::base::value::FieldValues { fields: std::vec![ $(($field).into()),+ ] }
35    };
36}
37
38pub struct SchemaBuilderFieldRef(AnalyzedLocalFieldReference);
39
40impl SchemaBuilderFieldRef {
41    pub fn to_field_ref(&self) -> AnalyzedFieldReference {
42        AnalyzedFieldReference {
43            local: self.0.clone(),
44            scope_up_level: 0,
45        }
46    }
47}
48pub struct StructSchemaBuilder<'a> {
49    base_fields_idx: Vec<u32>,
50    target: &'a mut StructSchema,
51}
52
53impl<'a> StructSchemaBuilder<'a> {
54    pub fn new(target: &'a mut StructSchema) -> Self {
55        Self {
56            base_fields_idx: Vec::new(),
57            target,
58        }
59    }
60
61    pub fn _set_description(&mut self, description: impl Into<Arc<str>>) {
62        self.target.description = Some(description.into());
63    }
64
65    pub fn add_field(&mut self, field_schema: FieldSchema) -> SchemaBuilderFieldRef {
66        let current_idx = self.target.fields.len() as u32;
67        Arc::make_mut(&mut self.target.fields).push(field_schema);
68        let mut fields_idx = self.base_fields_idx.clone();
69        fields_idx.push(current_idx);
70        SchemaBuilderFieldRef(AnalyzedLocalFieldReference { fields_idx })
71    }
72
73    pub fn _add_struct_field(
74        &mut self,
75        name: impl Into<FieldName>,
76        nullable: bool,
77        attrs: Arc<BTreeMap<String, serde_json::Value>>,
78    ) -> (StructSchemaBuilder<'_>, SchemaBuilderFieldRef) {
79        let field_schema = FieldSchema::new(
80            name.into(),
81            EnrichedValueType {
82                typ: ValueType::Struct(StructSchema {
83                    fields: Arc::new(Vec::new()),
84                    description: None,
85                }),
86                nullable,
87                attrs,
88            },
89        );
90        let local_ref = self.add_field(field_schema);
91        let struct_schema = match &mut Arc::make_mut(&mut self.target.fields)
92            .last_mut()
93            .unwrap()
94            .value_type
95            .typ
96        {
97            ValueType::Struct(s) => s,
98            _ => unreachable!(),
99        };
100        (
101            StructSchemaBuilder {
102                base_fields_idx: local_ref.0.fields_idx.clone(),
103                target: struct_schema,
104            },
105            local_ref,
106        )
107    }
108}