Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Escape stubgen names #406

Merged
merged 18 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
24 changes: 5 additions & 19 deletions trustfall_stubgen/src/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ impl RustFile {
static PATTERN: OnceLock<Regex> = OnceLock::new();
let pattern =
PATTERN.get_or_init(|| Regex::new("([^{])\n (pub|fn|use)").expect("invalid regex"));

let pretty_item =
prettyplease::unparse(&syn::parse_str(&item.to_string()).expect("not valid Rust"));
let postprocessed = pattern.replace_all(&pretty_item, "$1\n\n $2");
Expand Down Expand Up @@ -386,7 +385,7 @@ fn make_vertex_file(
.collect();
rows.sort_unstable();
for row in rows {
let name = &row.name;
let name = &escaped_rust_name(row.name);
let ident = syn::Ident::new(name.as_str(), proc_macro2::Span::call_site());
variants.extend(quote! {
#ident(()),
Expand Down Expand Up @@ -476,25 +475,12 @@ fn ensure_no_edge_name_keyword_conflicts(
for row in &rows {
let mut uniq: HashMap<String, String> = HashMap::new();

for edge_name in &row.edge_names {
let edge_name_for_map = edge_name.clone();
let converted = escaped_rust_name(to_lower_snake_case(&edge_name_for_map));
let v = uniq.insert(converted, edge_name_for_map);
if let Some(v) = v {
panic!(
"cannot generate adapter for a schema containing both '{}' and '{}' as field names on vertex '{}', consider renaming one of them",
v, &edge_name, &row.name
);
}
}
for property_name in &row.property_names {
let property_name_for_map = property_name.clone();
let converted = escaped_rust_name(to_lower_snake_case(&property_name_for_map));
let v = uniq.insert(converted, property_name_for_map);
if let Some(v) = v {
for field_name in row.edge_names.iter().chain(row.property_names.iter()) {
let converted = escaped_rust_name(to_lower_snake_case(field_name));
if let Some(v) = uniq.insert(converted, field_name.clone()) {
panic!(
"cannot generate adapter for a schema containing both '{}' and '{}' as field names on vertex '{}', consider renaming one of them",
v, &property_name, &row.name
v, &field_name, &row.name
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ impl<'a> trustfall::provider::Adapter<'a> for Adapter {
resolve_info: &ResolveInfo,
) -> VertexIterator<'a, Self::Vertex> {
match edge_name.as_ref() {
"type" => super::entrypoints::type_(resolve_info),
"type2" => super::entrypoints::type2(resolve_info),
"unsafe2" => super::entrypoints::unsafe2(resolve_info),
"use" => super::entrypoints::use_(resolve_info),
"use2" => super::entrypoints::use2(resolve_info),
_ => {
unreachable!(
"attempted to resolve starting vertices for unexpected edge name: {edge_name}"
Expand All @@ -53,8 +54,15 @@ impl<'a> trustfall::provider::Adapter<'a> for Adapter {
resolve_info: &ResolveInfo,
) -> ContextOutcomeIterator<'a, Self::Vertex, FieldValue> {
match type_name.as_ref() {
"Type2" => {
super::properties::resolve_type2_property(
"unsafe2" => {
super::properties::resolve_unsafe2_property(
contexts,
property_name.as_ref(),
resolve_info,
)
}
"use2" => {
super::properties::resolve_use2_property(
contexts,
property_name.as_ref(),
resolve_info,
Expand All @@ -77,8 +85,8 @@ impl<'a> trustfall::provider::Adapter<'a> for Adapter {
resolve_info: &ResolveEdgeInfo,
) -> ContextOutcomeIterator<'a, Self::Vertex, VertexIterator<'a, Self::Vertex>> {
match type_name.as_ref() {
"Type" => {
super::edges::resolve_type_edge(
"use" => {
super::edges::resolve_use_edge(
contexts,
edge_name.as_ref(),
parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,41 @@ use trustfall::provider::{ContextIterator, ContextOutcomeIterator, EdgeParameter

use super::vertex::Vertex;

pub(super) fn resolve_type_edge<'a>(
pub(super) fn resolve_use_edge<'a>(
contexts: ContextIterator<'a, Vertex>,
edge_name: &str,
parameters: &EdgeParameters,
resolve_info: &ResolveEdgeInfo,
) -> ContextOutcomeIterator<'a, Vertex, VertexIterator<'a, Vertex>> {
match edge_name {
"type" => type_::type_(contexts, resolve_info),
"use" => use_::use_(contexts, resolve_info),
_ => {
unreachable!(
"attempted to resolve unexpected edge '{edge_name}' on type 'Type'"
"attempted to resolve unexpected edge '{edge_name}' on type 'use'"
)
}
}
}

mod type_ {
mod use_ {
use trustfall::provider::{
resolve_neighbors_with, ContextIterator, ContextOutcomeIterator, ResolveEdgeInfo,
VertexIterator,
};

use super::super::vertex::Vertex;

pub(super) fn type_<'a>(
pub(super) fn use_<'a>(
contexts: ContextIterator<'a, Vertex>,
_resolve_info: &ResolveEdgeInfo,
) -> ContextOutcomeIterator<'a, Vertex, VertexIterator<'a, Vertex>> {
resolve_neighbors_with(
contexts,
|vertex| {
let vertex = vertex
.as_type()
.expect("conversion failed, vertex was not a Type");
todo!("get neighbors along edge 'type' for type 'Type'")
.as_use()
.expect("conversion failed, vertex was not a use");
todo!("get neighbors along edge 'use' for type 'use'")
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ use trustfall::provider::{ResolveInfo, VertexIterator};

use super::vertex::Vertex;

pub(super) fn type_<'a>(_resolve_info: &ResolveInfo) -> VertexIterator<'a, Vertex> {
todo!("implement resolving starting vertices for entrypoint edge 'type'")
pub(super) fn unsafe2<'a>(_resolve_info: &ResolveInfo) -> VertexIterator<'a, Vertex> {
todo!("implement resolving starting vertices for entrypoint edge 'unsafe2'")
}

pub(super) fn type2<'a>(_resolve_info: &ResolveInfo) -> VertexIterator<'a, Vertex> {
todo!("implement resolving starting vertices for entrypoint edge 'type2'")
pub(super) fn use_<'a>(_resolve_info: &ResolveInfo) -> VertexIterator<'a, Vertex> {
todo!("implement resolving starting vertices for entrypoint edge 'use'")
}

pub(super) fn use2<'a>(_resolve_info: &ResolveInfo) -> VertexIterator<'a, Vertex> {
todo!("implement resolving starting vertices for entrypoint edge 'use2'")
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,33 @@ use trustfall::{FieldValue, provider::{ContextIterator, ContextOutcomeIterator,

use super::vertex::Vertex;

pub(super) fn resolve_type2_property<'a>(
pub(super) fn resolve_unsafe2_property<'a>(
contexts: ContextIterator<'a, Vertex>,
property_name: &str,
_resolve_info: &ResolveInfo,
) -> ContextOutcomeIterator<'a, Vertex, FieldValue> {
match property_name {
"type" => todo!("implement property 'type' in fn `resolve_type2_property()`"),
"unsafe" => {
todo!("implement property 'unsafe' in fn `resolve_unsafe2_property()`")
}
_ => {
unreachable!(
"attempted to read unexpected property '{property_name}' on type 'unsafe2'"
)
}
}
}

pub(super) fn resolve_use2_property<'a>(
contexts: ContextIterator<'a, Vertex>,
property_name: &str,
_resolve_info: &ResolveInfo,
) -> ContextOutcomeIterator<'a, Vertex, FieldValue> {
match property_name {
"use" => todo!("implement property 'use' in fn `resolve_use2_property()`"),
_ => {
unreachable!(
"attempted to read unexpected property '{property_name}' on type 'Type2'"
"attempted to read unexpected property '{property_name}' on type 'use2'"
)
}
}
Expand Down
Loading