File size: 2,803 Bytes
e5034c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
use proc_macro::TokenStream;
use quote::{ToTokens, quote};
use syn::{DeriveInput, Expr, ExprLit, Lit, parse_macro_input};

/// Custom attribute for specifying tool description file path
extern crate proc_macro;

#[proc_macro_attribute]
pub fn tool_description_file(_attr: TokenStream, _item: TokenStream) -> TokenStream {
    // This is just a marker attribute, the actual processing happens in
    // ToolDescription
    _item
}

#[proc_macro_derive(ToolDescription, attributes(tool_description_file))]
pub fn derive_description(input: TokenStream) -> TokenStream {
    // Parse the input struct or enum
    let input = parse_macro_input!(input as DeriveInput);
    let name = &input.ident;
    let generics = &input.generics;

    // Check for tool_description_file attribute first
    let mut description_file = None;
    for attr in &input.attrs {
        if attr.path().is_ident("tool_description_file")
            && let syn::Meta::NameValue(name_value) = &attr.meta
            && let Expr::Lit(ExprLit { lit: Lit::Str(lit_str), .. }) = &name_value.value
        {
            description_file = Some(lit_str.value());
        }
    }

    // If we have a description file, read it at compile time
    let doc_string = if let Some(file_path) = description_file {
        std::fs::read_to_string(&file_path)
            .unwrap_or_else(|e| {
                panic!(
                    "Failed to read tool description file '{}': {}",
                    file_path, e
                )
            })
            .trim()
            .to_string()
    } else {
        // Collect doc lines from doc comments
        let mut doc_lines = Vec::new();
        for attr in &input.attrs {
            if attr.path().is_ident("doc") {
                // Get the doc content as a string
                let doc_string = attr.meta.to_token_stream().to_string();
                // Remove the quotes and = sign
                let clean = doc_string.trim_start_matches("=").trim_matches('"').trim();
                if !clean.is_empty() {
                    doc_lines.push(clean.to_string());
                }
            }
        }

        if doc_lines.is_empty() {
            panic!("No doc comment found for {name}");
        }
        doc_lines.join("\n")
    };

    // Generate the implementation
    let expanded = if generics.params.is_empty() {
        quote! {
            impl ToolDescription for #name {
                fn description(&self) -> String {
                    #doc_string.into()
                }
            }
        }
    } else {
        quote! {
            impl #generics ToolDescription for #name #generics {
                fn description(&self) -> String {
                    #doc_string.into()
                }
            }
        }
    };

    expanded.into()
}