use crate::errors::BigQueryError;
use crate::BigQueryResult;
use rand::RngExt;
use std::fmt::{Display, Formatter};
use std::str::FromStr;
#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct BigQueryJobId(String);
impl BigQueryJobId {
pub fn new(id: impl Into<String>) -> BigQueryResult<Self> {
let id = id.into();
if id.is_empty() {
return Err(BigQueryError::invalid_parameters(
"job_id",
"must not be empty",
));
}
if let Some(ch) = id.chars().find(|c| *c == '/' || c.is_control()) {
return Err(BigQueryError::invalid_parameters(
"job_id",
format!(
"must not contain / or control characters; got {ch:?} in \"{}\"",
id.escape_debug()
),
));
}
Ok(Self(id))
}
pub(crate) fn reported(id: String) -> Self {
Self(id)
}
pub(crate) fn random() -> Self {
Self(format!("bigquery_rs_{:032x}", rand::rng().random::<u128>()))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for BigQueryJobId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl TryFrom<&str> for BigQueryJobId {
type Error = BigQueryError;
fn try_from(id: &str) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl TryFrom<String> for BigQueryJobId {
type Error = BigQueryError;
fn try_from(id: String) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl FromStr for BigQueryJobId {
type Err = BigQueryError;
fn from_str(id: &str) -> Result<Self, Self::Err> {
Self::new(id)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct BigQueryQueryId(String);
impl BigQueryQueryId {
pub(crate) fn reported(id: String) -> Self {
Self(id)
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for BigQueryQueryId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
pub struct BigQueryRequestId(String);
impl BigQueryRequestId {
pub fn new(id: impl Into<String>) -> BigQueryResult<Self> {
let id = id.into();
if id.is_empty() {
return Err(BigQueryError::invalid_parameters(
"request_id",
"must not be empty",
));
}
Ok(Self(id))
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl Display for BigQueryRequestId {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl TryFrom<&str> for BigQueryRequestId {
type Error = BigQueryError;
fn try_from(id: &str) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl TryFrom<String> for BigQueryRequestId {
type Error = BigQueryError;
fn try_from(id: String) -> Result<Self, Self::Error> {
Self::new(id)
}
}
impl FromStr for BigQueryRequestId {
type Err = BigQueryError;
fn from_str(id: &str) -> Result<Self, Self::Err> {
Self::new(id)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_job_id_that_would_break_its_resource_path_is_refused() {
for bad in ["", "a/b", "a\nb"] {
match BigQueryJobId::new(bad) {
Err(BigQueryError::InvalidParametersError(err)) => {
assert_eq!(err.public.field, "job_id", "{bad:?}")
}
other => panic!("{bad:?}: expected an invalid job ID, got {other:?}"),
}
}
assert!(BigQueryJobId::new("bquxjob_1-a").is_ok());
}
#[test]
fn an_empty_request_id_is_refused() {
match BigQueryRequestId::new("") {
Err(BigQueryError::InvalidParametersError(err)) => {
assert_eq!(err.public.field, "request_id")
}
other => panic!("expected an invalid request ID, got {other:?}"),
}
assert_eq!(
BigQueryRequestId::new("req-1").expect("a key").as_str(),
"req-1"
);
}
}