Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e4f3097

Browse filesBrowse files
authored
Working Rust sdk (#721)
1 parent fa0c458 commit e4f3097
Copy full SHA for e4f3097

21 files changed

+4216
-0
lines changed

‎pgml-sdks/rust/pgml-macros/Cargo.lock

Copy file name to clipboardExpand all lines: pgml-sdks/rust/pgml-macros/Cargo.lock
+54Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎pgml-sdks/rust/pgml-macros/Cargo.toml

Copy file name to clipboard
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "pgml-macros"
3+
version = "0.1.0"
4+
edition = "2021"
5+
6+
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
7+
8+
[lib]
9+
proc-macro = true
10+
11+
[dependencies]
12+
syn = {version = "2.0.17", features=["full", "extra-traits", "fold", "visit"]}
13+
quote = "1.0.9"
14+
proc-macro2 = "1.0"
15+
anyhow = "1.0.9"
+95Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
use proc_macro2::Ident;
2+
use quote::{format_ident, ToTokens};
3+
use syn::{
4+
parse::Parser,
5+
punctuated::Punctuated,
6+
visit::{self, Visit},
7+
ImplItemFn, ReturnType, Token, Visibility,
8+
};
9+
10+
use crate::types::{GetOutputType, GetSupportedType, OutputType, SupportedType};
11+
12+
pub struct AttributeArgs {
13+
pub args: Vec<String>,
14+
}
15+
16+
impl AttributeArgs {
17+
pub fn new(attributes: proc_macro::TokenStream) -> Self {
18+
let attribute_parser = Punctuated::<Ident, Token![,]>::parse_terminated;
19+
let parsed_attributes = attribute_parser
20+
.parse(attributes)
21+
.expect("Error parsing attributes for custom_methods macro");
22+
let args: Vec<String> = parsed_attributes
23+
.into_pairs()
24+
.map(|p| p.value().to_string())
25+
.collect();
26+
Self { args }
27+
}
28+
}
29+
30+
#[derive(Debug)]
31+
pub struct GetImplMethod {
32+
pub exists: bool,
33+
pub method_ident: Ident,
34+
pub is_async: bool,
35+
pub method_arguments: Vec<(String, SupportedType)>,
36+
pub receiver: Option<proc_macro2::TokenStream>,
37+
pub output_type: OutputType,
38+
}
39+
40+
impl Default for GetImplMethod {
41+
fn default() -> Self {
42+
GetImplMethod {
43+
exists: false,
44+
method_ident: format_ident!("nothing"),
45+
is_async: false,
46+
method_arguments: Vec::new(),
47+
receiver: None,
48+
output_type: OutputType::Default,
49+
}
50+
}
51+
}
52+
53+
impl<'ast> Visit<'ast> for GetImplMethod {
54+
fn visit_impl_item_fn(&mut self, i: &'ast ImplItemFn) {
55+
if let Visibility::Public(_p) = i.vis {
56+
self.exists = true;
57+
visit::visit_impl_item_fn(self, i);
58+
}
59+
}
60+
61+
fn visit_signature(&mut self, i: &'ast syn::Signature) {
62+
self.method_ident = i.ident.clone();
63+
self.is_async = i.asyncness.is_some();
64+
self.output_type = match &i.output {
65+
ReturnType::Default => OutputType::Default,
66+
ReturnType::Type(_ra, ty) => {
67+
let mut get_output_type = GetOutputType::default();
68+
get_output_type.visit_type(ty);
69+
get_output_type.output
70+
}
71+
};
72+
visit::visit_signature(self, i);
73+
}
74+
75+
fn visit_receiver(&mut self, i: &'ast syn::Receiver) {
76+
self.receiver = Some(i.to_token_stream());
77+
visit::visit_receiver(self, i);
78+
}
79+
80+
fn visit_pat_type(&mut self, i: &'ast syn::PatType) {
81+
let pat = i.pat.to_token_stream().to_string();
82+
let mut ty = GetSupportedType::default();
83+
ty.visit_type(&i.ty);
84+
let ty = ty.ty.expect("No type found");
85+
self.method_arguments.push((pat, ty));
86+
visit::visit_pat_type(self, i);
87+
}
88+
89+
fn visit_expr_return(&mut self, i: &'ast syn::ExprReturn) {
90+
visit::visit_expr_return(self, i);
91+
}
92+
93+
// We don't want to visit any of the statments in the methods
94+
fn visit_block(&mut self, _i: &'ast syn::Block) {}
95+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.