#![forbid(unsafe_code)]
use crate::parser::span::Span;
#[derive(Debug, Clone, PartialEq)]
pub enum Edition {
Edition2023,
Unknown(String),
}
impl Edition {
pub fn parse(s: &str) -> Self {
match s {
"2023" => Edition::Edition2023,
other => Edition::Unknown(other.to_owned()),
}
}
pub fn syntax_sentinel() -> &'static str {
"editions"
}
}
#[derive(Debug, Clone, PartialEq, Default)]
pub struct ProtoFile {
pub syntax: Option<String>,
pub edition: Option<Edition>,
pub package: Option<String>,
pub imports: Vec<Import>,
pub options: Vec<ProtoOption>,
pub messages: Vec<Message>,
pub enums: Vec<Enum>,
pub services: Vec<Service>,
pub extends: Vec<ExtendBlock>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Import {
pub path: String,
pub modifier: ImportModifier,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ImportModifier {
None,
Public,
Weak,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Message {
pub name: String,
pub fields: Vec<Field>,
pub oneofs: Vec<Oneof>,
pub nested_messages: Vec<Message>,
pub nested_enums: Vec<Enum>,
pub reserved: Vec<Reserved>,
pub options: Vec<ProtoOption>,
pub extensions: Vec<ExtensionRange>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Field {
pub label: FieldLabel,
pub ty: FieldType,
pub name: String,
pub number: i32,
pub options: Vec<ProtoOption>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FieldLabel {
Singular,
Optional,
Repeated,
Required,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExtensionRange {
pub start: u32,
pub end: Option<u32>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ExtendBlock {
pub extendee: String,
pub fields: Vec<Field>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FieldType {
Scalar(ScalarType),
Map {
key: ScalarType,
value: Box<FieldType>,
},
Named(String),
Group(String),
}
#[derive(Debug, Clone, PartialEq, Copy)]
pub enum ScalarType {
Double,
Float,
Int32,
Int64,
Uint32,
Uint64,
Sint32,
Sint64,
Fixed32,
Fixed64,
Sfixed32,
Sfixed64,
Bool,
String,
Bytes,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Oneof {
pub name: String,
pub fields: Vec<Field>,
pub options: Vec<ProtoOption>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Enum {
pub name: String,
pub values: Vec<EnumValue>,
pub reserved: Vec<Reserved>,
pub options: Vec<ProtoOption>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct EnumValue {
pub name: String,
pub number: i32,
pub options: Vec<ProtoOption>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Service {
pub name: String,
pub methods: Vec<Method>,
pub options: Vec<ProtoOption>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Method {
pub name: String,
pub input_type: String,
pub output_type: String,
pub client_streaming: bool,
pub server_streaming: bool,
pub options: Vec<ProtoOption>,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub struct ProtoOption {
pub name: String,
pub value: OptionValue,
pub span: Span,
}
#[derive(Debug, Clone, PartialEq)]
pub enum OptionValue {
Ident(String),
Str(String),
Int(i64),
Float(f64),
Bool(bool),
MessageLiteral(Vec<(String, OptionValue)>),
}
#[derive(Debug, Clone, PartialEq)]
pub enum Reserved {
Ranges(Vec<ReservedRange>),
Names(Vec<String>),
}
#[derive(Debug, Clone, PartialEq)]
pub struct ReservedRange {
pub from: i32,
pub to: ReservedRangeTo,
}
#[derive(Debug, Clone, PartialEq)]
pub enum ReservedRangeTo {
Number(i32),
Max,
}