erltf_serde/
lib.rs

1// Copyright (C) 2025-2026 Michael S. Klishin and Contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15mod de;
16pub mod elixir;
17mod error;
18mod ser;
19
20pub use de::{Deserializer, ProplistDeserializer, from_bytes, from_proplist, from_term};
21pub use erltf_serde_derive::ElixirStruct;
22pub use error::{Error, Result};
23pub use ser::{Serializer, to_bytes, to_term};
24
25use erltf::OwnedTerm;
26use serde::de::DeserializeOwned;
27
28pub trait OwnedTermExt {
29    fn try_deserialize<T: DeserializeOwned>(&self) -> Result<T>;
30
31    fn try_deserialize_proplist<T: DeserializeOwned>(&self) -> Result<T>;
32}
33
34impl OwnedTermExt for OwnedTerm {
35    fn try_deserialize<T: DeserializeOwned>(&self) -> Result<T> {
36        let normalized = self
37            .to_map_recursive()
38            .map_err(|e| Error::Message(e.to_string()))?;
39        from_term(&normalized)
40    }
41
42    fn try_deserialize_proplist<T: DeserializeOwned>(&self) -> Result<T> {
43        from_proplist(self)
44    }
45}