dbcli 0.1.0

Convert SQL query results to JSON without struct mapping, supporting MySQL/PostgreSQL/SQLite/Odbc
Documentation
use encoding_rs::{BIG5, GBK, UTF_8};

/// Automatically detects the encoding and decodes a byte sequence into a UTF-8 string.
///
/// Tries UTF-8, GBK, and BIG5 encodings in order. If the decoded result contains the
/// Unicode replacement character (`\u{FFFD}`), the current encoding is considered a
/// mismatch and the next one is tried.
///
/// Returns an empty string `""` if none of the three encodings can decode losslessly.
///
/// # Use Cases
///
/// Primarily used for MySQL `VARBINARY` and `TEXT` (non-UTF-8 byte streams) as well as
/// PostgreSQL `bytea` columns, called after [`crate::to_json::blob_is_text`] determines
/// that the data is human-readable text.
///
/// # Example
///
/// ```rust
/// use dbcli::decode::decode_auto;
///
/// // UTF-8 bytes
/// let bytes = b"Hello, \xe4\xb8\x96\xe7\x95\x8c";
/// let s = decode_auto(bytes);
/// assert_eq!(s, "Hello, \u{4e16}\u{754c}");
/// ```
pub fn decode_auto(bytes: &[u8]) -> String {
    let decoders = vec![UTF_8, GBK, BIG5];
    for encoding in decoders {
        let (decoded, _, _) = encoding.decode(bytes);
        if !decoded.contains('\u{fffd}') {
            return decoded.into_owned();
        }
    }
    String::new()
}