-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Vendor in the protobuf metadata types
We need these to extend on them while https://github.com/danburkert/prost/pull/317 remains open or something like it
- Loading branch information
Tom Burdick
committed
Sep 22, 2020
1 parent
303eddf
commit 1050f13
Showing
12 changed files
with
2,132 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[package] | ||
name = "proto-types" | ||
version = "0.6.1" | ||
authors = ["Dan Burkert <dan@danburkert.com>"] | ||
license = "Apache-2.0" | ||
repository = "https://github.com/danburkert/prost" | ||
documentation = "https://docs.rs/prost-types" | ||
readme = "README.md" | ||
description = "A Protocol Buffers implementation for the Rust Language." | ||
edition = "2018" | ||
|
||
[lib] | ||
doctest = false | ||
test = false | ||
|
||
[dependencies] | ||
bytes = "0.5" | ||
prost = "0.6.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[](https://docs.rs/prost-types/) | ||
[](https://crates.io/crates/prost-types) | ||
|
||
# `prost-types` | ||
|
||
Prost definitions of Protocol Buffers well known types. See the [Protobuf reference][1] for more | ||
information about well known types. | ||
|
||
[1]: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf | ||
|
||
## License | ||
|
||
`prost-types` is distributed under the terms of the Apache License (Version 2.0). | ||
`prost-types` includes code imported from the Protocol Buffers projet, which is | ||
included under its original ([BSD][2]) license. | ||
|
||
[2]: https://github.com/google/protobuf/blob/master/LICENSE | ||
|
||
See [LICENSE](..LICENSE) for details. | ||
|
||
Copyright 2017 Dan Burkert |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/// The version number of protocol compiler. | ||
#[derive(Clone, PartialEq, ::prost::Message)] | ||
pub struct Version { | ||
#[prost(int32, optional, tag="1")] | ||
pub major: ::std::option::Option<i32>, | ||
#[prost(int32, optional, tag="2")] | ||
pub minor: ::std::option::Option<i32>, | ||
#[prost(int32, optional, tag="3")] | ||
pub patch: ::std::option::Option<i32>, | ||
/// A suffix for alpha, beta or rc release, e.g., "alpha-1", "rc2". It should | ||
/// be empty for mainline stable releases. | ||
#[prost(string, optional, tag="4")] | ||
pub suffix: ::std::option::Option<std::string::String>, | ||
} | ||
/// An encoded CodeGeneratorRequest is written to the plugin's stdin. | ||
#[derive(Clone, PartialEq, ::prost::Message)] | ||
pub struct CodeGeneratorRequest { | ||
/// The .proto files that were explicitly listed on the command-line. The | ||
/// code generator should generate code only for these files. Each file's | ||
/// descriptor will be included in proto_file, below. | ||
#[prost(string, repeated, tag="1")] | ||
pub file_to_generate: ::std::vec::Vec<std::string::String>, | ||
/// The generator parameter passed on the command-line. | ||
#[prost(string, optional, tag="2")] | ||
pub parameter: ::std::option::Option<std::string::String>, | ||
/// FileDescriptorProtos for all files in files_to_generate and everything | ||
/// they import. The files will appear in topological order, so each file | ||
/// appears before any file that imports it. | ||
/// | ||
/// protoc guarantees that all proto_files will be written after | ||
/// the fields above, even though this is not technically guaranteed by the | ||
/// protobuf wire format. This theoretically could allow a plugin to stream | ||
/// in the FileDescriptorProtos and handle them one by one rather than read | ||
/// the entire set into memory at once. However, as of this writing, this | ||
/// is not similarly optimized on protoc's end -- it will store all fields in | ||
/// memory at once before sending them to the plugin. | ||
/// | ||
/// Type names of fields and extensions in the FileDescriptorProto are always | ||
/// fully qualified. | ||
#[prost(message, repeated, tag="15")] | ||
pub proto_file: ::std::vec::Vec<super::FileDescriptorProto>, | ||
/// The version number of protocol compiler. | ||
#[prost(message, optional, tag="3")] | ||
pub compiler_version: ::std::option::Option<Version>, | ||
} | ||
/// The plugin writes an encoded CodeGeneratorResponse to stdout. | ||
#[derive(Clone, PartialEq, ::prost::Message)] | ||
pub struct CodeGeneratorResponse { | ||
/// Error message. If non-empty, code generation failed. The plugin process | ||
/// should exit with status code zero even if it reports an error in this way. | ||
/// | ||
/// This should be used to indicate errors in .proto files which prevent the | ||
/// code generator from generating correct code. Errors which indicate a | ||
/// problem in protoc itself -- such as the input CodeGeneratorRequest being | ||
/// unparseable -- should be reported by writing a message to stderr and | ||
/// exiting with a non-zero status code. | ||
#[prost(string, optional, tag="1")] | ||
pub error: ::std::option::Option<std::string::String>, | ||
#[prost(message, repeated, tag="15")] | ||
pub file: ::std::vec::Vec<code_generator_response::File>, | ||
} | ||
pub mod code_generator_response { | ||
/// Represents a single generated file. | ||
#[derive(Clone, PartialEq, ::prost::Message)] | ||
pub struct File { | ||
/// The file name, relative to the output directory. The name must not | ||
/// contain "." or ".." components and must be relative, not be absolute (so, | ||
/// the file cannot lie outside the output directory). "/" must be used as | ||
/// the path separator, not "\". | ||
/// | ||
/// If the name is omitted, the content will be appended to the previous | ||
/// file. This allows the generator to break large files into small chunks, | ||
/// and allows the generated text to be streamed back to protoc so that large | ||
/// files need not reside completely in memory at one time. Note that as of | ||
/// this writing protoc does not optimize for this -- it will read the entire | ||
/// CodeGeneratorResponse before writing files to disk. | ||
#[prost(string, optional, tag="1")] | ||
pub name: ::std::option::Option<std::string::String>, | ||
/// If non-empty, indicates that the named file should already exist, and the | ||
/// content here is to be inserted into that file at a defined insertion | ||
/// point. This feature allows a code generator to extend the output | ||
/// produced by another code generator. The original generator may provide | ||
/// insertion points by placing special annotations in the file that look | ||
/// like: | ||
/// @@protoc_insertion_point(NAME) | ||
/// The annotation can have arbitrary text before and after it on the line, | ||
/// which allows it to be placed in a comment. NAME should be replaced with | ||
/// an identifier naming the point -- this is what other generators will use | ||
/// as the insertion_point. Code inserted at this point will be placed | ||
/// immediately above the line containing the insertion point (thus multiple | ||
/// insertions to the same point will come out in the order they were added). | ||
/// The double-@ is intended to make it unlikely that the generated code | ||
/// could contain things that look like insertion points by accident. | ||
/// | ||
/// For example, the C++ code generator places the following line in the | ||
/// .pb.h files that it generates: | ||
/// // @@protoc_insertion_point(namespace_scope) | ||
/// This line appears within the scope of the file's package namespace, but | ||
/// outside of any particular class. Another plugin can then specify the | ||
/// insertion_point "namespace_scope" to generate additional classes or | ||
/// other declarations that should be placed in this scope. | ||
/// | ||
/// Note that if the line containing the insertion point begins with | ||
/// whitespace, the same whitespace will be added to every line of the | ||
/// inserted text. This is useful for languages like Python, where | ||
/// indentation matters. In these languages, the insertion point comment | ||
/// should be indented the same amount as any inserted code will need to be | ||
/// in order to work correctly in that context. | ||
/// | ||
/// The code generator that generates the initial file and the one which | ||
/// inserts into it must both run as part of a single invocation of protoc. | ||
/// Code generators are executed in the order in which they appear on the | ||
/// command line. | ||
/// | ||
/// If |insertion_point| is present, |name| must also be present. | ||
#[prost(string, optional, tag="2")] | ||
pub insertion_point: ::std::option::Option<std::string::String>, | ||
/// The file contents. | ||
#[prost(string, optional, tag="15")] | ||
pub content: ::std::option::Option<std::string::String>, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
#![doc(html_root_url = "https://docs.rs/prost-types/0.6.1")] | ||
|
||
//! Protocol Buffers well-known types. | ||
//! | ||
//! Note that the documentation for the types defined in this crate are generated from the Protobuf | ||
//! definitions, so code examples are not in Rust. | ||
//! | ||
//! See the [Protobuf reference][1] for more information about well-known types. | ||
//! | ||
//! [1]: https://developers.google.com/protocol-buffers/docs/reference/google.protobuf | ||
use std::convert::TryFrom; | ||
use std::i32; | ||
use std::i64; | ||
use std::time; | ||
|
||
include!("protobuf.rs"); | ||
pub mod compiler { | ||
include!("compiler.rs"); | ||
} | ||
|
||
// The Protobuf `Duration` and `Timestamp` types can't delegate to the standard library equivalents | ||
// because the Protobuf versions are signed. To make them easier to work with, `From` conversions | ||
// are defined in both directions. | ||
|
||
const NANOS_PER_SECOND: i32 = 1_000_000_000; | ||
|
||
impl Duration { | ||
/// Normalizes the duration to a canonical format. | ||
/// | ||
/// Based on [`google::protobuf::util::CreateNormalized`][1]. | ||
/// [1]: https://github.com/google/protobuf/blob/v3.3.2/src/google/protobuf/util/time_util.cc#L79-L100 | ||
fn normalize(&mut self) { | ||
// Make sure nanos is in the range. | ||
if self.nanos <= -NANOS_PER_SECOND || self.nanos >= NANOS_PER_SECOND { | ||
self.seconds += (self.nanos / NANOS_PER_SECOND) as i64; | ||
self.nanos %= NANOS_PER_SECOND; | ||
} | ||
|
||
// nanos should have the same sign as seconds. | ||
if self.seconds < 0 && self.nanos > 0 { | ||
self.seconds += 1; | ||
self.nanos -= NANOS_PER_SECOND; | ||
} else if self.seconds > 0 && self.nanos < 0 { | ||
self.seconds -= 1; | ||
self.nanos += NANOS_PER_SECOND; | ||
} | ||
// TODO: should this be checked? | ||
// debug_assert!(self.seconds >= -315_576_000_000 && self.seconds <= 315_576_000_000, | ||
// "invalid duration: {:?}", self); | ||
} | ||
} | ||
|
||
/// Converts a `std::time::Duration` to a `Duration`. | ||
impl From<time::Duration> for Duration { | ||
fn from(duration: time::Duration) -> Duration { | ||
let seconds = duration.as_secs(); | ||
let seconds = if seconds > i64::MAX as u64 { | ||
i64::MAX | ||
} else { | ||
seconds as i64 | ||
}; | ||
let nanos = duration.subsec_nanos(); | ||
let nanos = if nanos > i32::MAX as u32 { | ||
i32::MAX | ||
} else { | ||
nanos as i32 | ||
}; | ||
let mut duration = Duration { seconds, nanos }; | ||
duration.normalize(); | ||
duration | ||
} | ||
} | ||
|
||
impl TryFrom<Duration> for time::Duration { | ||
type Error = time::Duration; | ||
|
||
/// Converts a `Duration` to a result containing a positive (`Ok`) or negative (`Err`) | ||
/// `std::time::Duration`. | ||
fn try_from(mut duration: Duration) -> Result<time::Duration, time::Duration> { | ||
duration.normalize(); | ||
if duration.seconds >= 0 { | ||
Ok(time::Duration::new( | ||
duration.seconds as u64, | ||
duration.nanos as u32, | ||
)) | ||
} else { | ||
Err(time::Duration::new( | ||
(-duration.seconds) as u64, | ||
(-duration.nanos) as u32, | ||
)) | ||
} | ||
} | ||
} | ||
|
||
impl Timestamp { | ||
/// Normalizes the timestamp to a canonical format. | ||
/// | ||
/// Based on [`google::protobuf::util::CreateNormalized`][1]. | ||
/// [1]: https://github.com/google/protobuf/blob/v3.3.2/src/google/protobuf/util/time_util.cc#L59-L77 | ||
fn normalize(&mut self) { | ||
// Make sure nanos is in the range. | ||
if self.nanos <= -NANOS_PER_SECOND || self.nanos >= NANOS_PER_SECOND { | ||
self.seconds += (self.nanos / NANOS_PER_SECOND) as i64; | ||
self.nanos %= NANOS_PER_SECOND; | ||
} | ||
|
||
// For Timestamp nanos should be in the range [0, 999999999]. | ||
if self.nanos < 0 { | ||
self.seconds -= 1; | ||
self.nanos += NANOS_PER_SECOND; | ||
} | ||
|
||
// TODO: should this be checked? | ||
// debug_assert!(self.seconds >= -62_135_596_800 && self.seconds <= 253_402_300_799, | ||
// "invalid timestamp: {:?}", self); | ||
} | ||
} | ||
|
||
/// Converts a `std::time::SystemTime` to a `Timestamp`. | ||
impl From<time::SystemTime> for Timestamp { | ||
fn from(time: time::SystemTime) -> Timestamp { | ||
let duration = Duration::from(time.duration_since(time::UNIX_EPOCH).unwrap()); | ||
Timestamp { | ||
seconds: duration.seconds, | ||
nanos: duration.nanos, | ||
} | ||
} | ||
} | ||
|
||
impl TryFrom<Timestamp> for time::SystemTime { | ||
type Error = time::Duration; | ||
|
||
/// Converts a `Timestamp` to a `SystemTime`, or if the timestamp falls before the Unix epoch, | ||
/// a duration containing the difference. | ||
fn try_from(mut timestamp: Timestamp) -> Result<time::SystemTime, time::Duration> { | ||
timestamp.normalize(); | ||
if timestamp.seconds >= 0 { | ||
Ok(time::UNIX_EPOCH | ||
+ time::Duration::new(timestamp.seconds as u64, timestamp.nanos as u32)) | ||
} else { | ||
let mut duration = Duration { | ||
seconds: -timestamp.seconds, | ||
nanos: timestamp.nanos, | ||
}; | ||
duration.normalize(); | ||
Err(time::Duration::new( | ||
duration.seconds as u64, | ||
duration.nanos as u32, | ||
)) | ||
} | ||
} | ||
} |
Oops, something went wrong.