From 35aaa64d1bc45cb94d6ba94457678d9440f67094 Mon Sep 17 00:00:00 2001 From: Predrag Gruevski Date: Sat, 29 Jul 2023 03:49:37 +0000 Subject: [PATCH] Refactor and document `Schema` singleton's `vertex_type` edge. --- trustfall_core/src/schema/adapter/mod.rs | 28 ++++++++++++++---------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/trustfall_core/src/schema/adapter/mod.rs b/trustfall_core/src/schema/adapter/mod.rs index f276bcc7..968815bc 100644 --- a/trustfall_core/src/schema/adapter/mod.rs +++ b/trustfall_core/src/schema/adapter/mod.rs @@ -400,17 +400,23 @@ impl<'a> crate::interpreter::Adapter<'a> for SchemaAdapter<'a> { }, "Schema" => match edge_name.as_ref() { "vertex_type" => { - let candidate_destination = resolve_info.destination(); - - let candidate_value_for_name = - candidate_destination.statically_required_property("name"); - - let mut iter = Some(vertex_type_iter( - self.schema, - candidate_value_for_name.map(|x| x.cloned()), - )); - - resolve_neighbors_with(contexts, move |_| iter.take().unwrap()) + let schema = self.schema; + let destination = resolve_info.destination(); + + // The `Schema` vertex is a singleton -- there can only be one instance of it. + // So this hint can only be used once, and we don't want to clone it needlessly. + // Take it from this option and assert that it hasn't been taken more than once. + let mut vertex_type_name = + Some(destination.statically_required_property("name").map(|x| x.cloned())); + + resolve_neighbors_with(contexts, move |_| { + vertex_type_iter( + schema, + vertex_type_name.take().expect( + "found more than one Schema vertex when resolving vertex_type", + ), + ) + }) } "entrypoint" => { let mut iter = Some(self.entrypoints_iter());