Skip to content

Commit

Permalink
fmt + dry
Browse files Browse the repository at this point in the history
  • Loading branch information
Weakky committed Dec 8, 2023
1 parent c667de4 commit 44c2cbc
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 22 deletions.
9 changes: 1 addition & 8 deletions psl/builtin-connectors/src/cockroach_datamodel_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,14 +323,7 @@ impl Connector for CockroachDatamodelConnector {
CockroachType::Timestamp(_) => crate::utils::parse_timestamp(str),
CockroachType::Date => crate::utils::parse_date(str),
CockroachType::Time(_) => crate::utils::parse_time(str),
CockroachType::Timetz(_) => {
// We currently don't support time with timezone.
// We strip the timezone information and parse it as a time.
// This is inline with what Quaint does already.
let time_without_tz = str.split("+").next().unwrap();

crate::utils::parse_time(time_without_tz)
}
CockroachType::Timetz(_) => crate::utils::parse_timetz(str),
_ => unreachable!(),
},
None => crate::utils::parse_timestamptz(str),
Expand Down
6 changes: 1 addition & 5 deletions psl/builtin-connectors/src/postgres_datamodel_connector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -583,11 +583,7 @@ impl Connector for PostgresDatamodelConnector {
Timestamp(_) => crate::utils::parse_timestamp(str),
Date => crate::utils::parse_date(str),
Time(_) => crate::utils::parse_time(str),
Timetz(_) => {
let time_without_tz = str.split('+').next().unwrap();

crate::utils::parse_time(time_without_tz)
}
Timetz(_) => crate::utils::parse_timetz(str),
_ => unreachable!(),
},
None => crate::utils::parse_timestamptz(str),
Expand Down
9 changes: 9 additions & 0 deletions psl/builtin-connectors/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ pub(crate) fn parse_time(str: &str) -> Result<DateTime<FixedOffset>, chrono::Par
.map(DateTime::<FixedOffset>::from)
}

pub(crate) fn parse_timetz(str: &str) -> Result<DateTime<FixedOffset>, chrono::ParseError> {
// We currently don't support time with timezone.
// We strip the timezone information and parse it as a time.
// This is inline with what Quaint does already.
let time_without_tz = str.split('+').next().unwrap();

parse_time(time_without_tz)
}

pub(crate) fn parse_money(str: &str) -> Result<BigDecimal, ParseBigDecimalError> {
// We strip out the currency sign from the string.
BigDecimal::from_str(&str[1..]).map(|bd| bd.normalized())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,24 +100,24 @@ pub(crate) fn coerce_json_scalar_to_pv(value: serde_json::Value, sf: &ScalarFiel
serde_json::Value::Bool(b) => Ok(PrismaValue::Boolean(b)),
serde_json::Value::Number(n) => match sf.type_identifier() {
TypeIdentifier::Int => Ok(PrismaValue::Int(n.as_i64().ok_or_else(|| {
build_conversion_error(&sf, &format!("Number({n})"), &format!("{:?}", sf.type_identifier()))
build_conversion_error(sf, &format!("Number({n})"), &format!("{:?}", sf.type_identifier()))
})?)),
TypeIdentifier::BigInt => Ok(PrismaValue::BigInt(n.as_i64().ok_or_else(|| {
build_conversion_error(&sf, &format!("Number({n})"), &format!("{:?}", sf.type_identifier()))
build_conversion_error(sf, &format!("Number({n})"), &format!("{:?}", sf.type_identifier()))
})?)),
TypeIdentifier::Float | TypeIdentifier::Decimal => {
let bd = n
.as_f64()
.and_then(BigDecimal::from_f64)
.map(|bd| bd.normalized())
.ok_or_else(|| {
build_conversion_error(&sf, &format!("Number({n})"), &format!("{:?}", sf.type_identifier()))
build_conversion_error(sf, &format!("Number({n})"), &format!("{:?}", sf.type_identifier()))
})?;

Ok(PrismaValue::Float(bd))
}
_ => Err(build_conversion_error(
&sf,
sf,
&format!("Number({n})"),
&format!("{:?}", sf.type_identifier()),
)),
Expand All @@ -128,7 +128,7 @@ pub(crate) fn coerce_json_scalar_to_pv(value: serde_json::Value, sf: &ScalarFiel
TypeIdentifier::DateTime => {
let res = sf.coerce_json_datetime(&s).map_err(|err| {
build_conversion_error_with_reason(
&sf,
sf,
&format!("String({s})"),
&format!("{:?}", sf.type_identifier()),
&err.to_string(),
Expand All @@ -140,7 +140,7 @@ pub(crate) fn coerce_json_scalar_to_pv(value: serde_json::Value, sf: &ScalarFiel
TypeIdentifier::Decimal => {
let res = sf.coerce_json_decimal(&s).map_err(|err| {
build_conversion_error_with_reason(
&sf,
sf,
&format!("String({s})"),
&format!("{:?}", sf.type_identifier()),
&err.to_string(),
Expand All @@ -151,7 +151,7 @@ pub(crate) fn coerce_json_scalar_to_pv(value: serde_json::Value, sf: &ScalarFiel
}
TypeIdentifier::UUID => Ok(PrismaValue::Uuid(uuid::Uuid::parse_str(&s).map_err(|err| {
build_conversion_error_with_reason(
&sf,
sf,
&format!("String({s})"),
&format!("{:?}", sf.type_identifier()),
&err.to_string(),
Expand All @@ -161,7 +161,7 @@ pub(crate) fn coerce_json_scalar_to_pv(value: serde_json::Value, sf: &ScalarFiel
// We skip the first two characters because there's the \x prefix.
let bytes = hex::decode(&s[2..]).map_err(|err| {
build_conversion_error_with_reason(
&sf,
sf,
&format!("String({s})"),
&format!("{:?}", sf.type_identifier()),
&err.to_string(),
Expand All @@ -171,7 +171,7 @@ pub(crate) fn coerce_json_scalar_to_pv(value: serde_json::Value, sf: &ScalarFiel
Ok(PrismaValue::Bytes(bytes))
}
_ => Err(build_conversion_error(
&sf,
sf,
&format!("String({s})"),
&format!("{:?}", sf.type_identifier()),
)),
Expand Down

0 comments on commit 44c2cbc

Please sign in to comment.