risingwave_connector_codec/
lib.rs

1// Copyright 2024 RisingWave Labs
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Encoding and decoding between external data formats and RisingWave datum (i.e., type mappings).
16
17#![feature(coroutines)]
18#![feature(proc_macro_hygiene)]
19#![feature(stmt_expr_attributes)]
20#![feature(box_patterns)]
21#![feature(trait_alias)]
22#![feature(box_into_inner)]
23#![feature(type_alias_impl_trait)]
24#![feature(associated_type_defaults)]
25#![feature(impl_trait_in_assoc_type)]
26#![feature(iter_from_coroutine)]
27#![feature(if_let_guard)]
28#![feature(iterator_try_collect)]
29#![feature(try_blocks)]
30#![feature(error_generic_member_access)]
31#![feature(negative_impls)]
32#![feature(register_tool)]
33#![feature(assert_matches)]
34#![register_tool(rw)]
35#![recursion_limit = "256"]
36
37pub mod common;
38/// Converts JSON/AVRO/Protobuf data to RisingWave datum.
39/// The core API is [`decoder::Access`].
40pub mod decoder;
41
42pub use apache_avro::schema::Schema as AvroSchema;
43pub use apache_avro::types::{Value as AvroValue, ValueKind as AvroValueKind};
44pub use risingwave_pb::plan_common::ColumnDesc;
45pub struct JsonSchema(pub serde_json::Value);
46impl JsonSchema {
47    pub fn parse_str(schema: &str) -> anyhow::Result<Self> {
48        use anyhow::Context;
49
50        let value = serde_json::from_str(schema).context("failed to parse json schema")?;
51        Ok(Self(value))
52    }
53
54    pub fn parse_bytes(schema: &[u8]) -> anyhow::Result<Self> {
55        use anyhow::Context;
56
57        let value = serde_json::from_slice(schema).context("failed to parse json schema")?;
58        Ok(Self(value))
59    }
60}