datafusion_proto/
common.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use datafusion_common::{internal_datafusion_err, internal_err, Result};
19
20pub(crate) fn str_to_byte(s: &String, description: &str) -> Result<u8> {
21    if s.len() != 1 {
22        return internal_err!(
23            "Invalid CSV {description}: expected single character, got {s}"
24        );
25    }
26    Ok(s.as_bytes()[0])
27}
28
29pub(crate) fn byte_to_string(b: u8, description: &str) -> Result<String> {
30    let b = &[b];
31    let b = std::str::from_utf8(b).map_err(|_| {
32        internal_datafusion_err!(
33            "Invalid CSV {description}: can not represent {b:0x?} as utf8"
34        )
35    })?;
36    Ok(b.to_owned())
37}
38
39#[macro_export]
40macro_rules! convert_required {
41    ($PB:expr) => {{
42        if let Some(field) = $PB.as_ref() {
43            Ok(field.try_into()?)
44        } else {
45            Err(proto_error("Missing required field in protobuf"))
46        }
47    }};
48}
49
50#[macro_export]
51macro_rules! into_required {
52    ($PB:expr) => {{
53        if let Some(field) = $PB.as_ref() {
54            Ok(field.into())
55        } else {
56            Err(proto_error("Missing required field in protobuf"))
57        }
58    }};
59}
60
61#[macro_export]
62macro_rules! convert_box_required {
63    ($PB:expr) => {{
64        if let Some(field) = $PB.as_ref() {
65            field.as_ref().try_into()
66        } else {
67            Err(proto_error("Missing required field in protobuf"))
68        }
69    }};
70}