|
| 1 | +use anyhow::Context as _; |
| 2 | +use itertools::Itertools as _; |
| 3 | +use serde::Serialize; |
| 4 | +use syn::{Visibility, spanned::Spanned as _}; |
| 5 | +use tinytemplate::TinyTemplate; |
| 6 | + |
| 7 | +use super::ToHtml; |
| 8 | + |
| 9 | +const FUNCTION_TEMPLATE: &str = r#" |
| 10 | + <div class="function"> |
| 11 | + <div class="function-signature"> |
| 12 | + {{ if visibility }}<span class="function-visibility">{visibility}</span> {{ endif }} |
| 13 | + {{ if modifiers }}<span class="function-modifiers">{modifiers}</span> {{ endif }} |
| 14 | + <span class="function-name">{name}</span> |
| 15 | + <span class="function-params">({params})</span> |
| 16 | + {{ if return_type }}<span class="function-return"> -> {return_type}</span>{{ endif }} |
| 17 | + </div> |
| 18 | + </div> |
| 19 | +"#; |
| 20 | + |
| 21 | +#[derive(Serialize)] |
| 22 | +pub struct FunctionContext { |
| 23 | + pub name: String, |
| 24 | + pub params: String, |
| 25 | + pub return_type: Option<String>, |
| 26 | + pub visibility: Option<String>, |
| 27 | + pub modifiers: Option<String>, |
| 28 | +} |
| 29 | + |
| 30 | +impl FunctionContext { |
| 31 | + /// Create a `FunctionContext` from a `syn::Signature` and attributes |
| 32 | + pub fn new(sig: &syn::Signature, vis: &Visibility) -> Self { |
| 33 | + let visibility = match vis { |
| 34 | + Visibility::Public(_) => Some("pub".to_owned()), |
| 35 | + Visibility::Restricted(_) | Visibility::Inherited => None, |
| 36 | + }; |
| 37 | + |
| 38 | + let mut modifiers = Vec::new(); |
| 39 | + if sig.asyncness.is_some() { |
| 40 | + modifiers.push("async".to_owned()); |
| 41 | + } |
| 42 | + if sig.constness.is_some() { |
| 43 | + modifiers.push("const".to_owned()); |
| 44 | + } |
| 45 | + if sig.unsafety.is_some() { |
| 46 | + modifiers.push("unsafe".to_owned()); |
| 47 | + } |
| 48 | + |
| 49 | + let params = sig |
| 50 | + .inputs |
| 51 | + .iter() |
| 52 | + .map(|param| match param { |
| 53 | + syn::FnArg::Receiver(r) => { |
| 54 | + r.span().source_text().expect("Could not get source_text") |
| 55 | + } |
| 56 | + syn::FnArg::Typed(t) => t.span().source_text().expect("Could not get source_text"), |
| 57 | + }) |
| 58 | + .join(", "); |
| 59 | + |
| 60 | + let return_type = match &sig.output { |
| 61 | + syn::ReturnType::Default => None, |
| 62 | + syn::ReturnType::Type(_, ty) => { |
| 63 | + Some(ty.span().source_text().expect("Could not get source_text")) |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + Self { |
| 68 | + name: sig.ident.to_string(), |
| 69 | + params, |
| 70 | + return_type, |
| 71 | + visibility, |
| 72 | + modifiers: if modifiers.is_empty() { |
| 73 | + None |
| 74 | + } else { |
| 75 | + Some(modifiers.join(" ")) |
| 76 | + }, |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl ToHtml for FunctionContext { |
| 82 | + fn to_html(&self) -> anyhow::Result<String> { |
| 83 | + let mut tt = TinyTemplate::new(); |
| 84 | + tt.set_default_formatter(&tinytemplate::format_unescaped); |
| 85 | + |
| 86 | + tt.add_template("function", FUNCTION_TEMPLATE) |
| 87 | + .context("Failed to add template")?; |
| 88 | + |
| 89 | + tt.render("function", self) |
| 90 | + .context("Failed to render template") |
| 91 | + } |
| 92 | +} |
0 commit comments