Skip to content

Commit

Permalink
Apply rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann committed Sep 23, 2024
1 parent 33d7119 commit f957006
Show file tree
Hide file tree
Showing 23 changed files with 220 additions and 318 deletions.
3 changes: 1 addition & 2 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# General
edition = "2024" # Default: "2015"
unstable_features = true # Default: false
version = "Two" # Default: "One"
style_edition = "2024"

# Settings
condense_wildcard_suffixes = true # Default: false
reorder_impl_items = true # Default: false
use_field_init_shorthand = true # Default: false
use_try_shorthand = true # Default: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::collections::HashMap;

use serde::{Deserialize, Serialize};

use crate::{schema::DataType, url::VersionedUrl, Valid};
use crate::{Valid, schema::DataType, url::VersionedUrl};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(target_arch = "wasm32", derive(tsify::Tsify))]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use error_stack::Report;
use serde_json::Value as JsonValue;

use super::{extend_report, ConstraintError};
use super::{ConstraintError, extend_report};
use crate::schema::{DataType, JsonSchemaValueType};

pub(crate) fn check_array_constraints(
Expand All @@ -10,12 +10,9 @@ pub(crate) fn check_array_constraints(
result: &mut Result<(), Report<ConstraintError>>,
) {
if !data_type.json_type.contains(&JsonSchemaValueType::Array) {
extend_report!(
*result,
ConstraintError::InvalidType {
actual: JsonSchemaValueType::Array,
expected: data_type.json_type.clone()
}
);
extend_report!(*result, ConstraintError::InvalidType {
actual: JsonSchemaValueType::Array,
expected: data_type.json_type.clone()
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use error_stack::Report;

use super::{extend_report, ConstraintError};
use super::{ConstraintError, extend_report};
use crate::schema::{DataType, JsonSchemaValueType};

pub(crate) fn check_boolean_constraints(
Expand All @@ -9,12 +9,9 @@ pub(crate) fn check_boolean_constraints(
result: &mut Result<(), Report<ConstraintError>>,
) {
if !data_type.json_type.contains(&JsonSchemaValueType::Boolean) {
extend_report!(
*result,
ConstraintError::InvalidType {
actual: JsonSchemaValueType::Boolean,
expected: data_type.json_type.clone()
}
);
extend_report!(*result, ConstraintError::InvalidType {
actual: JsonSchemaValueType::Boolean,
expected: data_type.json_type.clone()
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use std::collections::HashSet;

use iso8601_duration::ParseDurationError;
use regex::Regex;
use serde_json::{json, Number as JsonNumber, Value as JsonValue};
use serde_json::{Number as JsonNumber, Value as JsonValue, json};
use thiserror::Error;

use crate::schema::{data_type::constraint::StringFormat, JsonSchemaValueType};
use crate::schema::{JsonSchemaValueType, data_type::constraint::StringFormat};

#[derive(Debug, Error)]
pub enum ConstraintError {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,5 @@ pub(crate) use self::{
null::check_null_constraints,
number::check_numeric_constraints,
object::check_object_constraints,
string::{check_string_constraints, StringFormat},
string::{StringFormat, check_string_constraints},
};
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use error_stack::Report;

use super::{extend_report, ConstraintError};
use super::{ConstraintError, extend_report};
use crate::schema::{DataType, JsonSchemaValueType};

pub(crate) fn check_null_constraints(
data_type: &DataType,
result: &mut Result<(), Report<ConstraintError>>,
) {
if !data_type.json_type.contains(&JsonSchemaValueType::Null) {
extend_report!(
*result,
ConstraintError::InvalidType {
actual: JsonSchemaValueType::Null,
expected: data_type.json_type.clone()
}
);
extend_report!(*result, ConstraintError::InvalidType {
actual: JsonSchemaValueType::Null,
expected: data_type.json_type.clone()
});
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use error_stack::Report;

use super::{extend_report, ConstraintError};
use super::{ConstraintError, extend_report};
use crate::schema::{DataType, JsonSchemaValueType};

pub(crate) fn check_numeric_constraints(
Expand All @@ -11,22 +11,19 @@ pub(crate) fn check_numeric_constraints(
if !data_type.json_type.contains(&JsonSchemaValueType::Number)
&& !data_type.json_type.contains(&JsonSchemaValueType::Integer)
{
extend_report!(
*result,
ConstraintError::InvalidType {
actual: JsonSchemaValueType::Number,
expected: data_type.json_type.clone()
}
);
extend_report!(*result, ConstraintError::InvalidType {
actual: JsonSchemaValueType::Number,
expected: data_type.json_type.clone()
});
}

if let Some(expected) = data_type.minimum {
if data_type.exclusive_minimum {
if actual <= expected {
extend_report!(
*result,
ConstraintError::ExclusiveMinimum { actual, expected }
);
extend_report!(*result, ConstraintError::ExclusiveMinimum {
actual,
expected
});
}
} else if actual < expected {
extend_report!(*result, ConstraintError::Minimum { actual, expected });
Expand All @@ -35,10 +32,10 @@ pub(crate) fn check_numeric_constraints(
if let Some(expected) = data_type.maximum {
if data_type.exclusive_maximum {
if actual >= expected {
extend_report!(
*result,
ConstraintError::ExclusiveMaximum { actual, expected }
);
extend_report!(*result, ConstraintError::ExclusiveMaximum {
actual,
expected
});
}
} else if actual > expected {
extend_report!(*result, ConstraintError::Maximum { actual, expected });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use error_stack::Report;

use super::{extend_report, ConstraintError};
use super::{ConstraintError, extend_report};
use crate::schema::{DataType, JsonSchemaValueType};

type JsonObject = serde_json::Map<String, serde_json::Value>;
Expand All @@ -11,12 +11,9 @@ pub(crate) fn check_object_constraints(
result: &mut Result<(), Report<ConstraintError>>,
) {
if !data_type.json_type.contains(&JsonSchemaValueType::Object) {
extend_report!(
*result,
ConstraintError::InvalidType {
actual: JsonSchemaValueType::Object,
expected: data_type.json_type.clone()
}
);
extend_report!(*result, ConstraintError::InvalidType {
actual: JsonSchemaValueType::Object,
expected: data_type.json_type.clone()
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use serde::{Deserialize, Serialize};
use url::{Host, Url};
use uuid::Uuid;

use super::{extend_report, ConstraintError};
use super::{ConstraintError, extend_report};
use crate::schema::{
data_type::constraint::error::StringFormatError, DataType, JsonSchemaValueType,
DataType, JsonSchemaValueType, data_type::constraint::error::StringFormatError,
};

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand Down Expand Up @@ -131,46 +131,34 @@ pub(crate) fn check_string_constraints(
result: &mut Result<(), Report<ConstraintError>>,
) {
if !data_type.json_type.contains(&JsonSchemaValueType::String) {
extend_report!(
*result,
ConstraintError::InvalidType {
actual: JsonSchemaValueType::String,
expected: data_type.json_type.clone()
}
);
extend_report!(*result, ConstraintError::InvalidType {
actual: JsonSchemaValueType::String,
expected: data_type.json_type.clone()
});
}

if let Some(expected) = data_type.min_length {
if actual.len() < expected {
extend_report!(
*result,
ConstraintError::MinLength {
actual: actual.to_owned(),
expected
}
);
extend_report!(*result, ConstraintError::MinLength {
actual: actual.to_owned(),
expected
});
}
}
if let Some(expected) = data_type.max_length {
if actual.len() > expected {
extend_report!(
*result,
ConstraintError::MaxLength {
actual: actual.to_owned(),
expected
}
);
extend_report!(*result, ConstraintError::MaxLength {
actual: actual.to_owned(),
expected
});
}
}
if let Some(expected) = &data_type.pattern {
if !expected.is_match(actual) {
extend_report!(
*result,
ConstraintError::Pattern {
actual: actual.to_owned(),
expected: expected.clone()
}
);
extend_report!(*result, ConstraintError::Pattern {
actual: actual.to_owned(),
expected: expected.clone()
});
}
}
if let Some(expected) = data_type.format {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ mod codec {

#[cfg(test)]
mod tests {
use serde_json::{self, json, Value as JsonValue};
use serde_json::{self, Value as JsonValue, json};

use super::*;

Expand Down
65 changes: 25 additions & 40 deletions libs/@blockprotocol/type-system/rust/src/schema/data_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ mod validation;

use alloc::sync::Arc;
use core::{cmp, fmt, mem};
use std::collections::{hash_map::RawEntryMut, HashMap, HashSet};
use std::collections::{HashMap, HashSet, hash_map::RawEntryMut};

use error_stack::{bail, Report};
use error_stack::{Report, bail};
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use thiserror::Error;

use crate::{
schema::data_type::constraint::{extend_report, ConstraintError, StringFormat},
schema::data_type::constraint::{ConstraintError, StringFormat, extend_report},
url::VersionedUrl,
};

Expand Down Expand Up @@ -263,26 +263,20 @@ impl OntologyTypeResolver {
.raw_entry_mut()
.from_key(&data_type.id)
.or_insert_with(|| {
(
data_type.id.clone(),
DataTypeCacheEntry {
data_type,
metadata: None,
},
)
(data_type.id.clone(), DataTypeCacheEntry {
data_type,
metadata: None,
})
});
}

pub fn add_closed(&mut self, data_type: Arc<DataType>, metadata: Arc<ClosedDataTypeMetadata>) {
match self.data_types.raw_entry_mut().from_key(&data_type.id) {
RawEntryMut::Vacant(entry) => {
entry.insert(
data_type.id.clone(),
DataTypeCacheEntry {
data_type,
metadata: Some(metadata),
},
);
entry.insert(data_type.id.clone(), DataTypeCacheEntry {
data_type,
metadata: Some(metadata),
});
}
RawEntryMut::Occupied(mut entry) => {
entry.insert(DataTypeCacheEntry {
Expand Down Expand Up @@ -525,23 +519,17 @@ impl DataType {

if let Some(const_value) = &self.const_value {
if value != const_value {
extend_report!(
result,
ConstraintError::Const {
actual: value.clone(),
expected: const_value.clone()
}
);
extend_report!(result, ConstraintError::Const {
actual: value.clone(),
expected: const_value.clone()
});
}
}
if !self.enum_values.is_empty() && !self.enum_values.contains(value) {
extend_report!(
result,
ConstraintError::Enum {
actual: value.clone(),
expected: self.enum_values.clone()
}
);
extend_report!(result, ConstraintError::Enum {
actual: value.clone(),
expected: self.enum_values.clone()
});
}

match value {
Expand All @@ -555,12 +543,9 @@ impl DataType {
if let Some(number) = number.as_f64() {
constraint::check_numeric_constraints(number, self, &mut result);
} else {
extend_report!(
result,
ConstraintError::InsufficientPrecision {
actual: number.clone()
}
);
extend_report!(result, ConstraintError::InsufficientPrecision {
actual: number.clone()
});
}
}
JsonValue::String(string) => {
Expand All @@ -586,11 +571,11 @@ mod tests {

use super::*;
use crate::{
Validator,
utils::tests::{
ensure_failed_deserialization, ensure_failed_validation, ensure_validation,
ensure_validation_from_str, JsonEqualityCheck,
JsonEqualityCheck, ensure_failed_deserialization, ensure_failed_validation,
ensure_validation, ensure_validation_from_str,
},
Validator,
};

#[tokio::test]
Expand Down
Loading

0 comments on commit f957006

Please sign in to comment.