1use proc_macro2::TokenStream;
2use quote::quote;
3use unsynn::*;
4
5use crate::parse::{DocStubArg, DocStubInner};
6use crate::token_processors::TokenProcessor;
7
8pub fn inject_all_docs_impl(args: TokenStream, input: TokenStream) -> core::result::Result<TokenStream, TokenStream> {
9 let base_path = match parse_path_from_args(args) {
11 Ok(path) => path,
12 Err(e) => {
13 let error_msg = e.to_string();
14 return Ok(quote! {
16 compile_error!(#error_msg);
17 #input
18 });
19 }
20 };
21
22 Ok(TokenProcessor::new(input, base_path).process())
24}
25
26fn parse_path_from_args(args: TokenStream) -> core::result::Result<String, String> {
27 if args.is_empty() {
29 let call_site = proc_macro2::Span::call_site();
30 if let Some(source_path) = call_site.local_file() {
31 let source_file = source_path.to_string_lossy().to_string();
32 return crate::config::get_docs_path(&source_file)
33 .map_err(|e| format!("Failed to get docs path from config: {}", e));
34 } else {
35 return Err("omnidoc requires a path argument".to_string());
36 }
37 }
38
39 let mut args_iter = args.into_token_iter();
40 match args_iter.parse::<DocStubInner>() {
41 Ok(parsed) => {
42 if let Some(arg_list) = parsed.args {
43 for arg in arg_list.0 {
44 if let DocStubArg::Path(path_arg) = arg.value {
45 return Ok(path_arg.value.as_str().to_string());
46 }
47 }
48 }
49 let call_site = proc_macro2::Span::call_site();
51 if let Some(source_path) = call_site.local_file() {
52 let source_file = source_path.to_string_lossy().to_string();
53 crate::config::get_docs_path(&source_file)
54 .map_err(|e| format!("Failed to get docs path from config: {}", e))
55 } else {
56 Err("path argument not found".to_string())
57 }
58 }
59 Err(_e) => Err("Failed to parse arguments".to_string()),
60 }
61}