golem-rust-macro 3.0.1

Golem Rust tooling library that facilitates writing Golem backends in Rust
Documentation
// Copyright 2024-2026 Golem Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Shared token emission helpers used to synthesize the runtime tool metadata
//! (`ExtendedToolType`, `ExtendedErrorCase`, ...) from the parsed IR.

use crate::tool::ir::{DocIr, ErrorKindIr};
use proc_macro2::TokenStream;
use quote::quote;

/// Emits a `golem_rust::agentic::Doc` value from a [`DocIr`].
pub fn doc_tokens(doc: &DocIr) -> TokenStream {
    doc_tokens_with_namespace(doc, quote! { golem_rust::agentic })
}

pub fn wire_doc_tokens(doc: &DocIr) -> TokenStream {
    doc_tokens_with_namespace(doc, quote! { golem_rust::schema::tool::wit::wire })
}

fn doc_tokens_with_namespace(doc: &DocIr, namespace: TokenStream) -> TokenStream {
    let summary = &doc.summary;
    let description = &doc.description;
    let examples = doc.examples.iter().map(|ex| {
        let title = &ex.title;
        let body = &ex.body;
        quote! {
            #namespace::Example {
                title: #title.to_string(),
                body: #body.to_string(),
            }
        }
    });
    quote! {
        #namespace::Doc {
            summary: #summary.to_string(),
            description: #description.to_string(),
            examples: vec![ #(#examples),* ],
        }
    }
}

/// Emits a `wire::ErrorKind` value.
pub fn error_kind_tokens(kind: ErrorKindIr) -> TokenStream {
    match kind {
        ErrorKindIr::UsageError => quote! {
            golem_rust::agentic::ErrorKind::UsageError
        },
        ErrorKindIr::RuntimeError => quote! {
            golem_rust::agentic::ErrorKind::RuntimeError
        },
    }
}