-
Notifications
You must be signed in to change notification settings - Fork 21
[ffe] add pyo3 conversion methods #1289
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 8 commits
8a462e1
b4f03b9
44f5f49
466bafa
572804b
750dcec
f8da08b
dad6a08
36fe6be
6b94369
3ba037f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,10 +10,10 @@ use crate::rules_based::Str; | |
| /// Attribute for evaluation context. See `From` implementations for initialization. | ||
| #[derive(Debug, Clone, PartialEq, PartialOrd, derive_more::From, Serialize, Deserialize)] | ||
| #[from(f64, bool, Str, String, &str, Arc<str>, Arc<String>, Cow<'_, str>)] | ||
| pub struct Attribute(AttributeValueImpl); | ||
| pub struct Attribute(AttributeImpl); | ||
| #[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize, derive_more::From)] | ||
| #[serde(untagged)] | ||
| enum AttributeValueImpl { | ||
| enum AttributeImpl { | ||
| #[from] | ||
| Number(f64), | ||
| #[from(forward)] | ||
|
|
@@ -26,7 +26,7 @@ enum AttributeValueImpl { | |
|
|
||
| impl Attribute { | ||
| pub(crate) fn is_null(&self) -> bool { | ||
| self == &Attribute(AttributeValueImpl::Null) | ||
| self == &Attribute(AttributeImpl::Null) | ||
| } | ||
|
|
||
| /// Try coercing attribute to a number. | ||
|
|
@@ -35,8 +35,8 @@ impl Attribute { | |
| /// number. | ||
| pub(crate) fn coerce_to_number(&self) -> Option<f64> { | ||
| match &self.0 { | ||
| AttributeValueImpl::Number(v) => Some(*v), | ||
| AttributeValueImpl::String(s) => s.parse().ok(), | ||
| AttributeImpl::Number(v) => Some(*v), | ||
| AttributeImpl::String(s) => s.parse().ok(), | ||
| _ => None, | ||
| } | ||
| } | ||
|
|
@@ -46,19 +46,61 @@ impl Attribute { | |
| /// String attributes are returned as is. Number and boolean attributes are converted to string. | ||
| pub(crate) fn coerce_to_string(&self) -> Option<Cow<'_, str>> { | ||
| match &self.0 { | ||
| AttributeValueImpl::String(s) => Some(Cow::Borrowed(s)), | ||
| AttributeValueImpl::Number(v) => Some(Cow::Owned(v.to_string())), | ||
| AttributeValueImpl::Boolean(v) => { | ||
| Some(Cow::Borrowed(if *v { "true" } else { "false" })) | ||
| } | ||
| AttributeValueImpl::Null => None, | ||
| AttributeImpl::String(s) => Some(Cow::Borrowed(s)), | ||
| AttributeImpl::Number(v) => Some(Cow::Owned(v.to_string())), | ||
| AttributeImpl::Boolean(v) => Some(Cow::Borrowed(if *v { "true" } else { "false" })), | ||
| AttributeImpl::Null => None, | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn as_str(&self) -> Option<&Str> { | ||
| match self { | ||
| Attribute(AttributeValueImpl::String(s)) => Some(s), | ||
| Attribute(AttributeImpl::String(s)) => Some(s), | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(feature = "pyo3")] | ||
| mod pyo3_impl { | ||
| use super::*; | ||
|
|
||
| use pyo3::{ | ||
| exceptions::PyTypeError, | ||
| prelude::*, | ||
| types::{PyBool, PyFloat, PyInt, PyString}, | ||
| }; | ||
|
|
||
| /// Convert Python value to Attribute. | ||
| /// | ||
| /// The following types are currently supported: | ||
| /// - `str` | ||
| /// - `int` | ||
| /// - `float` | ||
| /// - `bool` | ||
| /// - `NoneType` | ||
| /// | ||
| /// Note that nesting is not currently supported and will throw an error. | ||
| impl<'py> FromPyObject<'py> for Attribute { | ||
| #[inline] | ||
| fn extract_bound(value: &Bound<'py, PyAny>) -> PyResult<Self> { | ||
| if let Ok(s) = value.downcast::<PyString>() { | ||
| return Ok(Attribute(AttributeImpl::String(s.to_cow()?.into()))); | ||
| } | ||
| // In Python, Bool inherits from Int, so it must be checked first here. | ||
| if let Ok(s) = value.downcast::<PyBool>() { | ||
| return Ok(Attribute(AttributeImpl::Boolean(s.is_true()))); | ||
| } | ||
| if let Ok(s) = value.downcast::<PyFloat>() { | ||
| return Ok(Attribute(AttributeImpl::Number(s.value()))); | ||
| } | ||
| if let Ok(s) = value.downcast::<PyInt>() { | ||
| return Ok(Attribute(AttributeImpl::Number(s.extract::<f64>()?))); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really. We only support float attributes, so ints are squashed to floats here |
||
| } | ||
| if value.is_none() { | ||
| return Ok(Attribute(AttributeImpl::Null)); | ||
| } | ||
| Err(PyTypeError::new_err("invalid type for attribute")) | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