| 38 | const MAX_DEPTH: usize = 200; |
| 39 | |
| 40 | pub fn serialize(value: &Handle, opts: Options) -> Result<String> { |
| 41 | // `cls` short-circuits the whole walk: instantiate and delegate to its `.encode(value)`. |
| 42 | if let Some(cls) = &opts.cls { |
| 43 | let encoder = cls.call("__call__", &[])?; |
| 44 | let result = encoder.call("encode", &[value.raw()])?; |
| 45 | return match decode(result.raw())? { |
| 46 | Value::Bytes(b) => String::from_utf8(b).map_err(|e| Error::Value(format!("cls.encode produced non-UTF-8: {}", e))), |
| 47 | _ => Err(Error::Type("cls.encode must return str".into())), |
| 48 | }; |
| 49 | } |
| 50 | let mut out = String::new(); |
| 51 | let mut ctx = SerCtx { opts: &opts, depth: 0 }; |
| 52 | serialize_into(value, &mut out, &mut ctx, 0)?; |
| 53 | Ok(out) |
| 54 | } |
| 55 | |
| 56 | fn serialize_into(value: &Handle, out: &mut String, ctx: &mut SerCtx, depth: usize) -> Result<()> { |
| 57 | let ty_handle = value.type_of()?; |