use serde::{Deserialize, Serialize};
use sway_ast::attribute::{STORAGE_READ_ARG_NAME, STORAGE_WRITE_ARG_NAME};
#[derive(Clone, Debug, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum Purity {
#[default]
Pure,
Reads,
Writes,
ReadsWrites,
}
impl Purity {
pub fn can_call(&self, other: Purity) -> bool {
match self {
Purity::Pure => other == Purity::Pure,
Purity::Reads => other == Purity::Pure || other == Purity::Reads,
Purity::Writes => true, Purity::ReadsWrites => true,
}
}
pub fn to_attribute_syntax(&self) -> String {
match self {
Purity::Pure => "".to_owned(),
Purity::Reads => STORAGE_READ_ARG_NAME.to_owned(),
Purity::Writes => STORAGE_WRITE_ARG_NAME.to_owned(),
Purity::ReadsWrites => {
format!("{STORAGE_READ_ARG_NAME}, {STORAGE_WRITE_ARG_NAME}")
}
}
}
}
pub fn promote_purity(from: Purity, to: Purity) -> Purity {
match (from, to) {
(Purity::Reads, Purity::Writes)
| (Purity::Writes, Purity::Reads)
| (Purity::ReadsWrites, _) => Purity::ReadsWrites,
_otherwise => to,
}
}