Skip to main content

wit_bindgen_rust_macro/
lib.rs

1use proc_macro2::{Span, TokenStream};
2use quote::ToTokens;
3use std::collections::HashMap;
4use std::path::{Path, PathBuf};
5use std::sync::atomic::{AtomicUsize, Ordering::Relaxed};
6use syn::parse::{Error, Parse, ParseStream, Result};
7use syn::punctuated::Punctuated;
8use syn::{Token, braced, token};
9use wit_bindgen_core::AsyncFilterSet;
10use wit_bindgen_core::WorldGenerator;
11use wit_bindgen_core::wit_parser::{PackageId, Resolve, UnresolvedPackageGroup, WorldId};
12use wit_bindgen_rust::{Opts, Ownership, WithOption};
13
14#[proc_macro]
15pub fn generate(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
16    syn::parse_macro_input!(input as Config)
17        .expand()
18        .unwrap_or_else(Error::into_compile_error)
19        .into()
20}
21
22fn anyhow_to_syn(span: Span, err: anyhow::Error) -> Error {
23    let err = attach_with_context(err);
24    let mut msg = err.to_string();
25    for cause in err.chain().skip(1) {
26        msg.push_str(&format!("\n\nCaused by:\n  {cause}"));
27    }
28    Error::new(span, msg)
29}
30
31fn attach_with_context(err: anyhow::Error) -> anyhow::Error {
32    if let Some(e) = err.downcast_ref::<wit_bindgen_rust::MissingWith>() {
33        let option = e.0.clone();
34        return err.context(format!(
35            "missing one of:\n\
36            * `generate_all` option\n\
37            * `with: {{ \"{option}\": path::to::bindings, }}`\n\
38            * `with: {{ \"{option}\": generate, }}`\
39            "
40        ));
41    }
42    err
43}
44
45struct Config {
46    opts: Opts,
47    resolve: Resolve,
48    world: WorldId,
49    files: Vec<PathBuf>,
50    debug: bool,
51}
52
53/// The source of the wit package definition
54enum Source {
55    /// A list of paths to wit directories
56    Paths(Vec<PathBuf>),
57    /// Inline sources have an optional path to a directory of their dependencies
58    Inline(String, Option<Vec<PathBuf>>),
59}
60
61impl Parse for Config {
62    fn parse(input: ParseStream<'_>) -> Result<Self> {
63        let call_site = Span::call_site();
64        let mut opts = Opts::default();
65        let mut world = None;
66        let mut source = None;
67        let mut features = Vec::new();
68        let mut async_configured = false;
69        let mut debug = false;
70
71        if input.peek(token::Brace) {
72            let content;
73            syn::braced!(content in input);
74            let fields = Punctuated::<Opt, Token![,]>::parse_terminated(&content)?;
75            for field in fields.into_pairs() {
76                match field.into_value() {
77                    Opt::Path(span, p) => {
78                        let paths = p
79                            .into_iter()
80                            .map(|f| f.evaluate_string())
81                            .collect::<Result<Vec<_>>>()?
82                            .into_iter()
83                            .map(PathBuf::from)
84                            .collect();
85
86                        source = Some(match source {
87                            Some(Source::Paths(_)) | Some(Source::Inline(_, Some(_))) => {
88                                return Err(Error::new(span, "cannot specify second source"));
89                            }
90                            Some(Source::Inline(i, None)) => Source::Inline(i, Some(paths)),
91                            None => Source::Paths(paths),
92                        })
93                    }
94                    Opt::World(s) => {
95                        if world.is_some() {
96                            return Err(Error::new(s.span(), "cannot specify second world"));
97                        }
98                        world = Some(s.value());
99                    }
100                    Opt::Inline(s) => {
101                        source = Some(match source {
102                            Some(Source::Inline(_, _)) => {
103                                return Err(Error::new(s.span(), "cannot specify second source"));
104                            }
105                            Some(Source::Paths(p)) => Source::Inline(s.value(), Some(p)),
106                            None => Source::Inline(s.value(), None),
107                        })
108                    }
109                    Opt::UseStdFeature => opts.std_feature = true,
110                    Opt::RawStrings => opts.raw_strings = true,
111                    Opt::Ownership(ownership) => opts.ownership = ownership,
112                    Opt::Skip(list) => opts.skip.extend(list.iter().map(|i| i.value())),
113                    Opt::RuntimePath(path) => opts.runtime_path = Some(path.value()),
114                    Opt::MapType(path) => opts.map_type = Some(path.value()),
115                    Opt::BitflagsPath(path) => opts.bitflags_path = Some(path.value()),
116                    Opt::Stubs => {
117                        opts.stubs = true;
118                    }
119                    Opt::ExportPrefix(prefix) => opts.export_prefix = Some(prefix.value()),
120                    Opt::AdditionalDerives(paths) => {
121                        opts.additional_derive_attributes = paths
122                            .into_iter()
123                            .map(|p| p.into_token_stream().to_string())
124                            .collect()
125                    }
126                    Opt::AdditionalDerivesIgnore(list) => {
127                        opts.additional_derive_ignore =
128                            list.into_iter().map(|i| i.value()).collect()
129                    }
130                    Opt::With(with) => opts.with.extend(with),
131                    Opt::GenerateAll => {
132                        opts.generate_all = true;
133                    }
134                    Opt::TypeSectionSuffix(suffix) => {
135                        opts.type_section_suffix = Some(suffix.value());
136                    }
137                    Opt::DisableRunCtorsOnceWorkaround(enable) => {
138                        opts.disable_run_ctors_once_workaround = enable.value();
139                    }
140                    Opt::DefaultBindingsModule(enable) => {
141                        opts.default_bindings_module = Some(enable.value());
142                    }
143                    Opt::ExportMacroName(name) => {
144                        opts.export_macro_name = Some(name.value());
145                    }
146                    Opt::PubExportMacro(enable) => {
147                        opts.pub_export_macro = enable.value();
148                    }
149                    Opt::GenerateUnusedTypes(enable) => {
150                        opts.generate_unused_types = enable.value();
151                    }
152                    Opt::Features(f) => {
153                        features.extend(f.into_iter().map(|f| f.value()));
154                    }
155                    Opt::DisableCustomSectionLinkHelpers(disable) => {
156                        opts.disable_custom_section_link_helpers = disable.value();
157                    }
158                    Opt::Debug(enable) => {
159                        debug = enable.value();
160                    }
161                    Opt::Async(val, span) => {
162                        if async_configured {
163                            return Err(Error::new(span, "cannot specify second async config"));
164                        }
165                        async_configured = true;
166                        opts.async_ = val;
167                    }
168                    Opt::EnableMethodChaining(enable) => {
169                        opts.enable_method_chaining = enable.value();
170                    }
171                }
172            }
173        } else {
174            world = input.parse::<Option<syn::LitStr>>()?.map(|s| s.value());
175            if input.parse::<Option<syn::token::In>>()?.is_some() {
176                source = Some(Source::Paths(vec![PathBuf::from(
177                    input.parse::<syn::LitStr>()?.value(),
178                )]));
179            }
180        }
181        let (resolve, main_packages, files) =
182            parse_source(&source, &features).map_err(|err| anyhow_to_syn(call_site, err))?;
183        let world = resolve
184            .select_world(&main_packages, world.as_deref())
185            .map_err(|e| anyhow_to_syn(call_site, e))?;
186        Ok(Config {
187            opts,
188            resolve,
189            world,
190            files,
191            debug,
192        })
193    }
194}
195
196/// Parse the source
197fn parse_source(
198    source: &Option<Source>,
199    features: &[String],
200) -> anyhow::Result<(Resolve, Vec<PackageId>, Vec<PathBuf>)> {
201    let mut resolve = Resolve::default();
202    resolve.features.extend(features.iter().cloned());
203    let mut files = Vec::new();
204    let mut pkgs = Vec::new();
205    let root = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
206    let mut parse = |paths: &[PathBuf]| -> anyhow::Result<()> {
207        for path in paths {
208            let p = root.join(path);
209            // Try to normalize the path to make the error message more understandable when
210            // the path is not correct. Fallback to the original path if normalization fails
211            // (probably return an error somewhere else).
212            let normalized_path = match std::fs::canonicalize(&p) {
213                Ok(p) => p,
214                Err(_) => p.to_path_buf(),
215            };
216            let (pkg, sources) = resolve.push_path(normalized_path)?;
217            pkgs.push(pkg);
218            files.extend(sources.paths().map(|p| p.to_owned()));
219        }
220        Ok(())
221    };
222    let default = root.join("wit");
223    match source {
224        Some(Source::Inline(s, path)) => {
225            match path {
226                Some(p) => parse(p)?,
227                // If no `path` is explicitly specified still parse the default
228                // `wit` directory if it exists. Don't require its existence,
229                // however, as `inline` can be used in lieu of a folder. Test
230                // whether it exists and only if there is it parsed.
231                None => {
232                    if default.exists() {
233                        parse(&[default])?;
234                    }
235                }
236            }
237            pkgs.truncate(0);
238            pkgs.push(resolve.push_group(UnresolvedPackageGroup::parse("macro-input", s)?)?);
239        }
240        Some(Source::Paths(p)) => parse(p)?,
241        None => parse(&[default])?,
242    };
243
244    Ok((resolve, pkgs, files))
245}
246
247impl Config {
248    fn expand(mut self) -> Result<TokenStream> {
249        let mut files = Default::default();
250        let mut generator = self.opts.build();
251        generator
252            .generate(&mut self.resolve, self.world, &mut files)
253            .map_err(|e| anyhow_to_syn(Span::call_site(), e))?;
254        let (_, src) = files.iter().next().unwrap();
255        let mut src = std::str::from_utf8(src).unwrap().to_string();
256
257        // If a magical `WIT_BINDGEN_DEBUG` environment variable is set then
258        // place a formatted version of the expanded code into a file. This file
259        // will then show up in rustc error messages for any codegen issues and can
260        // be inspected manually.
261        if std::env::var("WIT_BINDGEN_DEBUG").is_ok() || self.debug {
262            static INVOCATION: AtomicUsize = AtomicUsize::new(0);
263            let root = Path::new(env!("DEBUG_OUTPUT_DIR"));
264            let world_name = &self.resolve.worlds[self.world].name;
265            let n = INVOCATION.fetch_add(1, Relaxed);
266            let path = root.join(format!("{world_name}{n}.rs"));
267
268            // optimistically format the code but don't require success
269            let contents = match fmt(&src) {
270                Ok(formatted) => formatted,
271                Err(_) => src.clone(),
272            };
273            std::fs::write(&path, contents.as_bytes()).unwrap();
274
275            src = format!("include!({path:?});");
276        }
277        let mut contents = src.parse::<TokenStream>().unwrap();
278
279        // Include a dummy `include_bytes!` for any files we read so rustc knows that
280        // we depend on the contents of those files.
281        for file in self.files.iter() {
282            contents.extend(
283                format!(
284                    "const _: &[u8] = include_bytes!(r#\"{}\"#);\n",
285                    file.display()
286                )
287                .parse::<TokenStream>()
288                .unwrap(),
289            );
290        }
291
292        Ok(contents)
293    }
294}
295
296mod kw {
297    syn::custom_keyword!(std_feature);
298    syn::custom_keyword!(raw_strings);
299    syn::custom_keyword!(skip);
300    syn::custom_keyword!(world);
301    syn::custom_keyword!(path);
302    syn::custom_keyword!(inline);
303    syn::custom_keyword!(ownership);
304    syn::custom_keyword!(runtime_path);
305    syn::custom_keyword!(map_type);
306    syn::custom_keyword!(bitflags_path);
307    syn::custom_keyword!(exports);
308    syn::custom_keyword!(stubs);
309    syn::custom_keyword!(export_prefix);
310    syn::custom_keyword!(additional_derives);
311    syn::custom_keyword!(additional_derives_ignore);
312    syn::custom_keyword!(with);
313    syn::custom_keyword!(generate_all);
314    syn::custom_keyword!(type_section_suffix);
315    syn::custom_keyword!(disable_run_ctors_once_workaround);
316    syn::custom_keyword!(default_bindings_module);
317    syn::custom_keyword!(export_macro_name);
318    syn::custom_keyword!(pub_export_macro);
319    syn::custom_keyword!(generate_unused_types);
320    syn::custom_keyword!(features);
321    syn::custom_keyword!(disable_custom_section_link_helpers);
322    syn::custom_keyword!(imports);
323    syn::custom_keyword!(debug);
324    syn::custom_keyword!(enable_method_chaining);
325}
326
327#[derive(Clone)]
328enum ExportKey {
329    World,
330    Name(syn::LitStr),
331}
332
333impl Parse for ExportKey {
334    fn parse(input: ParseStream<'_>) -> Result<Self> {
335        let l = input.lookahead1();
336        Ok(if l.peek(kw::world) {
337            input.parse::<kw::world>()?;
338            Self::World
339        } else {
340            Self::Name(input.parse()?)
341        })
342    }
343}
344
345impl From<ExportKey> for wit_bindgen_rust::ExportKey {
346    fn from(key: ExportKey) -> Self {
347        match key {
348            ExportKey::World => Self::World,
349            ExportKey::Name(s) => Self::Name(s.value()),
350        }
351    }
352}
353
354#[cfg(feature = "macro-string")]
355type PathType = macro_string::MacroString;
356#[cfg(not(feature = "macro-string"))]
357type PathType = syn::LitStr;
358
359trait EvaluateString {
360    fn evaluate_string(&self) -> Result<String>;
361}
362
363#[cfg(feature = "macro-string")]
364impl EvaluateString for macro_string::MacroString {
365    fn evaluate_string(&self) -> Result<String> {
366        self.eval()
367    }
368}
369
370#[cfg(not(feature = "macro-string"))]
371impl EvaluateString for syn::LitStr {
372    fn evaluate_string(&self) -> Result<String> {
373        Ok(self.value())
374    }
375}
376
377enum Opt {
378    World(syn::LitStr),
379    Path(Span, Vec<PathType>),
380    Inline(syn::LitStr),
381    UseStdFeature,
382    RawStrings,
383    Skip(Vec<syn::LitStr>),
384    Ownership(Ownership),
385    RuntimePath(syn::LitStr),
386    MapType(syn::LitStr),
387    BitflagsPath(syn::LitStr),
388    Stubs,
389    ExportPrefix(syn::LitStr),
390    // Parse as paths so we can take the concrete types/macro names rather than raw strings
391    AdditionalDerives(Vec<syn::Path>),
392    AdditionalDerivesIgnore(Vec<syn::LitStr>),
393    With(HashMap<String, WithOption>),
394    GenerateAll,
395    TypeSectionSuffix(syn::LitStr),
396    DisableRunCtorsOnceWorkaround(syn::LitBool),
397    DefaultBindingsModule(syn::LitStr),
398    ExportMacroName(syn::LitStr),
399    PubExportMacro(syn::LitBool),
400    GenerateUnusedTypes(syn::LitBool),
401    Features(Vec<syn::LitStr>),
402    DisableCustomSectionLinkHelpers(syn::LitBool),
403    Async(AsyncFilterSet, Span),
404    Debug(syn::LitBool),
405    EnableMethodChaining(syn::LitBool),
406}
407
408impl Parse for Opt {
409    fn parse(input: ParseStream<'_>) -> Result<Self> {
410        let l = input.lookahead1();
411        if l.peek(kw::path) {
412            input.parse::<kw::path>()?;
413            input.parse::<Token![:]>()?;
414            // the `path` supports two forms:
415            // * path: "xxx"
416            // * path: ["aaa", "bbb"]
417            if input.peek(token::Bracket) {
418                let contents;
419                syn::bracketed!(contents in input);
420                let span = input.span();
421                let list = Punctuated::<PathType, Token![,]>::parse_terminated(&contents)?;
422                Ok(Opt::Path(span, list.into_iter().collect()))
423            } else {
424                let span = input.span();
425                let path: PathType = input.parse()?;
426                Ok(Opt::Path(span, vec![path]))
427            }
428        } else if l.peek(kw::inline) {
429            input.parse::<kw::inline>()?;
430            input.parse::<Token![:]>()?;
431            Ok(Opt::Inline(input.parse()?))
432        } else if l.peek(kw::world) {
433            input.parse::<kw::world>()?;
434            input.parse::<Token![:]>()?;
435            Ok(Opt::World(input.parse()?))
436        } else if l.peek(kw::std_feature) {
437            input.parse::<kw::std_feature>()?;
438            Ok(Opt::UseStdFeature)
439        } else if l.peek(kw::raw_strings) {
440            input.parse::<kw::raw_strings>()?;
441            Ok(Opt::RawStrings)
442        } else if l.peek(kw::ownership) {
443            input.parse::<kw::ownership>()?;
444            input.parse::<Token![:]>()?;
445            let ownership = input.parse::<syn::Ident>()?;
446            Ok(Opt::Ownership(match ownership.to_string().as_str() {
447                "Owning" => Ownership::Owning,
448                "Borrowing" => Ownership::Borrowing {
449                    duplicate_if_necessary: {
450                        let contents;
451                        braced!(contents in input);
452                        let field = contents.parse::<syn::Ident>()?;
453                        match field.to_string().as_str() {
454                            "duplicate_if_necessary" => {
455                                contents.parse::<Token![:]>()?;
456                                contents.parse::<syn::LitBool>()?.value
457                            }
458                            name => {
459                                return Err(Error::new(
460                                    field.span(),
461                                    format!(
462                                        "unrecognized `Ownership::Borrowing` field: `{name}`; \
463                                         expected `duplicate_if_necessary`"
464                                    ),
465                                ));
466                            }
467                        }
468                    },
469                },
470                name => {
471                    return Err(Error::new(
472                        ownership.span(),
473                        format!(
474                            "unrecognized ownership: `{name}`; \
475                             expected `Owning` or `Borrowing`"
476                        ),
477                    ));
478                }
479            }))
480        } else if l.peek(kw::skip) {
481            input.parse::<kw::skip>()?;
482            input.parse::<Token![:]>()?;
483            let contents;
484            syn::bracketed!(contents in input);
485            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
486            Ok(Opt::Skip(list.iter().cloned().collect()))
487        } else if l.peek(kw::runtime_path) {
488            input.parse::<kw::runtime_path>()?;
489            input.parse::<Token![:]>()?;
490            Ok(Opt::RuntimePath(input.parse()?))
491        } else if l.peek(kw::map_type) {
492            input.parse::<kw::map_type>()?;
493            input.parse::<Token![:]>()?;
494            Ok(Opt::MapType(input.parse()?))
495        } else if l.peek(kw::bitflags_path) {
496            input.parse::<kw::bitflags_path>()?;
497            input.parse::<Token![:]>()?;
498            Ok(Opt::BitflagsPath(input.parse()?))
499        } else if l.peek(kw::stubs) {
500            input.parse::<kw::stubs>()?;
501            Ok(Opt::Stubs)
502        } else if l.peek(kw::export_prefix) {
503            input.parse::<kw::export_prefix>()?;
504            input.parse::<Token![:]>()?;
505            Ok(Opt::ExportPrefix(input.parse()?))
506        } else if l.peek(kw::additional_derives) {
507            input.parse::<kw::additional_derives>()?;
508            input.parse::<Token![:]>()?;
509            let contents;
510            syn::bracketed!(contents in input);
511            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
512            Ok(Opt::AdditionalDerives(list.iter().cloned().collect()))
513        } else if l.peek(kw::additional_derives_ignore) {
514            input.parse::<kw::additional_derives_ignore>()?;
515            input.parse::<Token![:]>()?;
516            let contents;
517            syn::bracketed!(contents in input);
518            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
519            Ok(Opt::AdditionalDerivesIgnore(list.iter().cloned().collect()))
520        } else if l.peek(kw::with) {
521            input.parse::<kw::with>()?;
522            input.parse::<Token![:]>()?;
523            let contents;
524            let _lbrace = braced!(contents in input);
525            let fields: Punctuated<_, Token![,]> =
526                contents.parse_terminated(with_field_parse, Token![,])?;
527            Ok(Opt::With(HashMap::from_iter(fields)))
528        } else if l.peek(kw::generate_all) {
529            input.parse::<kw::generate_all>()?;
530            Ok(Opt::GenerateAll)
531        } else if l.peek(kw::type_section_suffix) {
532            input.parse::<kw::type_section_suffix>()?;
533            input.parse::<Token![:]>()?;
534            Ok(Opt::TypeSectionSuffix(input.parse()?))
535        } else if l.peek(kw::disable_run_ctors_once_workaround) {
536            input.parse::<kw::disable_run_ctors_once_workaround>()?;
537            input.parse::<Token![:]>()?;
538            Ok(Opt::DisableRunCtorsOnceWorkaround(input.parse()?))
539        } else if l.peek(kw::default_bindings_module) {
540            input.parse::<kw::default_bindings_module>()?;
541            input.parse::<Token![:]>()?;
542            Ok(Opt::DefaultBindingsModule(input.parse()?))
543        } else if l.peek(kw::export_macro_name) {
544            input.parse::<kw::export_macro_name>()?;
545            input.parse::<Token![:]>()?;
546            Ok(Opt::ExportMacroName(input.parse()?))
547        } else if l.peek(kw::pub_export_macro) {
548            input.parse::<kw::pub_export_macro>()?;
549            input.parse::<Token![:]>()?;
550            Ok(Opt::PubExportMacro(input.parse()?))
551        } else if l.peek(kw::generate_unused_types) {
552            input.parse::<kw::generate_unused_types>()?;
553            input.parse::<Token![:]>()?;
554            Ok(Opt::GenerateUnusedTypes(input.parse()?))
555        } else if l.peek(kw::features) {
556            input.parse::<kw::features>()?;
557            input.parse::<Token![:]>()?;
558            let contents;
559            syn::bracketed!(contents in input);
560            let list = Punctuated::<_, Token![,]>::parse_terminated(&contents)?;
561            Ok(Opt::Features(list.into_iter().collect()))
562        } else if l.peek(kw::disable_custom_section_link_helpers) {
563            input.parse::<kw::disable_custom_section_link_helpers>()?;
564            input.parse::<Token![:]>()?;
565            Ok(Opt::DisableCustomSectionLinkHelpers(input.parse()?))
566        } else if l.peek(kw::debug) {
567            input.parse::<kw::debug>()?;
568            input.parse::<Token![:]>()?;
569            Ok(Opt::Debug(input.parse()?))
570        } else if l.peek(kw::enable_method_chaining) {
571            input.parse::<kw::enable_method_chaining>()?;
572            input.parse::<Token![:]>()?;
573            Ok(Opt::EnableMethodChaining(input.parse()?))
574        } else if l.peek(Token![async]) {
575            let span = input.parse::<Token![async]>()?.span;
576            input.parse::<Token![:]>()?;
577            if input.peek(syn::LitBool) {
578                let enabled = input.parse::<syn::LitBool>()?.value;
579                Ok(Opt::Async(AsyncFilterSet::all(enabled), span))
580            } else {
581                let mut set = AsyncFilterSet::default();
582                let contents;
583                syn::bracketed!(contents in input);
584                for val in contents.parse_terminated(|p| p.parse::<syn::LitStr>(), Token![,])? {
585                    set.push(&val.value());
586                }
587                Ok(Opt::Async(set, span))
588            }
589        } else {
590            Err(l.error())
591        }
592    }
593}
594
595fn with_field_parse(input: ParseStream<'_>) -> Result<(String, WithOption)> {
596    let interface = input.parse::<syn::LitStr>()?.value();
597    input.parse::<Token![:]>()?;
598    let start = input.span();
599    let path = input.parse::<syn::Path>()?;
600
601    // It's not possible for the segments of a path to be empty
602    let span = start
603        .join(path.segments.last().unwrap().ident.span())
604        .unwrap_or(start);
605
606    if path.is_ident("generate") {
607        return Ok((interface, WithOption::Generate));
608    }
609
610    let mut buf = String::new();
611    let append = |buf: &mut String, segment: syn::PathSegment| -> Result<()> {
612        if !segment.arguments.is_none() {
613            return Err(Error::new(
614                span,
615                "Module path must not contain angles or parens",
616            ));
617        }
618
619        buf.push_str(&segment.ident.to_string());
620
621        Ok(())
622    };
623
624    if path.leading_colon.is_some() {
625        buf.push_str("::");
626    }
627
628    let mut segments = path.segments.into_iter();
629
630    if let Some(segment) = segments.next() {
631        append(&mut buf, segment)?;
632    }
633
634    for segment in segments {
635        buf.push_str("::");
636        append(&mut buf, segment)?;
637    }
638
639    Ok((interface, WithOption::Path(buf)))
640}
641
642/// Format a valid Rust string
643fn fmt(input: &str) -> Result<String> {
644    let syntax_tree = syn::parse_file(input)?;
645    Ok(prettyplease::unparse(&syntax_tree))
646}