Skip to content

Commit

Permalink
Refactor type name
Browse files Browse the repository at this point in the history
  • Loading branch information
FlickerSoul committed Jul 22, 2024
1 parent abae882 commit 5d7a29e
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions pyo3-macros-backend/src/pyfunction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,32 +85,34 @@ impl PyFunctionArgPyO3Attributes {
}
}

type FunctionWarningMessageAttribute = KeywordAttribute<attributes::kw::message, LitStr>;
type FunctionWarningCategoryAttribute = KeywordAttribute<attributes::kw::category, Path>;
type PyFunctionWarningMessageAttribute = KeywordAttribute<attributes::kw::message, LitStr>;
type PyFunctionWarningCategoryAttribute = KeywordAttribute<attributes::kw::category, Path>;
type PyFunctionDeprecatedWarningAttribute = KeywordAttribute<attributes::kw::deprecated, LitStr>;

pub struct PyFunctionWarningAttribute {
pub message: FunctionWarningMessageAttribute,
pub category: Option<FunctionWarningCategoryAttribute>,
pub message: PyFunctionWarningMessageAttribute,
pub category: Option<PyFunctionWarningCategoryAttribute>,
pub span: Span,
}

pub enum Category {
pub enum PyFunctionWarningCategory {
Path(Path),
DeprecationWarning,
}

pub struct PyFunctionWarning {
pub message: LitStr,
pub category: Option<Category>,
pub category: Option<PyFunctionWarningCategory>,
pub span: Span,
}

impl From<PyFunctionWarningAttribute> for PyFunctionWarning {
fn from(value: PyFunctionWarningAttribute) -> Self {
Self {
message: value.message.value,
category: value.category.map(|cat| Category::Path(cat.value)),
category: value
.category
.map(|cat| PyFunctionWarningCategory::Path(cat.value)),
span: value.span,
}
}
Expand All @@ -121,7 +123,7 @@ impl From<PyFunctionDeprecatedWarningAttribute> for PyFunctionWarning {
Self {
span: value.span(),
message: value.value,
category: Some(Category::DeprecationWarning),
category: Some(PyFunctionWarningCategory::DeprecationWarning),
}
}
}
Expand All @@ -143,8 +145,8 @@ impl WarningFactory for PyFunctionWarning {
let category = self.category.as_ref().map_or(
quote! {#pyo3_path::exceptions::PyUserWarning},
|category| match category {
Category::Path(path) => quote! {#path},
Category::DeprecationWarning => {
PyFunctionWarningCategory::Path(path) => quote! {#path},
PyFunctionWarningCategory::DeprecationWarning => {
quote! {#pyo3_path::exceptions::PyDeprecationWarning}
}
},
Expand Down Expand Up @@ -177,8 +179,8 @@ impl<T: WarningFactory> WarningFactory for Vec<T> {

impl Parse for PyFunctionWarningAttribute {
fn parse(input: ParseStream<'_>) -> Result<Self> {
let mut message: Option<FunctionWarningMessageAttribute> = None;
let mut category: Option<FunctionWarningCategoryAttribute> = None;
let mut message: Option<PyFunctionWarningMessageAttribute> = None;
let mut category: Option<PyFunctionWarningCategoryAttribute> = None;

let span = input.parse::<attributes::kw::warn>()?.span();

Expand All @@ -190,11 +192,11 @@ impl Parse for PyFunctionWarningAttribute {

if lookahead.peek(attributes::kw::message) {
message = content
.parse::<FunctionWarningMessageAttribute>()
.parse::<PyFunctionWarningMessageAttribute>()
.map(Some)?;
} else if lookahead.peek(attributes::kw::category) {
category = content
.parse::<FunctionWarningCategoryAttribute>()
.parse::<PyFunctionWarningCategoryAttribute>()
.map(Some)?;
} else {
return Err(lookahead.error());
Expand Down

0 comments on commit 5d7a29e

Please sign in to comment.