-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
TypedDict
for **kwargs
- Loading branch information
Showing
3 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use std::str::FromStr; | ||
|
||
use pyo3::intern; | ||
use pyo3::prelude::*; | ||
use pyo3::types::{PyDict, PyString}; | ||
|
||
use crate::input::Input; | ||
use crate::tools::SchemaDict; | ||
|
||
use super::validation_state::ValidationState; | ||
use super::{build_validator, BuildValidator, CombinedValidator, DefinitionsBuilder, Validator}; | ||
|
||
enum VarKwargsMode { | ||
Single, | ||
TypedDict, | ||
} | ||
|
||
impl FromStr for VarKwargsMode { | ||
type Err = (); | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"single" => Ok(VarKwargsMode::Single), | ||
"typed_dict" => Ok(VarKwargsMode::TypedDict), | ||
_ => Err(()), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct VarKwargsValidator { | ||
mode: VarKwargsMode, | ||
validator: Box<CombinedValidator>, | ||
} | ||
|
||
impl BuildValidator for VarKwargsValidator { | ||
const EXPECTED_TYPE: &'static str = "var_kwargs"; | ||
|
||
fn build( | ||
schema: &Bound<'_, PyDict>, | ||
config: Option<&Bound<'_, PyDict>>, | ||
definitions: &mut DefinitionsBuilder<CombinedValidator>, | ||
) -> PyResult<CombinedValidator> { | ||
let py = schema.py(); | ||
|
||
let py_mode: Bound<PyString> = schema.get_as_req(intern!(py, "mode"))?; | ||
let mode = VarKwargsMode::from_str(py_mode.to_string().as_str())?; | ||
|
||
let validator_schema: Bound<PyDict> = schema.get_as_req(intern!(py, "schema"))?; | ||
|
||
Ok(Self { | ||
mode, | ||
validator: Box::new(build_validator(&validator_schema, config, definitions)?), | ||
} | ||
.into()) | ||
} | ||
} | ||
|
||
impl_py_gc_traverse!(VarKwargsValidator { mode, validator }); | ||
|
||
impl Validator for VarKwargsValidator { | ||
fn validate<'py>( | ||
&self, | ||
py: Python<'py>, | ||
input: &(impl Input<'py> + ?Sized), | ||
state: &mut ValidationState<'_, 'py>, | ||
) -> ValResult<PyObject> { | ||
match self.mode { | ||
VarKwargsMode::Single => { | ||
// TODO iter over kwargs | ||
} | ||
VarKwargsMode::TypedDict => { | ||
// TODO | ||
} | ||
} | ||
} | ||
} |