mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-11 06:08:03 +00:00

Some checks are pending
CI / Determine changes (push) Waiting to run
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / Fuzz for new ty panics (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
[ty Playground] Release / publish (push) Waiting to run
62 lines
1.8 KiB
Rust
62 lines
1.8 KiB
Rust
use proc_macro2::TokenStream;
|
|
use quote::quote;
|
|
use syn::{Attribute, DeriveInput, Error, Lit, LitStr, Meta};
|
|
|
|
pub(crate) fn derive_impl(input: DeriveInput) -> syn::Result<TokenStream> {
|
|
let docs = get_docs(&input.attrs)?;
|
|
|
|
let name = input.ident;
|
|
|
|
let (impl_generics, ty_generics, where_clause) = &input.generics.split_for_impl();
|
|
|
|
Ok(quote! {
|
|
#[automatically_derived]
|
|
impl #impl_generics ruff_db::RustDoc for #name #ty_generics #where_clause {
|
|
fn rust_doc() -> &'static str {
|
|
#docs
|
|
}
|
|
}
|
|
})
|
|
}
|
|
/// Collect all doc comment attributes into a string
|
|
fn get_docs(attrs: &[Attribute]) -> syn::Result<String> {
|
|
let mut explanation = String::new();
|
|
for attr in attrs {
|
|
if attr.path().is_ident("doc") {
|
|
if let Some(lit) = parse_attr(["doc"], attr) {
|
|
let value = lit.value();
|
|
// `/// ` adds
|
|
let line = value.strip_prefix(' ').unwrap_or(&value);
|
|
explanation.push_str(line);
|
|
explanation.push('\n');
|
|
} else {
|
|
return Err(Error::new_spanned(attr, "unimplemented doc comment style"));
|
|
}
|
|
}
|
|
}
|
|
Ok(explanation)
|
|
}
|
|
|
|
fn parse_attr<'a, const LEN: usize>(
|
|
path: [&'static str; LEN],
|
|
attr: &'a Attribute,
|
|
) -> Option<&'a LitStr> {
|
|
if let Meta::NameValue(name_value) = &attr.meta {
|
|
let path_idents = name_value
|
|
.path
|
|
.segments
|
|
.iter()
|
|
.map(|segment| &segment.ident);
|
|
|
|
if path_idents.eq(path) {
|
|
if let syn::Expr::Lit(syn::ExprLit {
|
|
lit: Lit::Str(lit), ..
|
|
}) = &name_value.value
|
|
{
|
|
return Some(lit);
|
|
}
|
|
}
|
|
}
|
|
|
|
None
|
|
}
|