tabnas_proto/
detect_version.rs1use std::fmt;
8
9use serde::{Deserialize, Deserializer, Serialize};
10use tabnas::Value;
11
12use crate::error::ProtoError;
13use crate::node::nsrc;
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize)]
21pub enum ProtoVersion {
22 #[serde(rename = "proto2")]
23 Proto2,
24 #[serde(rename = "proto3")]
25 Proto3,
26 #[serde(rename = "2023")]
27 Edition2023,
28 #[serde(rename = "2024")]
29 Edition2024,
30}
31
32impl ProtoVersion {
33 pub fn as_str(self) -> &'static str {
35 match self {
36 ProtoVersion::Proto2 => "proto2",
37 ProtoVersion::Proto3 => "proto3",
38 ProtoVersion::Edition2023 => "2023",
39 ProtoVersion::Edition2024 => "2024",
40 }
41 }
42
43 pub fn from_declared(value: &str) -> Option<ProtoVersion> {
46 match value {
47 "proto2" => Some(ProtoVersion::Proto2),
48 "proto3" => Some(ProtoVersion::Proto3),
49 "2023" => Some(ProtoVersion::Edition2023),
50 "2024" => Some(ProtoVersion::Edition2024),
51 _ => None,
52 }
53 }
54}
55
56impl fmt::Display for ProtoVersion {
57 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
58 formatter.write_str(self.as_str())
59 }
60}
61
62impl<'de> Deserialize<'de> for ProtoVersion {
63 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
64 let text = String::deserialize(deserializer)?;
65 ProtoVersion::from_declared(&text)
66 .ok_or_else(|| serde::de::Error::custom(format!("proto: unknown version {:?}", text)))
67 }
68}
69
70pub fn is_edition(version: ProtoVersion) -> bool {
72 matches!(
73 version,
74 ProtoVersion::Edition2023 | ProtoVersion::Edition2024
75 )
76}
77
78pub fn edition_enum(version: ProtoVersion) -> String {
84 format!("EDITION_{}", version.as_str())
85}
86
87pub fn declared_version(node: &Value) -> Result<Option<ProtoVersion>, ProtoError> {
93 declared_version_src(nsrc(node))
94}
95
96pub fn declared_version_src(src: &str) -> Result<Option<ProtoVersion>, ProtoError> {
101 let Some((keyword, rest)) = keyword_of(src) else {
105 return Ok(None);
106 };
107 let Some(rest) = rest.strip_prefix('=') else {
108 return Ok(None);
109 };
110 let mut chars = rest.char_indices();
111 match chars.next() {
112 Some((_, '"' | '\'')) => {}
113 _ => return Ok(None),
114 }
115 let body = &rest[1..];
120 let Some(end) = body.find(['"', '\'']) else {
121 return Ok(None);
122 };
123 if 0 == end {
124 return Ok(None);
125 }
126 let value = &body[..end];
127
128 match ProtoVersion::from_declared(value) {
129 Some(version) => Ok(Some(version)),
130 None => Err(ProtoError::Version(format!(
131 "proto: unknown {keyword} version \"{value}\""
132 ))),
133 }
134}
135
136fn keyword_of(src: &str) -> Option<(&'static str, &str)> {
138 if let Some(rest) = src.strip_prefix("syntax") {
139 return Some(("syntax", rest));
140 }
141 src.strip_prefix("edition").map(|rest| ("edition", rest))
142}
143
144pub fn resolve_version(
151 declared: Option<ProtoVersion>,
152 option: Option<ProtoVersion>,
153 reconcile: bool,
154) -> Result<ProtoVersion, ProtoError> {
155 if let (Some(declared), Some(option)) = (declared, option) {
156 if declared != option {
157 if reconcile {
158 return Err(ProtoError::Version(format!(
159 "proto: version mismatch \u{2014} option \"{option}\" but the file \
160 declares \"{declared}\". Set reconcile:false to let the file win."
161 )));
162 }
163 return Ok(declared);
164 }
165 }
166 Ok(declared.or(option).unwrap_or(ProtoVersion::Proto2))
167}