Skip to content

Commit

Permalink
Format Rust code using rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored Jan 2, 2024
1 parent 1b36dcb commit 2d4966c
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 69 deletions.
15 changes: 11 additions & 4 deletions azure-kusto-data/examples/query.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use azure_kusto_data::models::v2::Frame;
use azure_kusto_data::prelude::*;
use azure_kusto_data::{
KustoBool, KustoDateTime, KustoDecimal, KustoDynamic, KustoGuid, KustoInt, KustoLong,
KustoReal, KustoString, KustoTimespan,
};
use clap::Parser;
use futures::{pin_mut, TryStreamExt};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::error::Error;
use azure_kusto_data::{KustoBool, KustoDateTime, KustoDecimal, KustoDynamic, KustoGuid, KustoInt, KustoLong, KustoReal, KustoString, KustoTimespan};
use azure_kusto_data::models::v2::Frame;

/// Simple program to greet a person
#[derive(Parser, Debug, Clone)]
Expand Down Expand Up @@ -146,7 +149,7 @@ struct Item {
vreal: KustoReal,
vstr: KustoString,
vlong: KustoLong,
vguid: KustoGuid
vguid: KustoGuid,
}

async fn to_struct(args: &Args, client: &KustoClient) -> Result<(), Box<dyn Error>> {
Expand All @@ -172,7 +175,11 @@ async fn to_struct(args: &Args, client: &KustoClient) -> Result<(), Box<dyn Erro
.next()
.ok_or_else(|| "Expected to get a primary result, but got none".to_string())?;

let rows = results.rows.into_iter().map(|r| Value::Array(r.into_result().unwrap())).collect::<Vec<_>>();
let rows = results
.rows
.into_iter()
.map(|r| Value::Array(r.into_result().unwrap()))
.collect::<Vec<_>>();

let items = serde_json::from_value::<Vec<Item>>(Value::Array(rows))?;

Expand Down
2 changes: 1 addition & 1 deletion azure-kusto-data/src/arrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ use azure_core::error::{ErrorKind, ResultExt};
use serde_json::Value;

use crate::error::Result;
use crate::models::v2::Row::Values;
use crate::models::v2::{Column, DataTable};
use crate::models::ColumnType;
use crate::models::v2::Row::Values;
use crate::types::{KustoDateTime, KustoTimespan};

fn convert_array_string(values: Vec<Value>) -> Result<ArrayRef> {
Expand Down
4 changes: 2 additions & 2 deletions azure-kusto-data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ use crate::operations::query::{QueryRunner, QueryRunnerBuilder, V1QueryRunner, V
use azure_core::{ClientOptions, Pipeline};

use crate::client_details::ClientDetails;
use crate::models::v2::Row;
use crate::prelude::ClientRequestProperties;
use azure_core::headers::Headers;
use azure_core::prelude::{Accept, AcceptEncoding, ClientVersion, ContentType};
use serde::de::DeserializeOwned;
use serde_json::Value;
use std::convert::TryFrom;
use std::fmt::Debug;
use std::sync::Arc;
use serde_json::Value;
use crate::models::v2::Row;

/// Options for specifying how a Kusto client will behave
#[derive(Clone, Default)]
Expand Down
3 changes: 1 addition & 2 deletions azure-kusto-data/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ impl TokenCredential for ConstTokenCredential {

/// Uses a user provided callback that accepts the resource and returns a token in order to authenticate.
pub struct CallbackTokenCredential {
pub(crate) token_callback:
TokenCallbackFunction,
pub(crate) token_callback: TokenCallbackFunction,
pub(crate) time_to_live: Option<Duration>,
}

Expand Down
4 changes: 2 additions & 2 deletions azure-kusto-data/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
use azure_core::StatusCode;
use std::fmt::Debug;

use thiserror;
use crate::models::v2::OneApiError;
use thiserror;
/// Error type for kusto operations.
#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -53,7 +53,7 @@ pub enum Error {

/// Errors raised from the api calls to kusto
#[error("Query API error: {0}")]
QueryApiError(OneApiError)
QueryApiError(OneApiError),
}

#[derive(thiserror::Error, Debug)]
Expand Down
8 changes: 6 additions & 2 deletions azure-kusto-data/src/models/v2/errors.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::{Display, Formatter};
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};

#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
#[serde(rename_all = "PascalCase")]
Expand All @@ -10,7 +10,11 @@ pub struct OneApiError {

impl Display for OneApiError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", serde_json::to_string_pretty(&self.error_message).unwrap())
write!(
f,
"{}",
serde_json::to_string_pretty(&self.error_message).unwrap()
)
}
}

Expand Down
16 changes: 9 additions & 7 deletions azure-kusto-data/src/operations/v2.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::error::{Error::JsonError, Result};
use crate::models::v2;
use futures::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, Stream, stream};
use crate::error::{Result, Error::JsonError};
use futures::{stream, AsyncBufRead, AsyncBufReadExt, AsyncReadExt, Stream};

