1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use crate::*;
use ErrorMessage;
use YAD;
/// Serializes a [`YAD`] structure into its binary representation.
///
/// # Type Parameters
/// - `Y`: A generic type that implements [`AsRef<YAD>`].
/// This allows passing either a reference to a [`YAD`] instance or any type that
/// can be converted into a reference to [`YAD`] (e.g., `&YAD`, `Box<YAD>`, etc.).
///
/// # Parameters
/// - `yad`: The input value implementing [`AsRef<YAD>`] that will be serialized
/// into a `Vec<u8>`.
///
/// # Returns
/// - `Ok(Vec<u8>)`: A vector of bytes containing the serialized binary data
/// representation of the [`YAD`] structure.
/// - `Err(ErrorMessage)`: An error indicating why serialization failed, such as
/// invalid data, corrupted state, or unsupported structure within the [`YAD`].
///
/// # Errors
/// Returns [`ErrorMessage`] if the serialization process encounters issues, such as:
/// - Encountering invalid or incomplete data inside the [`YAD`] structure.
/// - Inconsistent or unsupported field values.
/// - Any internal logic error preventing a correct binary encoding.
///
/// # Examples
/// ```rust
/// let yad = yad_core::YAD::new();
/// let bytes = yad_core::serialize(&yad)?;
/// assert!(!bytes.is_empty());
/// ```
///
/// # Notes
/// - This function performs a **lossless** serialization: all relevant
/// information stored in the [`YAD`] structure is preserved in the binary output.
/// - The output format follows the internal specification of YAD's binary layout,
/// ensuring compatibility across serialization and deserialization steps.
///
/// # See Also
/// - [`deserialize`] for converting a binary `Vec<u8>` back into a [`YAD`].