use std::fmt::{self, Display};
#[derive(Debug, PartialEq)]
#[non_exhaustive]
pub struct NewName {
pub(crate) new_name: String,
pub(crate) returns_bool: ReturnsBool,
pub(crate) rule: NewNameRule,
}
impl NewName {
pub fn as_str(&self) -> &str {
self.new_name.as_str()
}
pub fn unwrap(self) -> String {
self.new_name
}
pub fn returns_bool(&self) -> ReturnsBool {
self.returns_bool
}
pub fn rule(&self) -> NewNameRule {
self.rule
}
pub fn is_fixed(&self) -> bool {
self.rule.is_fixed()
}
pub fn is_substituted(&self) -> bool {
self.rule.is_substituted()
}
pub fn is_regular(&self) -> bool {
self.rule.is_regular()
}
pub fn is_no_prefix(&self) -> bool {
self.rule.is_no_prefix()
}
}
impl Display for NewName {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
write!(f, "{}{} {}", self.returns_bool, self.rule, self.new_name)
}
}
impl<T: AsRef<str>> PartialEq<T> for NewName {
fn eq(&self, other: &T) -> bool {
self.as_str() == other.as_ref()
}
}
#[derive(Clone, Copy, Debug, PartialEq)]
#[non_exhaustive]
pub enum NewNameRule {
Fixed,
Regular,
NoPrefix,
Substituted,
}
impl NewNameRule {
pub fn is_fixed(&self) -> bool {
matches!(self, NewNameRule::Fixed)
}
pub fn is_substituted(&self) -> bool {
matches!(self, NewNameRule::Substituted)
}
pub fn is_regular(&self) -> bool {
matches!(self, NewNameRule::Regular)
}
pub fn is_no_prefix(&self) -> bool {
matches!(self, NewNameRule::NoPrefix)
}
}
impl Display for NewNameRule {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use NewNameRule::*;
match self {
Fixed => f.write_str("fixed as"),
Substituted => f.write_str("substituted with"),
NoPrefix => f.write_str("kept as"),
Regular => f.write_str("renamed as"),
}
}
}
#[derive(Debug, Copy, Clone, PartialEq)]
pub enum ReturnsBool {
True,
False,
Maybe,
}
impl ReturnsBool {
pub fn is_true(&self) -> bool {
matches!(self, ReturnsBool::True)
}
pub fn is_false(&self) -> bool {
matches!(self, ReturnsBool::False)
}
pub fn is_maybe(&self) -> bool {
matches!(self, ReturnsBool::Maybe)
}
}
impl From<bool> for ReturnsBool {
fn from(returns_bool: bool) -> Self {
if returns_bool {
ReturnsBool::True
} else {
ReturnsBool::False
}
}
}
impl Display for ReturnsBool {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
use ReturnsBool::*;
match self {
False => Ok(()),
True => f.write_str("-> bool "),
Maybe => f.write_str("-> ? "),
}
}
}