| 1 |
use std::{fmt, str::FromStr}; |
| 2 |
|
| 3 |
#[derive(Default, Debug, Clone, Copy, Eq, PartialEq)] |
| 4 |
pub enum Visibility { |
| 5 |
#[default] |
| 6 |
Public, |
| 7 |
Crate, |
| 8 |
Super, |
| 9 |
Private, |
| 10 |
} |
| 11 |
|
| 12 |
impl Visibility { |
| 13 |
pub fn is_public(self) -> bool { |
| 14 |
self == Self::Public |
| 15 |
} |
| 16 |
|
| 17 |
pub fn export_visibility(self) -> &'static str { |
| 18 |
match self { |
| 19 |
Self::Public => "pub", |
| 20 |
Self::Private => "", |
| 21 |
Self::Crate => "pub(crate)", |
| 22 |
Self::Super => "pub(super)", |
| 23 |
} |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
impl fmt::Display for Visibility { |
| 28 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 29 |
f.write_str(self.export_visibility()) |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
#[derive(Debug)] |
| 34 |
pub struct ParseVisibilityError(String); |
| 35 |
|
| 36 |
impl std::error::Error for ParseVisibilityError {} |
| 37 |
|
| 38 |
impl fmt::Display for ParseVisibilityError { |
| 39 |
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 40 |
write!(f, "{}", self.0) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
impl FromStr for Visibility { |
| 45 |
type Err = ParseVisibilityError; |
| 46 |
|
| 47 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 48 |
match s { |
| 49 |
"pub" => Ok(Self::Public), |
| 50 |
"super" => Ok(Self::Super), |
| 51 |
"private" => Ok(Self::Private), |
| 52 |
"crate" => Ok(Self::Crate), |
| 53 |
e => Err(ParseVisibilityError(format!("Wrong visibility type '{e}'"))), |
| 54 |
} |
| 55 |
} |
| 56 |
} |