Skip to main content

mysql_replication/
version.rs

1//! Shared numeric version parsing for protocol producers and CDC adapters.
2use crate::{Result, error::invalid};
3
4/// Parses three numeric components, optionally followed by a nonempty `-` or `+`
5/// suffix. This validates syntax only; consumers select supported vendors and
6/// series separately, including custom CDC adapters.
7pub fn parse_server_version(raw: &str) -> Result<(u32, u32, u32)> {
8    if raw.chars().any(|c| c.is_whitespace() || c.is_control()) {
9        return Err(invalid("invalid server version"));
10    }
11    let core = if let Some((core, suffix)) = raw.split_once(['-', '+']) {
12        if suffix.is_empty() {
13            return Err(invalid("empty server version suffix"));
14        }
15        core
16    } else {
17        raw
18    };
19    let mut parts = core.split('.');
20    let mut number = || -> Result<u32> {
21        let part = parts
22            .next()
23            .ok_or_else(|| invalid("incomplete server version"))?;
24        if part.is_empty() || !part.bytes().all(|b| b.is_ascii_digit()) {
25            return Err(invalid("invalid server version component"));
26        }
27        part.parse()
28            .map_err(|_| invalid("server version component overflows u32"))
29    };
30    let version = (number()?, number()?, number()?);
31    if parts.next().is_some() {
32        return Err(invalid("extra server version component"));
33    }
34    Ok(version)
35}