use indexmap::IndexMap;
use serde::{Serialize, Serializer};
use serde_json::value::RawValue;
use crate::jsnum::js_number_to_string;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum FieldLabel {
#[serde(rename = "LABEL_OPTIONAL")]
Optional,
#[serde(rename = "LABEL_REQUIRED")]
Required,
#[serde(rename = "LABEL_REPEATED")]
Repeated,
}
impl FieldLabel {
pub fn as_str(self) -> &'static str {
match self {
FieldLabel::Optional => "LABEL_OPTIONAL",
FieldLabel::Required => "LABEL_REQUIRED",
FieldLabel::Repeated => "LABEL_REPEATED",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum FieldType {
#[serde(rename = "TYPE_DOUBLE")]
Double,
#[serde(rename = "TYPE_FLOAT")]
Float,
#[serde(rename = "TYPE_INT64")]
Int64,
#[serde(rename = "TYPE_UINT64")]
Uint64,
#[serde(rename = "TYPE_INT32")]
Int32,
#[serde(rename = "TYPE_FIXED64")]
Fixed64,
#[serde(rename = "TYPE_FIXED32")]
Fixed32,
#[serde(rename = "TYPE_BOOL")]
Bool,
#[serde(rename = "TYPE_STRING")]
String,
#[serde(rename = "TYPE_GROUP")]
Group,
#[serde(rename = "TYPE_MESSAGE")]
Message,
#[serde(rename = "TYPE_BYTES")]
Bytes,
#[serde(rename = "TYPE_UINT32")]
Uint32,
#[serde(rename = "TYPE_ENUM")]
Enum,
#[serde(rename = "TYPE_SFIXED32")]
Sfixed32,
#[serde(rename = "TYPE_SFIXED64")]
Sfixed64,
#[serde(rename = "TYPE_SINT32")]
Sint32,
#[serde(rename = "TYPE_SINT64")]
Sint64,
}
impl FieldType {
pub fn as_str(self) -> &'static str {
match self {
FieldType::Double => "TYPE_DOUBLE",
FieldType::Float => "TYPE_FLOAT",
FieldType::Int64 => "TYPE_INT64",
FieldType::Uint64 => "TYPE_UINT64",
FieldType::Int32 => "TYPE_INT32",
FieldType::Fixed64 => "TYPE_FIXED64",
FieldType::Fixed32 => "TYPE_FIXED32",
FieldType::Bool => "TYPE_BOOL",
FieldType::String => "TYPE_STRING",
FieldType::Group => "TYPE_GROUP",
FieldType::Message => "TYPE_MESSAGE",
FieldType::Bytes => "TYPE_BYTES",
FieldType::Uint32 => "TYPE_UINT32",
FieldType::Enum => "TYPE_ENUM",
FieldType::Sfixed32 => "TYPE_SFIXED32",
FieldType::Sfixed64 => "TYPE_SFIXED64",
FieldType::Sint32 => "TYPE_SINT32",
FieldType::Sint64 => "TYPE_SINT64",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum SymbolVisibility {
#[serde(rename = "VISIBILITY_EXPORT")]
Export,
#[serde(rename = "VISIBILITY_LOCAL")]
Local,
}
impl SymbolVisibility {
pub fn as_str(self) -> &'static str {
match self {
SymbolVisibility::Export => "VISIBILITY_EXPORT",
SymbolVisibility::Local => "VISIBILITY_LOCAL",
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub enum OptionValue {
Bool(bool),
Number(#[serde(serialize_with = "json_number")] f64),
Str(String),
}
pub type Options = IndexMap<String, OptionValue>;
pub(crate) fn json_number<S: Serializer>(value: &f64, serializer: S) -> Result<S::Ok, S::Error> {
if !value.is_finite() {
return serializer.serialize_none();
}
let text = js_number_to_string(*value);
let raw = RawValue::from_string(text).map_err(serde::ser::Error::custom)?;
raw.serialize(serializer)
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct DescriptorRange {
#[serde(serialize_with = "json_number")]
pub start: f64,
#[serde(serialize_with = "json_number")]
pub end: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
impl DescriptorRange {
pub fn new(start: f64, end: f64) -> Self {
DescriptorRange {
start,
end,
options: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct FieldDescriptorProto {
pub name: String,
#[serde(serialize_with = "json_number")]
pub number: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub label: Option<FieldLabel>,
#[serde(skip_serializing_if = "Option::is_none")]
pub r#type: Option<FieldType>,
#[serde(skip_serializing_if = "Option::is_none")]
pub type_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extendee: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub json_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub default_value: Option<String>,
#[serde(skip_serializing_if = "is_false")]
pub proto3_optional: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub oneof_index: Option<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
fn is_false(value: &bool) -> bool {
!*value
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct EnumValueDescriptorProto {
pub name: String,
#[serde(serialize_with = "json_number")]
pub number: f64,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct EnumDescriptorProto {
pub name: String,
pub value: Vec<EnumValueDescriptorProto>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reserved_range: Option<Vec<DescriptorRange>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reserved_name: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub visibility: Option<SymbolVisibility>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
impl EnumDescriptorProto {
pub fn new(name: impl Into<String>) -> Self {
EnumDescriptorProto {
name: name.into(),
value: Vec::new(),
reserved_range: None,
reserved_name: None,
visibility: None,
options: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct OneofDescriptorProto {
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct DescriptorProto {
pub name: String,
pub field: Vec<FieldDescriptorProto>,
pub nested_type: Vec<DescriptorProto>,
pub enum_type: Vec<EnumDescriptorProto>,
pub oneof_decl: Vec<OneofDescriptorProto>,
pub extension: Vec<FieldDescriptorProto>,
#[serde(skip_serializing_if = "Option::is_none")]
pub extension_range: Option<Vec<DescriptorRange>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reserved_range: Option<Vec<DescriptorRange>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub reserved_name: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub visibility: Option<SymbolVisibility>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
impl DescriptorProto {
pub fn new(name: impl Into<String>) -> Self {
DescriptorProto {
name: name.into(),
field: Vec::new(),
nested_type: Vec::new(),
enum_type: Vec::new(),
oneof_decl: Vec::new(),
extension: Vec::new(),
extension_range: None,
reserved_range: None,
reserved_name: None,
visibility: None,
options: None,
}
}
}
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct MethodDescriptorProto {
pub name: String,
pub input_type: String,
pub output_type: String,
#[serde(skip_serializing_if = "is_false")]
pub client_streaming: bool,
#[serde(skip_serializing_if = "is_false")]
pub server_streaming: bool,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
#[derive(Debug, Clone, PartialEq, Serialize)]
pub struct ServiceDescriptorProto {
pub name: String,
pub method: Vec<MethodDescriptorProto>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Default)]
#[serde(rename_all = "camelCase")]
pub struct FileDescriptorProto {
#[serde(skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub package: Option<String>,
pub dependency: Vec<String>,
pub public_dependency: Vec<usize>,
pub weak_dependency: Vec<usize>,
#[serde(skip_serializing_if = "Option::is_none")]
pub option_dependency: Option<Vec<String>>,
pub message_type: Vec<DescriptorProto>,
pub enum_type: Vec<EnumDescriptorProto>,
pub service: Vec<ServiceDescriptorProto>,
pub extension: Vec<FieldDescriptorProto>,
#[serde(skip_serializing_if = "Option::is_none")]
pub options: Option<Options>,
#[serde(skip_serializing_if = "Option::is_none")]
pub syntax: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub edition: Option<String>,
}
pub const MAX_FIELD_NUMBER_END: f64 = 536_870_912.0;
pub const MAX_MESSAGE_SET_END: f64 = 2_147_483_647.0;
pub const MAX_ENUM_NUMBER: f64 = 2_147_483_647.0;
pub const SCALAR_TYPES: &[(&str, FieldType)] = &[
("double", FieldType::Double),
("float", FieldType::Float),
("int32", FieldType::Int32),
("int64", FieldType::Int64),
("uint32", FieldType::Uint32),
("uint64", FieldType::Uint64),
("sint32", FieldType::Sint32),
("sint64", FieldType::Sint64),
("fixed32", FieldType::Fixed32),
("fixed64", FieldType::Fixed64),
("sfixed32", FieldType::Sfixed32),
("sfixed64", FieldType::Sfixed64),
("bool", FieldType::Bool),
("string", FieldType::String),
("bytes", FieldType::Bytes),
];
pub fn scalar_type(name: &str) -> Option<FieldType> {
SCALAR_TYPES
.iter()
.find(|(scalar, _)| *scalar == name)
.map(|(_, field_type)| *field_type)
}