specialize ConversionFlag (#42)

* specialize ConversionFlag

* Change value of ConversionFlag to i8 and None to -1

* is_* methods to ConversionFlag
This commit is contained in:
Jeong, YunWon 2023-05-16 22:52:50 +09:00 committed by GitHub
parent 611dcc2e9b
commit 9d47d3d212
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
29 changed files with 60 additions and 104 deletions

View file

@ -1,4 +1,4 @@
use crate::{builtin, fold::Fold};
use crate::{builtin, fold::Fold, ConversionFlag};
pub trait Foldable<T, U> {
type Mapped;
@ -67,5 +67,6 @@ simple_fold!(
builtin::String,
builtin::Identifier,
bool,
ConversionFlag,
builtin::Constant
);

View file

@ -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>>>,
}

View file

@ -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;

View file

@ -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::*;
@ -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>> {

View file

@ -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,
@ -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));
@ -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();
@ -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!(),
}
}