pub fn parse_frames_iterative(reader: impl AsyncBufRead + Unpin) -> impl Stream<Item = Result<v2::Frame>> {
pub fn parse_frames_iterative(
reader: impl AsyncBufRead + Unpin,
) -> impl Stream<Item = Result<v2::Frame>> {
let buf = Vec::with_capacity(4096);
stream::unfold((reader, buf), |(mut reader, mut buf)| async move {
let size = reader.read_until(b'\n', &mut buf).await.ok()?;
Expand All @@ -14,17 +16,17 @@ pub fn parse_frames_iterative(reader: impl AsyncBufRead + Unpin) -> impl Stream<
return None;
}

Some((serde_json::from_slice(&buf[1..size]).map_err(JsonError), (reader, buf)))
Some((
serde_json::from_slice(&buf[1..size]).map_err(JsonError),
(reader, buf),
))
})
}


pub async fn parse_frames_full(
mut reader: (impl AsyncBufRead + Send + Unpin),
) -> Result<Vec<v2::Frame>> {
let mut buf = Vec::new();
reader.read_to_end(&mut buf).await?;
return Ok(serde_json::from_slice(&buf)?);
}


15 changes: 8 additions & 7 deletions azure-kusto-data/src/types/datetime.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::fmt::Display;
use std::str::FromStr;
use crate::error::{Error, ParseError};
use derive_more::{From, Into};
use serde::{Deserialize, Serialize};
use time::{OffsetDateTime};
use std::fmt::Display;
use std::str::FromStr;
use time::format_description::well_known::Rfc3339;
use crate::error::{Error, ParseError};
use time::OffsetDateTime;

/// Datetime for kusto, for serialization and deserialization.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Into, Debug)]
Expand Down Expand Up @@ -44,7 +44,8 @@ impl<'de> Deserialize<'de> for KustoDateTime {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let opt = Option::<String>::deserialize(deserializer)?;
if let Some(s) = opt {
Ok(s.parse::<KustoDateTime>().map_err(|e| serde::de::Error::custom(e.to_string()))?)
Ok(s.parse::<KustoDateTime>()
.map_err(|e| serde::de::Error::custom(e.to_string()))?)
} else {
Ok(Self::null())
}
Expand All @@ -55,8 +56,8 @@ impl FromStr for KustoDateTime {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self::new(OffsetDateTime::parse(s, &Rfc3339).map_err(
|e| Error::from(ParseError::DateTime(e)))?
Ok(Self::new(
OffsetDateTime::parse(s, &Rfc3339).map_err(|e| Error::from(ParseError::DateTime(e)))?,
))
}
}
Expand Down
19 changes: 8 additions & 11 deletions azure-kusto-data/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
//! Types used for serialization and deserialization of ADX data.
use std::convert::Infallible;
use derive_more::{Display, From, Into, FromStr};
use crate::error::{Error, ParseError};
use derive_more::{Display, From, FromStr, Into};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::convert::Infallible;
use std::fmt::Debug;
use rust_decimal::Decimal;
use crate::error::{Error, ParseError};

mod timespan;
mod datetime;
mod timespan;

macro_rules! kusto_type {
($name:ident, $type:ty, primitive) => {
Expand Down Expand Up @@ -64,11 +64,9 @@ macro_rules! kusto_from_str {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(
Self::new(
s.parse::<$t>().map_err(|e| Error::from($err(e)))?
)
)
Ok(Self::new(
s.parse::<$t>().map_err(|e| Error::from($err(e)))?,
))
}
}
};
Expand All @@ -92,7 +90,6 @@ kusto_from_str!(KustoReal, f64, ParseError::Float);
kusto_from_str!(KustoDecimal, Decimal, ParseError::Decimal);
kusto_from_str!(KustoGuid, uuid::Uuid, ParseError::Guid);


impl FromStr for KustoString {
type Err = Infallible;

Expand Down
37 changes: 19 additions & 18 deletions azure-kusto-data/src/types/timespan.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::error::{Error};
use crate::error::Error;
use crate::error::ParseError;
use derive_more::{From, Into};
use once_cell::sync::Lazy;
use regex::{Captures, Regex};
use std::fmt::{Debug, Display, Formatter};
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use std::fmt::{Debug, Display, Formatter};
use std::num::TryFromIntError;
use derive_more::{From, Into};
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use time::Duration;
use crate::error::ParseError;



fn parse_regex_segment(captures: &Captures, name: &str) -> i64 {
captures
Expand Down Expand Up @@ -61,7 +59,6 @@ impl KustoTimespan {
}
}


impl FromStr for KustoTimespan {
type Err = Error;

Expand All @@ -82,10 +79,10 @@ impl FromStr for KustoTimespan {
let nanos = parse_regex_segment(&captures, "nanos");
let duration = neg
* (Duration::days(days)
+ Duration::hours(hours)
+ Duration::minutes(minutes)
+ Duration::seconds(seconds)
+ Duration::nanoseconds(nanos * 100)); // Ticks
+ Duration::hours(hours)
+ Duration::minutes(minutes)
+ Duration::seconds(seconds)
+ Duration::nanoseconds(nanos * 100)); // Ticks

Ok(Self(Some(duration)))
}
Expand All @@ -102,7 +99,6 @@ impl Display for KustoTimespan {
} else {
write!(f, "null")
}

}
}

Expand Down Expand Up @@ -131,19 +127,22 @@ impl<'de> Deserialize<'de> for KustoTimespan {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let opt = Option::<String>::deserialize(deserializer)?;
if let Some(s) = opt {
Ok(s.parse::<KustoTimespan>().map_err(|e| serde::de::Error::custom(e.to_string()))?)
Ok(s.parse::<KustoTimespan>()
.map_err(|e| serde::de::Error::custom(e.to_string()))?)
} else {
Ok(Self::null())
}
}
}


impl TryFrom<std::time::Duration> for KustoTimespan {
type Error = TryFromIntError;

fn try_from(d: std::time::Duration) -> Result<Self, Self::Error> {
Ok(Self(Some(Duration::new(d.as_secs().try_into()?, d.subsec_nanos().try_into()?))))
Ok(Self(Some(Duration::new(
d.as_secs().try_into()?,
d.subsec_nanos().try_into()?,
))))
}
}

Expand Down Expand Up @@ -174,7 +173,9 @@ mod tests {
assert_eq!(
KustoTimespan::from_str(from)
.unwrap_or_else(|_| panic!("Failed to parse duration {}", from))
.0.unwrap().whole_nanoseconds(),
.0
.unwrap()
.whole_nanoseconds(),
i128::from(to)
);
}
Expand Down
Loading

0 comments on commit 2d4966c

Please sign in to comment.