solana_sdk/
commitment_config.rs#![cfg(feature = "full")]
use {std::str::FromStr, thiserror::Error};
#[derive(Serialize, Deserialize, Default, Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub struct CommitmentConfig {
pub commitment: CommitmentLevel,
}
impl CommitmentConfig {
pub const fn finalized() -> Self {
Self {
commitment: CommitmentLevel::Finalized,
}
}
pub const fn confirmed() -> Self {
Self {
commitment: CommitmentLevel::Confirmed,
}
}
pub const fn processed() -> Self {
Self {
commitment: CommitmentLevel::Processed,
}
}
pub fn ok(self) -> Option<Self> {
if self == Self::default() {
None
} else {
Some(self)
}
}
pub fn is_finalized(&self) -> bool {
self.commitment == CommitmentLevel::Finalized
}
pub fn is_confirmed(&self) -> bool {
self.commitment == CommitmentLevel::Confirmed
}
pub fn is_processed(&self) -> bool {
self.commitment == CommitmentLevel::Processed
}
pub fn is_at_least_confirmed(&self) -> bool {
self.is_confirmed() || self.is_finalized()
}
#[deprecated(
since = "2.0.2",
note = "Returns self. Please do not use. Will be removed in the future."
)]
pub fn use_deprecated_commitment(commitment: CommitmentConfig) -> Self {
commitment
}
}
impl FromStr for CommitmentConfig {
type Err = ParseCommitmentLevelError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
CommitmentLevel::from_str(s).map(|commitment| Self { commitment })
}
}
#[derive(Serialize, Deserialize, Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[serde(rename_all = "camelCase")]
pub enum CommitmentLevel {
Processed,
Confirmed,
Finalized,
}
impl Default for CommitmentLevel {
fn default() -> Self {
Self::Finalized
}
}
impl FromStr for CommitmentLevel {
type Err = ParseCommitmentLevelError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"processed" => Ok(CommitmentLevel::Processed),
"confirmed" => Ok(CommitmentLevel::Confirmed),
"finalized" => Ok(CommitmentLevel::Finalized),
_ => Err(ParseCommitmentLevelError::Invalid),
}
}
}
impl std::fmt::Display for CommitmentLevel {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let s = match self {
CommitmentLevel::Processed => "processed",
CommitmentLevel::Confirmed => "confirmed",
CommitmentLevel::Finalized => "finalized",
};
write!(f, "{s}")
}
}
#[derive(Error, Debug)]
pub enum ParseCommitmentLevelError {
#[error("invalid variant")]
Invalid,
}