Skip to main content

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, storing Postgres's `numeric`
81/// text (`-5000.0000`, `NaN`) as `Value::Text`.
82#[derive(Debug, Clone, Copy, Default)]
83pub struct DecimalTextDecoder;
84
85/// Decoder for `timestamp` (without time zone), storing
86/// `YYYY-MM-DD HH:MM:SS.ffffff` as `Value::Text`.
87#[derive(Debug, Clone, Copy, Default)]
88pub struct TimestampDecoder;
89
90/// Decoder for `timestamptz`, storing the instant in UTC as
91/// `YYYY-MM-DD HH:MM:SS.ffffff+00:00`, which orders as text and which
92/// SQLite's date functions read.
93#[derive(Debug, Clone, Copy, Default)]
94pub struct TimestampTzDecoder;
95
96/// Decoder for `date`, storing `YYYY-MM-DD` as `Value::Text`.
97#[derive(Debug, Clone, Copy, Default)]
98pub struct DateDecoder;
99
100/// Decoder for `time`, storing `HH:MM:SS.ffffff`, and for `timetz`,
101/// storing the UTC time `HH:MM:SS.ffffff+00:00`.
102#[derive(Debug, Clone, Copy, Default)]
103pub struct TimeDecoder;
104
105/// Decoder for `interval`, storing the ISO 8601 duration Postgres prints
106/// under `IntervalStyle = iso_8601`, such as `P1Y2M3DT4H5M6.789S`.
107#[derive(Debug, Clone, Copy, Default)]
108pub struct IntervalDecoder;
109
110/// Decoder for `json`/`jsonb`. Preserves the wire form verbatim as
111/// `Value::Text`.
112#[derive(Debug, Clone, Copy, Default)]
113pub struct JsonVerbatimDecoder;
114
115/// Decoder for `json`/`jsonb` that canonicalizes into sorted-key compact
116/// JSON text.
117#[derive(Debug, Clone, Copy, Default)]
118pub struct JsonCanonicalDecoder;