loess_rust/names/
visibility_and_privacy.rs

1use loess::{Error, ErrorPriority, Errors, Input, IntoTokens, PeekFrom, PopFrom};
2use proc_macro2::{TokenStream, TokenTree};
3
4use crate::{Parentheses, Pub};
5
6/// [*Visibility*](https://doc.rust-lang.org/stable/reference/visibility-and-privacy.html#r-vis.syntax):
7/// [`pub`](`Pub`) [`(`](`Parentheses`) [`T`](`TokenStream`) [`)`](`Parentheses`)<sup>?</sup>
8#[derive(Clone)]
9pub struct Visibility<T = TokenStream> {
10	#[allow(missing_docs)]
11	pub r#pub: Pub,
12	#[allow(missing_docs)]
13	pub parentheses: Option<Parentheses<T>>,
14}
15
16impl<T> PeekFrom for Visibility<T> {
17	fn peek_from(input: &Input) -> bool {
18		Pub::peek_from(input)
19	}
20}
21
22impl<T: PopFrom> PopFrom for Visibility<T> {
23	fn pop_from(input: &mut Input, errors: &mut Errors) -> Result<Self, ()> {
24		if let Some(r#pub) = Pub::peek_pop_from(input, errors)? {
25			Ok(Self {
26				r#pub,
27				parentheses: Parentheses::<TokenStream>::peek_from(input)
28					.then(|| Parentheses::pop_from(input, errors))
29					.transpose()?,
30			})
31		} else {
32			Err(errors.push(Error::new(
33				ErrorPriority::GRAMMAR,
34				"Expected Visibility. (Expected `pub`.)",
35				[input.front_span()],
36			)))
37		}
38	}
39}
40
41impl IntoTokens for Visibility {
42	fn into_tokens(self, root: &TokenStream, tokens: &mut impl Extend<TokenTree>) {
43		let Self { r#pub, parentheses } = self;
44		r#pub.into_tokens(root, tokens);
45		parentheses.into_tokens(root, tokens);
46	}
47}