Skip to content

Commit

Permalink
add pymethods to extract default
Browse files Browse the repository at this point in the history
  • Loading branch information
JR-1991 committed Nov 28, 2024
1 parent 54f43d8 commit 91c0efc
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{error::Error, fmt, str::FromStr};

#[cfg(feature = "python")]
use pyo3::pyclass;
use pyo3::pymethods;

/// Represents an attribute with various properties and options.
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
Expand Down Expand Up @@ -210,6 +211,57 @@ pub enum DataType {
String(String),
}

#[cfg_attr(feature = "python", pymethods)]
impl DataType {
pub fn is_boolean(&self) -> bool {
matches!(self, DataType::Boolean(_))
}

pub fn is_integer(&self) -> bool {
matches!(self, DataType::Integer(_))
}

pub fn is_float(&self) -> bool {
matches!(self, DataType::Float(_))
}

pub fn is_string(&self) -> bool {
matches!(self, DataType::String(_))
}

pub fn as_boolean(&self) -> Option<bool> {
if let DataType::Boolean(value) = self {
Some(*value)
} else {
None
}
}

pub fn as_integer(&self) -> Option<i64> {
if let DataType::Integer(value) = self {
Some(*value)
} else {
None
}
}

pub fn as_float(&self) -> Option<f64> {
if let DataType::Float(value) = self {
Some(*value)
} else {
None
}
}

pub fn as_string(&self) -> Option<String> {
if let DataType::String(value) = self {
Some(value.clone())
} else {
None
}
}
}

impl PartialEq for DataType {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand Down

0 comments on commit 91c0efc

Please sign in to comment.