Skip to content

specialize ConversionFlag #42

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

Merged
merged 3 commits into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ast/asdl_rs.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
BUILTIN_INT_NAMES = {
"simple": "bool",
"is_async": "bool",
"conversion": "ConversionFlag",
}

RUST_KEYWORDS = {
Expand Down
3 changes: 2 additions & 1 deletion ast/src/fold_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{builtin, fold::Fold};
use crate::{builtin, fold::Fold, ConversionFlag};

pub trait Foldable<T, U> {
type Mapped;
Expand Down Expand Up @@ -67,5 +67,6 @@ simple_fold!(
builtin::String,
builtin::Identifier,
bool,
ConversionFlag,
builtin::Constant
);
2 changes: 1 addition & 1 deletion ast/src/gen/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ impl<R> From<ExprCall<R>> for Expr<R> {
pub struct ExprFormattedValue<R = TextRange> {
pub range: R,
pub value: Box<Expr<R>>,
pub conversion: Int,
pub conversion: ConversionFlag,
pub format_spec: Option<Box<Expr<R>>>,
}

Expand Down
2 changes: 1 addition & 1 deletion ast/src/generic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#![allow(clippy::derive_partial_eq_without_eq)]
pub use crate::{builtin::*, text_size::TextSize, Node};
pub use crate::{builtin::*, text_size::TextSize, ConversionFlag, Node};
use std::fmt::{Debug, Display, Formatter};
use std::marker::PhantomData;

Expand Down
9 changes: 8 additions & 1 deletion ast/src/pyo3.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{source_code::SourceRange, text_size::TextRange, Node};
use crate::{source_code::SourceRange, text_size::TextRange, ConversionFlag, Node};
use num_complex::Complex64;
use once_cell::sync::OnceCell;
use pyo3::prelude::*;
Expand Down Expand Up @@ -73,6 +73,13 @@ impl ToPyo3Ast for bool {
}
}

impl ToPyo3Ast for ConversionFlag {
#[inline]
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
Ok((*self as i8).to_object(py))
}
}

impl ToPyo3Ast for crate::Constant {
#[inline]
fn to_pyo3_ast(&self, py: Python) -> PyResult<Py<PyAny>> {
Expand Down
8 changes: 4 additions & 4 deletions ast/src/unparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ impl<'a> Unparser<'a> {
conversion,
format_spec,
range: _range,
}) => self.unparse_formatted(value, conversion.to_u32(), format_spec.as_deref())?,
}) => self.unparse_formatted(value, *conversion, format_spec.as_deref())?,
Expr::JoinedStr(crate::ExprJoinedStr {
values,
range: _range,
Expand Down Expand Up @@ -519,7 +519,7 @@ impl<'a> Unparser<'a> {
fn unparse_formatted<U>(
&mut self,
val: &Expr<U>,
conversion: u32,
conversion: ConversionFlag,
spec: Option<&Expr<U>>,
) -> fmt::Result {
let buffered = to_string_fmt(|f| Unparser::new(f).unparse_expr(val, precedence::TEST + 1));
Expand All @@ -533,7 +533,7 @@ impl<'a> Unparser<'a> {
self.p(&buffered)?;
drop(buffered);

if conversion != ConversionFlag::None as u32 {
if conversion != ConversionFlag::None {
self.p("!")?;
let buf = &[conversion as u8];
let c = std::str::from_utf8(buf).unwrap();
Expand Down Expand Up @@ -568,7 +568,7 @@ impl<'a> Unparser<'a> {
conversion,
format_spec,
range: _range,
}) => self.unparse_formatted(value, conversion.to_u32(), format_spec.as_deref()),
}) => self.unparse_formatted(value, *conversion, format_spec.as_deref()),
_ => unreachable!(),
}
}
Expand Down
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ ruff_text_size = { path = "../ruff_text_size" }
ruff_source_location = { path = "../ruff_source_location", optional = true }

serde = { version = "1.0.133", optional = true, default-features = false, features = ["derive"] }
is-macro.workspace = true

[features]
default = []
Expand Down
12 changes: 6 additions & 6 deletions core/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/// Transforms a value prior to formatting it.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
#[repr(u8)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, is_macro::Is)]
#[repr(i8)]
pub enum ConversionFlag {
/// No conversion
None = 0, // CPython uses -1 but not pleasure for us
None = -1, // CPython uses -1
/// Converts by calling `str(<value>)`.
Str = b's',
Str = b's' as i8,
/// Converts by calling `ascii(<value>)`.
Ascii = b'a',
Ascii = b'a' as i8,
/// Converts by calling `repr(<value>)`.
Repr = b'r',
Repr = b'r' as i8,
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading