-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
599655a
commit 5cbd66f
Showing
9 changed files
with
363 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
use syn::Type; | ||
|
||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] | ||
pub(crate) enum ReturnType { | ||
Value, | ||
Result, | ||
} | ||
|
||
pub(crate) fn get_return_type(ty: &Type) -> ReturnType { | ||
if is_result_type(ty) { | ||
ReturnType::Result | ||
} else { | ||
ReturnType::Value | ||
} | ||
} | ||
|
||
fn is_result_type(ty: &Type) -> bool { | ||
match ty { | ||
| Type::Path(type_path) => { | ||
let path_segments = &type_path.path.segments; | ||
path_segments.last().map_or(false, |last_segment| { | ||
if last_segment.ident == "Result" { | ||
match &last_segment.arguments { | ||
syn::PathArguments::AngleBracketed(args) => args.args.len() == 2, | ||
_ => false, | ||
} | ||
} else if path_segments.len() >= 2 { | ||
path_segments | ||
.iter() | ||
.rev() | ||
.nth(1) | ||
.map_or(false, |second_last_segment| { | ||
second_last_segment.ident == "result" | ||
&& match &last_segment.arguments { | ||
syn::PathArguments::AngleBracketed(args) => args.args.len() == 2, | ||
_ => false, | ||
} | ||
}) | ||
} else { | ||
false | ||
} | ||
}) | ||
}, | ||
| _ => false, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
type TestResult = Result<i32, String>; | ||
|
||
#[test] | ||
fn test_get_return_type() { | ||
let ty = syn::parse_str::<Type>("Result<i32, String>").unwrap(); | ||
assert_eq!(get_return_type(&ty), ReturnType::Result); | ||
|
||
let ty = | ||
syn::parse_str::<Type>("std::result::Result<i32, String>").unwrap(); | ||
assert_eq!(get_return_type(&ty), ReturnType::Result); | ||
|
||
let ty = syn::parse_str::<Type>("i32").unwrap(); | ||
assert_eq!(get_return_type(&ty), ReturnType::Value); | ||
|
||
let ty: Type = syn::parse_str::<Type>("TestResult").unwrap(); | ||
assert_eq!(get_return_type(&ty), ReturnType::Value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,14 @@ | ||
use crate::tool::{impl_tool, impl_tool_with_result}; | ||
use crate::tool::impl_tool; | ||
use proc_macro::TokenStream; | ||
|
||
mod check_result; | ||
mod tool; | ||
|
||
#[proc_macro_attribute] | ||
pub fn clust_tool( | ||
attr: TokenStream, | ||
_attr: TokenStream, | ||
item: TokenStream, | ||
) -> TokenStream { | ||
let item_func = syn::parse::<syn::ItemFn>(item).unwrap(); | ||
impl_tool(&item_func) | ||
} | ||
|
||
#[proc_macro_attribute] | ||
pub fn clust_tool_result( | ||
attr: TokenStream, | ||
item: TokenStream, | ||
) -> TokenStream { | ||
let item_func = syn::parse::<syn::ItemFn>(item).unwrap(); | ||
impl_tool_with_result(&item_func) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use std::collections::BTreeMap; | ||
|
||
use clust::messages::{AsyncTool, FunctionCalls, FunctionResults, Invoke}; | ||
|
||
use clust_macros::clust_tool; | ||
|
||
/// An asynchronous function for testing. | ||
/// | ||
/// ## Arguments | ||
/// - `arg1` - First argument. | ||
#[clust_tool] | ||
async fn test_function(arg1: i32) -> i32 { | ||
arg1 + 1 | ||
} | ||
|
||
#[test] | ||
fn test_description() { | ||
let tool = ClustTool_test_function {}; | ||
|
||
assert_eq!( | ||
tool.description().to_string(), | ||
r#" | ||
<tool_description> | ||
<tool_name>test_function</tool_name> | ||
<description>An asynchronous function for testing.</description> | ||
<parameters> | ||
<parameter> | ||
<name>arg1</name> | ||
<type>i32</type> | ||
<description>First argument.</description> | ||
</parameter> | ||
</parameters> | ||
</tool_description>"# | ||
); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_call() { | ||
let tool = ClustTool_test_function {}; | ||
|
||
let function_calls = FunctionCalls { | ||
invoke: Invoke { | ||
tool_name: String::from("test_function"), | ||
parameters: BTreeMap::from_iter(vec![( | ||
"arg1".to_string(), | ||
"42".to_string(), | ||
)]), | ||
}, | ||
}; | ||
|
||
let result = tool | ||
.call(function_calls) | ||
.await | ||
.unwrap(); | ||
|
||
if let FunctionResults::Result(result) = result { | ||
assert_eq!(result.tool_name, "test_function"); | ||
assert_eq!(result.stdout, "43"); | ||
} else { | ||
panic!("Expected FunctionResults::Result"); | ||
} | ||
} |
Oops, something went wrong.