sqlite_diff_rs/wire/decoder.rs
1//! [`Decoder`]: per-(source, semantic) conversion from a payload into
2//! [`Value`](crate::encoding::Value), plus every built-in decoder unit
3//! type the crate ships.
4
5use super::error::DecodeError;
6use super::source::WireSource;
7use crate::encoding::Value;
8
9/// Converts a per-column payload into a [`Value`].
10///
11/// Zero-sized unit-struct implementations (the crate's built-ins) cover
12/// the common cases. User-defined stateful decoders carry their own
13/// fields (lookup tables, compiled regexes, per-adapter configuration)
14/// and implement the same trait shape.
15pub trait Decoder<Src: WireSource, S, B> {
16 /// Decode one payload.
17 ///
18 /// # Errors
19 ///
20 /// Returns a [`DecodeError`] variant describing the failure. Every
21 /// variant carries the offending column name so the outer
22 /// [`WireAdapter`](super::adapter::WireAdapter) impl does not have to wrap.
23 fn decode(&self, payload: Src::Payload<'_>) -> Result<Value<S, B>, DecodeError>;
24}
25
26// ------------------------------------------------------------------
27// Built-in decoder unit types. Every one is a zero-sized marker whose
28// `impl Decoder<Src, S, B>` for each supported `Src` lives in the format
29// module (`pg_walstream`, `wal2json`, `maxwell`).
30// ------------------------------------------------------------------
31
32/// Decoder producing `Value::Null` regardless of the payload's non-null
33/// contents. Registered internally when the payload's null discriminator
34/// is set. Users normally do not register this directly.
35#[derive(Debug, Clone, Copy, Default)]
36pub struct NullDecoder;
37
38/// Decoder for boolean columns.
39#[derive(Debug, Clone, Copy, Default)]
40pub struct BoolDecoder;
41
42/// Decoder for integer columns fitting in `i64`.
43#[derive(Debug, Clone, Copy, Default)]
44pub struct IntDecoder;
45
46/// Decoder for integer columns whose wire values may exceed `i64`. Values
47/// fitting `i64` produce `Value::Integer`, values above produce
48/// `Value::Text` with the base-10 digits preserved.
49#[derive(Debug, Clone, Copy, Default)]
50pub struct Int64OverflowToTextDecoder;
51
52/// Decoder for floating-point columns.
53#[derive(Debug, Clone, Copy, Default)]
54pub struct RealDecoder;
55
56/// Decoder for text-affinity columns.
57#[derive(Debug, Clone, Copy, Default)]
58pub struct TextDecoder;
59
60/// Decoder for pg_walstream binary-mode BYTEA.
61#[derive(Debug, Clone, Copy, Default)]
62pub struct PgByteaBinaryDecoder;
63
64/// Decoder for pg_walstream text-mode BYTEA (`\xHEX` form).
65#[derive(Debug, Clone, Copy, Default)]
66pub struct PgByteaTextModeDecoder;
67
68/// Decoder for MySQL binary-family columns (base64 on the wire).
69#[derive(Debug, Clone, Copy, Default)]
70pub struct MySqlBinaryDecoder;
71
72/// Decoder rendering UUID wire text into a 16-byte `Value::Blob`.
73#[derive(Debug, Clone, Copy, Default)]
74pub struct UuidBlob16Decoder;
75
76/// Decoder rendering UUID wire text into a verbatim `Value::Text(36)`.
77#[derive(Debug, Clone, Copy, Default)]
78pub struct UuidText36Decoder;
79
80/// Decoder for `numeric`/`decimal` columns, preserving precision as
81/// `Value::Text`.
82#[derive(Debug, Clone, Copy, Default)]
83pub struct DecimalTextDecoder;
84
85/// Decoder for `timestamp` (without time zone). Preserves the wire text
86/// verbatim.
87#[derive(Debug, Clone, Copy, Default)]
88pub struct TimestampVerbatimDecoder;
89
90/// Decoder for `timestamptz`. Preserves the wire text verbatim.
91#[derive(Debug, Clone, Copy, Default)]
92pub struct TimestampTzVerbatimDecoder;
93
94/// Decoder for `date`. Preserves the wire text verbatim.
95#[derive(Debug, Clone, Copy, Default)]
96pub struct DateVerbatimDecoder;
97
98/// Decoder for `time`/`timetz`. Preserves the wire text verbatim.
99#[derive(Debug, Clone, Copy, Default)]
100pub struct TimeVerbatimDecoder;
101
102/// Decoder for `interval`. Preserves the wire text verbatim.
103#[derive(Debug, Clone, Copy, Default)]
104pub struct IntervalVerbatimDecoder;
105
106/// Decoder for `json`/`jsonb`. Preserves the wire form verbatim as
107/// `Value::Text`.
108#[derive(Debug, Clone, Copy, Default)]
109pub struct JsonVerbatimDecoder;
110
111/// Decoder for `json`/`jsonb` that canonicalizes into sorted-key compact
112/// JSON text.
113#[derive(Debug, Clone, Copy, Default)]
114pub struct JsonCanonicalDecoder;