Skip to content

Special case bool literals #784

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 2 commits into from
Jul 17, 2023
Merged
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
38 changes: 35 additions & 3 deletions src/validators/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ use crate::tools::SchemaDict;

use super::{BuildValidator, CombinedValidator, Definitions, DefinitionsBuilder, Extra, Validator};

#[derive(Debug, Clone)]
struct BoolLiteral {
pub true_id: Option<usize>,
pub false_id: Option<usize>,
}

#[derive(Debug, Clone)]
pub struct LiteralLookup<T: Clone + Debug> {
// Specialized lookups for ints and strings because they
// Specialized lookups for ints, bools and strings because they
// (1) are easy to convert between Rust and Python
// (2) hashing them in Rust is very fast
// (3) are the most commonly used things in Literal[...]
expected_bool: Option<BoolLiteral>,
expected_int: Option<AHashMap<i64, usize>>,
expected_str: Option<AHashMap<String, usize>>,
// Catch all for Enum and bytes (the latter only because it is seldom used)
Expand All @@ -31,12 +38,23 @@ pub struct LiteralLookup<T: Clone + Debug> {
impl<T: Clone + Debug> LiteralLookup<T> {
pub fn new<'py>(py: Python<'py>, expected: impl Iterator<Item = (&'py PyAny, T)>) -> PyResult<Self> {
let mut expected_int = AHashMap::new();
let mut expected_str = AHashMap::new();
let mut expected_str: AHashMap<String, usize> = AHashMap::new();
let mut expected_bool = BoolLiteral {
true_id: None,
false_id: None,
};
let expected_py = PyDict::new(py);
let mut values = Vec::new();
for (k, v) in expected {
let id = values.len();
values.push(v);
if let Ok(bool) = k.strict_bool() {
if bool {
expected_bool.true_id = Some(id);
} else {
expected_bool.false_id = Some(id);
}
}
Comment on lines +51 to +57
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The chain of if let Ok(...) = input.x() here just seems to scream at me for the input-as-enum we spoke about the other day...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed!

if let Ok(either_int) = k.exact_int() {
let int = either_int
.into_i64(py)
Expand All @@ -53,6 +71,10 @@ impl<T: Clone + Debug> LiteralLookup<T> {
}

Ok(Self {
expected_bool: match expected_bool.true_id.is_some() || expected_bool.false_id.is_some() {
true => Some(expected_bool),
false => None,
},
expected_int: match expected_int.is_empty() {
true => None,
false => Some(expected_int),
Expand All @@ -74,7 +96,17 @@ impl<T: Clone + Debug> LiteralLookup<T> {
py: Python<'data>,
input: &'data I,
) -> ValResult<'data, Option<(&'data I, &T)>> {
// dbg!(input.to_object(py).as_ref(py).repr().unwrap());
if let Some(expected_bool) = &self.expected_bool {
if let Ok(bool_value) = input.strict_bool() {
if bool_value {
if let Some(true_value) = &expected_bool.true_id {
return Ok(Some((input, &self.values[*true_value])));
}
} else if let Some(false_value) = &expected_bool.false_id {
return Ok(Some((input, &self.values[*false_value])));
}
}
}
if let Some(expected_ints) = &self.expected_int {
if let Ok(either_int) = input.exact_int() {
let int = either_int.into_i64(py)?;
Expand Down