use crate::errors::BigQueryError;
use crate::{BigQueryResult, BigQueryTableRef};
use serde::{Deserialize, Serialize, Serializer};
use std::borrow::Cow;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
const MAX_ID_BYTES: usize = 1024;
const FORBIDDEN_ASCII: &[u8] = b"`'\"\\./$@";
#[derive(Clone, Copy)]
enum IdViolation {
Empty,
TooLong {
length: usize,
},
InvalidChar {
at: usize,
},
}
impl IdViolation {
const fn rule(self) -> &'static str {
match self {
Self::Empty => "must not be empty",
Self::TooLong { .. } => "must be at most 1,024 UTF-8 bytes",
Self::InvalidChar { .. } => {
"must not contain control characters or any of ` ' \" \\ . / $ @"
}
}
}
fn into_error(self, field: &'static str, id: &str) -> BigQueryError {
let rule = self.rule();
let error = match self {
Self::Empty => rule.to_string(),
Self::TooLong { length } => format!("{rule}, was {length} bytes"),
Self::InvalidChar { at } => {
let ch = id[at..]
.chars()
.next()
.expect("`at` is the start of the character the check rejected");
format!(
"{rule}; got {ch:?} at byte {at} of \"{}\"",
id.escape_debug()
)
}
};
BigQueryError::invalid_parameters(field, error)
}
}
const fn check_id(id: &str) -> Result<(), IdViolation> {
let bytes = id.as_bytes();
let length = bytes.len();
if length == 0 {
return Err(IdViolation::Empty);
}
if length > MAX_ID_BYTES {
return Err(IdViolation::TooLong { length });
}
let mut index = 0;
while index < length {
let byte = bytes[index];
let c1_control = byte == 0xC2
&& index + 1 < length
&& bytes[index + 1] >= 0x80
&& bytes[index + 1] <= 0x9F;
if byte.is_ascii_control() || c1_control || is_forbidden_ascii(byte) {
return Err(IdViolation::InvalidChar { at: index });
}
index += 1;
}
Ok(())
}
const fn is_forbidden_ascii(byte: u8) -> bool {
let mut index = 0;
while index < FORBIDDEN_ASCII.len() {
if FORBIDDEN_ASCII[index] == byte {
return true;
}
index += 1;
}
false
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize)]
#[serde(try_from = "String")]
pub struct BigQueryDatasetId(Cow<'static, str>);
impl BigQueryDatasetId {
pub fn new(id: impl Into<String>) -> BigQueryResult<Self> {
let id = id.into();
match check_id(&id) {
Ok(()) => Ok(Self(Cow::Owned(id))),
Err(violation) => Err(violation.into_error("dataset_id", &id)),
}
}
pub const fn from_static(id: &'static str) -> Self {
match check_id(id) {
Ok(()) => Self(Cow::Borrowed(id)),
Err(violation) => panic!("{}", violation.rule()),
}
}
pub fn table(&self, table: BigQueryTableId) -> BigQueryTableRef {
BigQueryTableRef::new(None, self.clone(), table)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for BigQueryDatasetId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.0.as_ref(), f)
}
}
impl TryFrom<&str> for BigQueryDatasetId {
type Error = BigQueryError;
fn try_from(id: &str) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl TryFrom<String> for BigQueryDatasetId {
type Error = BigQueryError;
fn try_from(id: String) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl FromStr for BigQueryDatasetId {
type Err = BigQueryError;
fn from_str(id: &str) -> Result<Self, Self::Err> {
Self::new(id)
}
}
impl Serialize for BigQueryDatasetId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.0)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash, Deserialize)]
#[serde(try_from = "String")]
pub struct BigQueryTableId(Cow<'static, str>);
impl BigQueryTableId {
pub fn new(id: impl Into<String>) -> BigQueryResult<Self> {
let id = id.into();
match check_id(&id) {
Ok(()) => Ok(Self(Cow::Owned(id))),
Err(violation) => Err(violation.into_error("table_id", &id)),
}
}
pub const fn from_static(id: &'static str) -> Self {
match check_id(id) {
Ok(()) => Self(Cow::Borrowed(id)),
Err(violation) => panic!("{}", violation.rule()),
}
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for BigQueryTableId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
Display::fmt(self.0.as_ref(), f)
}
}
impl TryFrom<&str> for BigQueryTableId {
type Error = BigQueryError;
fn try_from(id: &str) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl TryFrom<String> for BigQueryTableId {
type Error = BigQueryError;
fn try_from(id: String) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl FromStr for BigQueryTableId {
type Err = BigQueryError;
fn from_str(id: &str) -> Result<Self, Self::Err> {
Self::new(id)
}
}
impl Serialize for BigQueryTableId {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::errors::BigQueryInvalidParametersError;
use proptest::prelude::*;
fn invalid<T: std::fmt::Debug>(result: BigQueryResult<T>) -> (String, String) {
match result {
Err(BigQueryError::InvalidParametersError(BigQueryInvalidParametersError {
public,
})) => (public.field, public.error),
other => panic!("expected an invalid-parameters error, got {other:?}"),
}
}
#[test]
fn ids_that_could_reshape_a_path_or_sql_are_rejected() {
let over_limit = "é".repeat(513);
let cases = [
("", "empty"),
(over_limit.as_str(), "over 1,024 bytes"),
("ord\0ers", "NUL"),
("ord\ners", "newline"),
("ord\rers", "carriage return"),
("ord\ters", "tab"),
("ord\u{7f}ers", "DEL"),
("ord\u{85}ers", "C1 control"),
("ord`ers", "backtick"),
("ord'ers", "single quote"),
("ord\"ers", "double quote"),
("ord\\ers", "backslash"),
("shop.orders", "part separator"),
("shop/orders", "path separator"),
("orders$20250101", "partition decorator"),
("orders@1700000000000", "snapshot decorator"),
];
for (id, class) in cases {
assert_eq!(
invalid(BigQueryDatasetId::new(id)).0,
"dataset_id",
"{class}: {id:?}"
);
assert_eq!(
invalid(BigQueryTableId::new(id)).0,
"table_id",
"{class}: {id:?}"
);
}
}
#[test]
fn other_ids_are_left_to_bigquery() {
let at_limit = "é".repeat(512);
for id in [
"orders",
"shop-eu",
"table 01",
"étudiant-01",
"00_お客様",
"ord😀ers",
"a+b;c",
at_limit.as_str(),
] {
assert!(BigQueryDatasetId::new(id).is_ok(), "{id:?}");
assert!(BigQueryTableId::new(id).is_ok(), "{id:?}");
}
}
#[test]
fn an_invalid_character_error_carries_no_raw_control_character() {
let (_, message) = invalid(BigQueryDatasetId::new("sh\nop"));
assert!(!message.contains('\n'), "{message:?}");
}
#[test]
fn an_oversized_id_error_leaves_the_value_out() {
let (_, message) = invalid(BigQueryTableId::new("x".repeat(10_000)));
assert!(!message.contains(&"x".repeat(10)), "{message}");
}
const SHOP: BigQueryDatasetId = BigQueryDatasetId::from_static("shop");
static ORDERS: BigQueryTableId = BigQueryTableId::from_static("orders");
#[test]
fn from_static_agrees_with_new() {
assert_eq!(
SHOP,
BigQueryDatasetId::new("shop").expect("valid test input")
);
assert_eq!(
ORDERS,
BigQueryTableId::new("orders").expect("valid test input")
);
}
#[test]
#[should_panic]
fn from_static_panics_at_run_time_on_a_bad_literal() {
let _ = BigQueryDatasetId::from_static("shop.eu");
}
#[test]
fn deserializing_validates() {
let shop: BigQueryDatasetId = serde_json::from_str("\"shop\"").expect("valid test input");
assert_eq!(shop, BigQueryDatasetId::from_static("shop"));
assert!(serde_json::from_str::<BigQueryDatasetId>("\"shop.eu\"").is_err());
assert!(serde_json::from_str::<BigQueryTableId>("\"a.b\"").is_err());
assert_eq!(
serde_json::to_string(&ORDERS).expect("valid test input"),
"\"orders\""
);
}
proptest! {
#[test]
fn the_id_check_agrees_with_char(id in any::<String>()) {
let expected = !id.is_empty()
&& id.len() <= 1024
&& !id.chars().any(|c| c.is_control() || "`'\"\\./$@".contains(c));
prop_assert_eq!(check_id(&id).is_ok(), expected);
}
}
}