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

Resource refactor #188

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
186 changes: 108 additions & 78 deletions mantle/Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion mantle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ members = [
"rbx_auth",
"rbx_cookie",
"logger",
"integration_executor"
"integration_executor",
"derive_resource"
]
11 changes: 11 additions & 0 deletions mantle/derive_resource/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "derive_resource"
version = "0.1.0"
edition = "2021"

[lib]
proc-macro = true

[dependencies]
quote = "1.0.23"
syn = "1.0.107"
186 changes: 186 additions & 0 deletions mantle/derive_resource/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, Data, DeriveInput};

#[proc_macro_derive(ResourceGroup)]
pub fn derive_resource_group(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);

let name = &input.ident;

let variants: Vec<_> = match &input.data {
Data::Enum(data) => data.variants.iter().collect(),
_ => panic!("expected enum to derive ResourceGroup"),
};

let variant_idents = variants.iter().map(|variant| variant.ident.clone());
let variant_idents2 = variant_idents.clone();
let variant_idents3 = variant_idents.clone();
let variant_idents4 = variant_idents.clone();
let variant_idents5 = variant_idents.clone();
let variant_idents6 = variant_idents.clone();
let variant_idents7 = variant_idents.clone();

let expanded = quote! {
#[async_trait]
impl ResourceGroup for #name {
fn id(&self) -> &str {
match self {
#(Self::#variant_idents(resource) => &resource.id),*
}
}

fn has_outputs(&self) -> bool {
match self {
#(Self::#variant_idents2(resource) => resource.outputs.is_some()),*
}
}

fn dependency_ids(&self) -> Vec<&str> {
match self {
#(Self::#variant_idents3(resource) => resource.dependency_ids()),*
}
}

fn next(
&self,
previous_graph: &ResourceGraph,
next_graph: &ResourceGraph,
) -> anyhow::Result<RbxResource> {
match self {
#(Self::#variant_idents4(resource) => Ok(Self::#variant_idents4(#variant_idents4::next(
resource,
previous_graph.get(&resource.id),
next_graph.get_many(resource.dependency_ids()),
)?))),*
}
}

async fn create(&mut self) -> anyhow::Result<()> {
match self {
#(Self::#variant_idents5(resource) => resource.create().await),*
}
}

async fn update(&mut self) -> anyhow::Result<()> {
match self {
#(Self::#variant_idents6(resource) => resource.update().await),*
}
}

async fn delete(&mut self) -> anyhow::Result<()> {
match self {
#(Self::#variant_idents7(resource) => resource.delete().await),*
}
}
}
};

TokenStream::from(expanded)
}

#[proc_macro_derive(Resource, attributes(dependency, resource_group))]
pub fn derive_resource(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);

let name = &input.ident;

let data = match &input.data {
Data::Struct(data) => data,
_ => panic!("expected struct to derive Resource"),
};

let dependency_fields: Vec<_> = data
.fields
.iter()
.filter_map(|field| {
if field.attrs.iter().any(|a| a.path.is_ident("dependency")) {
let var_name = field.ident.clone().unwrap();
let field_type = if let syn::Type::Path(path) = &field.ty {
path.path.get_ident().unwrap().clone()
} else {
panic!("expected dependency type to be a type path");
};
Some((var_name, field_type))
} else {
None
}
})
.collect();
let dependency_field_idents = dependency_fields
.iter()
.map(|(var_name, _field_type)| var_name);

let dependency_variables = dependency_fields.iter().map(|(var_name, field_type)| {
quote! {
let mut #var_name: Option<#field_type> = None;
}
});

let dependency_matchers = dependency_fields.iter().map(|(var_name, field_type)| {
quote! {
RbxResource::#field_type(resource) => {
#var_name = Some(resource.clone());
}
}
});

let dependency_values = dependency_fields.iter().map(|(var_name, field_type)| {
let field_type_str = field_type.to_string();
quote! {
#var_name: #var_name.ok_or(anyhow::Error::msg(format!(
"Expected dependency of type {} to be present",
#field_type_str
)))?
}
});

let expanded = quote! {
impl Resource for #name {
// TODO: RbxResource should come from a variable/attribute
fn next(
resource: &Self,
previous_resource: Option<&RbxResource>,
dependencies: Vec<&RbxResource>
) -> anyhow::Result<Self> {
#(#dependency_variables)*

for dependency in dependencies {
match dependency {
#(#dependency_matchers)*
_ => {}
}
}

let outputs = match previous_resource {
Some(RbxResource::#name(resource)) => {
resource.outputs.clone()
}
Some(_) => {
return anyhow::Result::Err(anyhow::Error::msg(format!(
"Expected previous resource with ID {} to be of the same type",
resource.id
)))
}
None => None
};

Ok(Self {
id: resource.id.clone(),
inputs: resource.inputs.clone(),
outputs,
#(#dependency_values),*
})
}

fn dependency_ids(&self) -> Vec<&str> {
vec![
#(&self.#dependency_field_idents.id),*
]
}
}
};

TokenStream::from(expanded)
}
6 changes: 6 additions & 0 deletions mantle/rbx_mantle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ include = [
rbx_auth = { path = "../rbx_auth" }
rbx_api = { path = "../rbx_api" }
logger = { path = "../logger" }
derive_resource = { path = "../derive_resource" }

serde_yaml = { version = "0.8" }
serde = { version = "1.0", features = ["derive"] }
Expand All @@ -32,3 +33,8 @@ yansi = "0.5.0"
url = { version = "2.2.2", features = ["serde"] }
log = "0.4.14"
schemars = { version = "=0.8.8-blake.2", git = "https://github.com/blake-mealey/schemars", branch = "raw-comments", features = ["derive", "url", "preserve_order"] }
anyhow = "1.0.68"
enum_dispatch = "0.3.11"

[dev-dependencies]
pretty_assertions = "1.3.0"
2 changes: 2 additions & 0 deletions mantle/rbx_mantle/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod config;
pub mod project;
pub mod resource_graph;
pub mod resource_graph_v2;
pub mod resources_v2;
pub mod roblox_resource_manager;
pub mod state;
Loading