| 
 | 1 | +use std::borrow::Cow;  | 
 | 2 | +use std::sync::Arc;  | 
 | 3 | + | 
 | 4 | +use pyo3::exceptions::PyValueError;  | 
 | 5 | +use pyo3::intern;  | 
 | 6 | +use pyo3::prelude::*;  | 
 | 7 | +use pyo3::types::{PyBool, PyDict, PyType};  | 
 | 8 | + | 
 | 9 | +use crate::definitions::DefinitionsBuilder;  | 
 | 10 | +use crate::tools::SchemaDict;  | 
 | 11 | +use crate::SchemaSerializer;  | 
 | 12 | + | 
 | 13 | +use super::extra::Extra;  | 
 | 14 | +use super::shared::{BuildSerializer, CombinedSerializer, TypeSerializer};  | 
 | 15 | + | 
 | 16 | +#[derive(Debug)]  | 
 | 17 | +pub struct PrebuiltSerializer {  | 
 | 18 | +    serializer: Arc<CombinedSerializer>,  | 
 | 19 | +}  | 
 | 20 | + | 
 | 21 | +impl BuildSerializer for PrebuiltSerializer {  | 
 | 22 | +    const EXPECTED_TYPE: &'static str = "prebuilt";  | 
 | 23 | + | 
 | 24 | +    fn build(  | 
 | 25 | +        schema: &Bound<'_, PyDict>,  | 
 | 26 | +        _config: Option<&Bound<'_, PyDict>>,  | 
 | 27 | +        _definitions: &mut DefinitionsBuilder<CombinedSerializer>,  | 
 | 28 | +    ) -> PyResult<CombinedSerializer> {  | 
 | 29 | +        let py = schema.py();  | 
 | 30 | +        let class: Bound<'_, PyType> = schema.get_as_req(intern!(py, "cls"))?;  | 
 | 31 | + | 
 | 32 | +        // note: we NEED to use the __dict__ here (and perform get_item calls rather than getattr)  | 
 | 33 | +        // because we don't want to fetch prebuilt serializers from parent classes  | 
 | 34 | +        let class_dict: Bound<'_, PyDict> = class.getattr(intern!(py, "__dict__"))?.extract()?;  | 
 | 35 | + | 
 | 36 | +        // Ensure the class has completed its Pydantic setup  | 
 | 37 | +        let is_complete: bool = class_dict  | 
 | 38 | +            .get_as_req::<Bound<'_, PyBool>>(intern!(py, "__pydantic_complete__"))  | 
 | 39 | +            .is_ok_and(|b| b.extract().unwrap_or(false));  | 
 | 40 | + | 
 | 41 | +        if !is_complete {  | 
 | 42 | +            return Err(PyValueError::new_err("Prebuilt serializer not found."));  | 
 | 43 | +        }  | 
 | 44 | + | 
 | 45 | +        // Retrieve the prebuilt validator if available  | 
 | 46 | +        let prebuilt_serializer: Bound<'_, PyAny> = class_dict.get_as_req(intern!(py, "__pydantic_serializer__"))?;  | 
 | 47 | +        let schema_serializer: PyRef<SchemaSerializer> = prebuilt_serializer.extract()?;  | 
 | 48 | +        let combined_serializer: Arc<CombinedSerializer> = schema_serializer.serializer.clone();  | 
 | 49 | + | 
 | 50 | +        Ok(Self {  | 
 | 51 | +            serializer: combined_serializer,  | 
 | 52 | +        }  | 
 | 53 | +        .into())  | 
 | 54 | +    }  | 
 | 55 | +}  | 
 | 56 | + | 
 | 57 | +impl_py_gc_traverse!(PrebuiltSerializer { serializer });  | 
 | 58 | + | 
 | 59 | +impl TypeSerializer for PrebuiltSerializer {  | 
 | 60 | +    fn to_python(  | 
 | 61 | +        &self,  | 
 | 62 | +        value: &Bound<'_, PyAny>,  | 
 | 63 | +        include: Option<&Bound<'_, PyAny>>,  | 
 | 64 | +        exclude: Option<&Bound<'_, PyAny>>,  | 
 | 65 | +        extra: &Extra,  | 
 | 66 | +    ) -> PyResult<PyObject> {  | 
 | 67 | +        self.serializer.to_python(value, include, exclude, extra)  | 
 | 68 | +    }  | 
 | 69 | + | 
 | 70 | +    fn json_key<'a>(&self, key: &'a Bound<'_, PyAny>, extra: &Extra) -> PyResult<Cow<'a, str>> {  | 
 | 71 | +        self.serializer.json_key(key, extra)  | 
 | 72 | +    }  | 
 | 73 | + | 
 | 74 | +    fn serde_serialize<S: serde::ser::Serializer>(  | 
 | 75 | +        &self,  | 
 | 76 | +        value: &Bound<'_, PyAny>,  | 
 | 77 | +        serializer: S,  | 
 | 78 | +        include: Option<&Bound<'_, PyAny>>,  | 
 | 79 | +        exclude: Option<&Bound<'_, PyAny>>,  | 
 | 80 | +        extra: &Extra,  | 
 | 81 | +    ) -> Result<S::Ok, S::Error> {  | 
 | 82 | +        self.serializer  | 
 | 83 | +            .serde_serialize(value, serializer, include, exclude, extra)  | 
 | 84 | +    }  | 
 | 85 | + | 
 | 86 | +    fn get_name(&self) -> &str {  | 
 | 87 | +        self.serializer.get_name()  | 
 | 88 | +    }  | 
 | 89 | + | 
 | 90 | +    fn retry_with_lax_check(&self) -> bool {  | 
 | 91 | +        self.serializer.retry_with_lax_check()  | 
 | 92 | +    }  | 
 | 93 | +}  | 
0 commit comments