datafusion_proto/
common.rs1use 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}