Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/constraints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ pub struct Report<R> {
/// # type ConstraintB = DocConstraint;
/// #
/// # let schema: Schema =
/// # SchemaBuilder::new(Identifier::try_new("MySchema").unwrap()).build();
/// # SchemaBuilder::new(Identifier::try_new("MySchema").unwrap()).build().unwrap();
///
/// let report = validate::<(ConstraintA, ConstraintB)>(&schema);
/// assert!(report.base_constraints.is_ok());
Expand Down
43 changes: 22 additions & 21 deletions crates/constraints/tests/validator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ use quent_schema::{
/// Annotations carrying the constraint `name` without data.
fn constraint(name: &str) -> Annotations {
AnnotationsBuilder::new()
.try_with_constraint(name, None)
.unwrap()
.with_constraint(name, None)
.build()
.unwrap()
}

fn empty_schema() -> Schema {
Expand Down Expand Up @@ -97,7 +97,8 @@ fn passing_constraint_on_empty_schema() {
fn constraint_without_validator_is_unregistered() {
let schema = SchemaBuilder::new(ident("TestSchema"))
.with_annotations(constraint("unknown"))
.build();
.build()
.unwrap();
let report = validate::<(NoopA,)>(&schema);
assert_eq!(report.unregistered_constraints.len(), 1);
assert!(
Expand All @@ -114,11 +115,12 @@ fn metadata_is_never_validated() {
let schema = SchemaBuilder::new(ident("TestSchema"))
.with_annotations(
AnnotationsBuilder::new()
.try_with_metadata("not_validated", None)
.unwrap()
.build(),
.with_metadata("not_validated", None)
.build()
.unwrap(),
)
.build();
.build()
.unwrap();
let report = validate::<(NoopA,)>(&schema);
assert!(report.unregistered_constraints.is_empty());
}
Expand All @@ -128,29 +130,27 @@ fn unregistered_constraint_is_reported_once() {
let unknown = || constraint("unknown");
let field = Field::new(ident("ef"), DataType::U64, unknown());
let event = EventBuilder::new(ident("Ev"), Cardinality::Once)
.try_with_field(field)
.unwrap()
.with_field(field)
.with_annotations(unknown())
.build();
.build()
.unwrap();
let entity = EntityBuilder::new(ident("E"))
.try_with_event(event)
.unwrap()
.with_event(event)
.with_annotations(unknown())
.build()
.unwrap();
let record_field = Field::new(ident("rf"), DataType::U64, unknown());
let record = RecordBuilder::new(ident("R"))
.try_with_field(record_field)
.unwrap()
.with_field(record_field)
.with_annotations(unknown())
.build();
.build()
.unwrap();
let schema = SchemaBuilder::new(ident("S"))
.try_with_entity(entity)
.unwrap()
.try_with_record(record)
.unwrap()
.with_entity(entity)
.with_record(record)
.with_annotations(unknown())
.build();
.build()
.unwrap();
let report = validate::<(NoopA,)>(&schema);
// The same name used at six sites is deduplicated to a single entry.
assert_eq!(
Expand All @@ -174,7 +174,8 @@ fn constraint_failure_is_reported_per_constraint() {
fn unregistered_and_failure_aggregate() {
let schema = SchemaBuilder::new(ident("TestSchema"))
.with_annotations(constraint("unknown"))
.build();
.build()
.unwrap();
let report = validate::<(Failing,)>(&schema);
assert!(
report
Expand Down
30 changes: 15 additions & 15 deletions crates/fsm/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum FsmEntityBuilderError {
NoExitState,
#[error("duplicate state `{0}`")]
DuplicateState(Identifier),
/// A duplicate attribute name within a state reached the schema builder.
/// The generated schema element is invalid.
#[error(transparent)]
Build(#[from] BuilderError),
/// The FSM topology failed to serialize to its constraint payload.
Expand Down Expand Up @@ -119,7 +119,7 @@ impl FsmEntityBuilder {
.map(|s| s.name.clone())
.collect();
let initial = match initials.as_slice() {
[one] => one.clone(),
[initial] => initial.clone(),
[] => return Err(FsmEntityBuilderError::NoInitialState),
_ => return Err(FsmEntityBuilderError::MultipleInitialStates(initials)),
};
Expand All @@ -132,8 +132,7 @@ impl FsmEntityBuilder {
let Some((first_exit, other_exits)) = exits.split_first() else {
return Err(FsmEntityBuilderError::NoExitState);
};

let transitions: Vec<Transition> = states
let transitions = states
.iter()
.flat_map(|state| {
let source = state.name.clone();
Expand All @@ -155,22 +154,23 @@ impl FsmEntityBuilder {
// reports it, so `Once` here is only a stand-in.
let cardinality = fsm.cardinality(&state.name).unwrap_or(Cardinality::Once);
let event = EventBuilder::new(state.name, cardinality)
.try_with_fields(state.attributes)?
.build();
entity = entity.try_with_event(event)?;
.with_fields(state.attributes)
.build()?;
entity = entity.with_event(event);
}

annotations.set_constraint(FsmConstraint::NAME, Some(fsm.constraint_data()?));
let entity = entity.with_annotations(annotations.build()).build()?;
annotations =
annotations.with_constraint(FsmConstraint::NAME, Some(fsm.constraint_data()?));
let entity = entity.with_annotations(annotations.build()?).build()?;

// Validate the full topology now, the same checks the constraint runs
// during schema validation, so a built entity is always valid.
let mut errors = Vec::new();
check_entity(&entity, &fsm, &mut errors);
match errors.len() {
0 => Ok(entity),
1 => Err(FsmEntityBuilderError::Invalid(errors.pop().unwrap())),
_ => Err(FsmEntityBuilderError::Invalid(FsmError::Multiple(errors))),
let mut topology_errors = Vec::new();
check_entity(&entity, &fsm, &mut topology_errors);
if let Some(error) = topology_errors.into_iter().next() {
Err(FsmEntityBuilderError::Invalid(error))
} else {
Ok(entity)
}
}
}
13 changes: 5 additions & 8 deletions crates/fsm/tests/fsm-constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ fn fsm(initial: &str, transitions: &[(&str, &str)], exit: &[&str]) -> String {
/// Annotations carrying the FSM constraint with `data`.
fn fsm_annotations(data: Option<String>) -> Annotations {
AnnotationsBuilder::new()
.try_with_constraint(FsmConstraint::NAME, data)
.unwrap()
.with_constraint(FsmConstraint::NAME, data)
.build()
.unwrap()
}

fn entity_with(name: &str, events: Vec<Event>, data: &str) -> Entity {
EntityBuilder::new(ident(name))
.try_with_events(events)
.unwrap()
.with_events(events)
.with_annotations(fsm_annotations(Some(data.to_string())))
.build()
.unwrap()
Expand Down Expand Up @@ -96,8 +95,7 @@ fn single_state_fsm_passes() {
#[test]
fn missing_data_is_rejected() {
let entity = EntityBuilder::new(ident("E"))
.try_with_event(event("a", Cardinality::Once))
.unwrap()
.with_event(event("a", Cardinality::Once))
.with_annotations(fsm_annotations(None))
.build()
.unwrap();
Expand All @@ -112,8 +110,7 @@ fn missing_data_is_rejected() {
#[test]
fn invalid_json_is_rejected() {
let entity = EntityBuilder::new(ident("E"))
.try_with_event(event("a", Cardinality::Once))
.unwrap()
.with_event(event("a", Cardinality::Once))
.with_annotations(fsm_annotations(Some("{ trash".to_string())))
.build()
.unwrap();
Expand Down
16 changes: 9 additions & 7 deletions crates/instrumentation-build/src/any_event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,22 @@ mod tests {

fn entity(name: &str, event: &str) -> quent_schema::Entity {
EntityBuilder::new(ident(name))
.try_with_event(EventBuilder::new(ident(event), Cardinality::Once).build())
.unwrap()
.with_event(
EventBuilder::new(ident(event), Cardinality::Once)
.build()
.unwrap(),
)
.build()
.unwrap()
}

#[test]
fn emits_a_variant_and_arm_per_entity() {
let schema = SchemaBuilder::new(ident("Demo"))
.try_with_entity(entity("Query", "submitted"))
.unwrap()
.try_with_entity(entity("Server", "booted"))
.unwrap()
.build();
.with_entity(entity("Query", "submitted"))
.with_entity(entity("Server", "booted"))
.build()
.unwrap();
let opts = Options {
event_derives: &["Debug"],
..Options::default()
Expand Down
6 changes: 3 additions & 3 deletions crates/instrumentation-build/src/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,11 @@ mod tests {
fn entity_ref_uses_its_ref_target_marker() {
use quent_schema::builder::AnnotationsBuilder;

let mut annotations = AnnotationsBuilder::new();
annotations.set_constraint(RefTargetConstraint::NAME, Some("Cluster".to_string()));
let annotations = AnnotationsBuilder::new()
.with_constraint(RefTargetConstraint::NAME, Some("Cluster".to_string()));
let ty = DataType::EntityRef {
data: Some(Box::new(DataType::U64)),
annotations: annotations.build(),
annotations: annotations.build().unwrap(),
};
let tokens = map_data_type(&ty, 0).to_string();
assert!(tokens.contains("EntityRef < Cluster , u64 >"), "{tokens}");
Expand Down
21 changes: 8 additions & 13 deletions crates/instrumentation-build/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,27 +135,22 @@ mod tests {

#[test]
fn docs_annotations_become_doc_attributes() {
let docs = |text: &str| {
let mut builder = AnnotationsBuilder::new();
builder.set_docs(text);
builder.build()
};
let docs = |text: &str| AnnotationsBuilder::new().with_docs(text).build().unwrap();
let field_x = Field::new(ident("x"), DataType::U8, docs("field doc"));
let ev = EventBuilder::new(ident("ev"), Cardinality::Once)
.try_with_field(field_x)
.unwrap()
.with_field(field_x)
.with_annotations(docs("event doc"))
.build();
.build()
.unwrap();
let en = EntityBuilder::new(ident("E"))
.try_with_event(ev)
.unwrap()
.with_event(ev)
.with_annotations(docs("entity doc"))
.build()
.unwrap();
let s = SchemaBuilder::new(ident("M"))
.try_with_entity(en)
.unwrap()
.build();
.with_entity(en)
.build()
.unwrap();

let expected = quote! {
#[doc = "entity doc"]
Expand Down
15 changes: 7 additions & 8 deletions crates/instrumentation-build/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,18 @@ mod tests {
#[test]
fn generate_assembles_event_impl_observer_handle_and_context() {
let connection = EntityBuilder::new(ident("Connection"))
.try_with_event(
.with_event(
EventBuilder::new(ident("data"), Cardinality::Multi)
.try_with_field(field("bytes", DataType::U64))
.unwrap()
.build(),
.with_field(field("bytes", DataType::U64))
.build()
.unwrap(),
)
.unwrap()
.build()
.unwrap();
let s = SchemaBuilder::new(ident("Demo"))
.try_with_entity(connection)
.unwrap()
.build();
.with_entity(connection)
.build()
.unwrap();
let src = pretty(generate_runtime_types(&s).unwrap());
assert!(src.contains("impl ::quent_instrumentation::EntityEvent for ConnectionEvent"));
assert!(src.contains(r#"const NAME: &'static str = "connection""#));
Expand Down
6 changes: 3 additions & 3 deletions crates/ref-target/tests/ref-target-constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ fn entity_ref(target: Option<&str>) -> DataType {
DataType::EntityRef {
data: None,
annotations: AnnotationsBuilder::new()
.try_with_constraint(RefTargetConstraint::NAME, target.map(ToString::to_string))
.unwrap()
.build(),
.with_constraint(RefTargetConstraint::NAME, target.map(ToString::to_string))
.build()
.unwrap(),
}
}

Expand Down
15 changes: 7 additions & 8 deletions crates/ref-tree/tests/ref-tree-constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ fn tree_ref() -> DataType {
DataType::EntityRef {
data: None,
annotations: AnnotationsBuilder::new()
.try_with_constraint(RefTreeConstraint::NAME, None)
.unwrap()
.build(),
.with_constraint(RefTreeConstraint::NAME, None)
.build()
.unwrap(),
}
}

Expand All @@ -26,11 +26,10 @@ fn tree_ref_to(target: &str) -> DataType {
DataType::EntityRef {
data: None,
annotations: AnnotationsBuilder::new()
.try_with_constraint(RefTreeConstraint::NAME, None)
.unwrap()
.try_with_constraint(RefTargetConstraint::NAME, Some(ident(target).to_string()))
.unwrap()
.build(),
.with_constraint(RefTreeConstraint::NAME, None)
.with_constraint(RefTargetConstraint::NAME, Some(ident(target).to_string()))
.build()
.unwrap(),
}
}

Expand Down
Loading