Debian Codegraph @

src/visibility.rs

raw symbols SCIP
1 use syn::{parse_quote, Visibility};
2
3 // If `vis` describes a visibility relative to some module scope, returns the
4 // same visibility as described from a module contained within that scope.
5 //
6 // quote! {
7 // #vis #A;
8 // mod #name {
9 // // Same visibility as super::A.
10 // #vis_super #B;
11 // }
12 // }
13 //
14 // Note if you are considering copying this implementation into another crate:
15 // please try not to. Visibilities in Rust are confusing and subtle, especially
16 // around visibility specifiers of items declared inside of a function body.
17 // This function is not a robust implementation of the transformation that it
18 // claims to implement. Always first try to restructure your code to avoid
19 // needing this logic.
20 pub fn vis_super(vis: &Visibility) -> Visibility {
21 match vis {
22 Visibility::Public(vis) => {
23 // pub -> pub
24 parse_quote!(#vis)
25 }
26 Visibility::Crate(vis) => {
27 // crate -> crate
28 parse_quote!(#vis)
29 }
30 Visibility::Restricted(vis) => {
31 if vis.path.segments[0].ident == "self" {
32 // pub(self) -> pub(in super)
33 // pub(in self::super) -> pub(in super::super)
34 let path = vis.path.segments.iter().skip(1);
35 parse_quote!(pub(in super #(::#path)*))
36 } else if vis.path.segments[0].ident == "super" {
37 // pub(super) -> pub(in super::super)
38 // pub(in super::super) -> pub(in super::super::super)
39 let path = &vis.path;
40 parse_quote!(pub(in super::#path))
41 } else {
42 // pub(crate) -> pub(crate)
43 // pub(in crate::path) -> pub(in crate::path)
44 parse_quote!(#vis)
45 }
46 }
47 Visibility::Inherited => {
48 // private -> pub(super)
49 parse_quote!(pub(super))
50 }
51 }
52 }