use std::fmt;
use serde::{Deserialize, Deserializer, Serialize};
use tabnas::Value;
use crate::error::ProtoError;
use crate::node::nsrc;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
pub enum ProtoVersion {
#[serde(rename = "proto2")]
Proto2,
#[serde(rename = "proto3")]
Proto3,
#[serde(rename = "2023")]
Edition2023,
#[serde(rename = "2024")]
Edition2024,
}
impl ProtoVersion {
pub fn as_str(self) -> &'static str {
match self {
ProtoVersion::Proto2 => "proto2",
ProtoVersion::Proto3 => "proto3",
ProtoVersion::Edition2023 => "2023",
ProtoVersion::Edition2024 => "2024",
}
}
pub fn from_declared(value: &str) -> Option<ProtoVersion> {
match value {
"proto2" => Some(ProtoVersion::Proto2),
"proto3" => Some(ProtoVersion::Proto3),
"2023" => Some(ProtoVersion::Edition2023),
"2024" => Some(ProtoVersion::Edition2024),
_ => None,
}
}
}
impl fmt::Display for ProtoVersion {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for ProtoVersion {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let text = String::deserialize(deserializer)?;
ProtoVersion::from_declared(&text)
.ok_or_else(|| serde::de::Error::custom(format!("proto: unknown version {:?}", text)))
}
}
pub fn is_edition(version: ProtoVersion) -> bool {
matches!(
version,
ProtoVersion::Edition2023 | ProtoVersion::Edition2024
)
}
pub fn edition_enum(version: ProtoVersion) -> String {
format!("EDITION_{}", version.as_str())
}
pub fn declared_version(node: &Value) -> Result<Option<ProtoVersion>, ProtoError> {
declared_version_src(nsrc(node))
}
pub fn declared_version_src(src: &str) -> Result<Option<ProtoVersion>, ProtoError> {
let Some((keyword, rest)) = keyword_of(src) else {
return Ok(None);
};
let Some(rest) = rest.strip_prefix('=') else {
return Ok(None);
};
let mut chars = rest.char_indices();
match chars.next() {
Some((_, '"' | '\'')) => {}
_ => return Ok(None),
}
let body = &rest[1..];
let Some(end) = body.find(['"', '\'']) else {
return Ok(None);
};
if 0 == end {
return Ok(None);
}
let value = &body[..end];
match ProtoVersion::from_declared(value) {
Some(version) => Ok(Some(version)),
None => Err(ProtoError::Version(format!(
"proto: unknown {keyword} version \"{value}\""
))),
}
}
fn keyword_of(src: &str) -> Option<(&'static str, &str)> {
if let Some(rest) = src.strip_prefix("syntax") {
return Some(("syntax", rest));
}
src.strip_prefix("edition").map(|rest| ("edition", rest))
}
pub fn resolve_version(
declared: Option<ProtoVersion>,
option: Option<ProtoVersion>,
reconcile: bool,
) -> Result<ProtoVersion, ProtoError> {
if let (Some(declared), Some(option)) = (declared, option) {
if declared != option {
if reconcile {
return Err(ProtoError::Version(format!(
"proto: version mismatch \u{2014} option \"{option}\" but the file \
declares \"{declared}\". Set reconcile:false to let the file win."
)));
}
return Ok(declared);
}
}
Ok(declared.or(option).unwrap_or(ProtoVersion::Proto2))
}