Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion protobuf-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ once_cell = "1.10.0"
tempfile = "3"

protobuf = { path = "../protobuf", version = "=4.0.0-alpha.0" }
protobuf-parse = { path = "../protobuf-parse", version = "=4.0.0-alpha.0" }
protobuf-parse = { path = "../protobuf-parse", version = "=4.0.0-alpha.1" }

[[bin]]

Expand Down
2 changes: 1 addition & 1 deletion protobuf-parse/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "protobuf-parse"
version = "4.0.0-alpha.0"
version = "4.0.0-alpha.1"
edition = "2021"
authors = ["Stepan Koltsov <[email protected]>"]
license = "MIT"
Expand Down
15 changes: 15 additions & 0 deletions protobuf-parse/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::collections::HashSet;
use std::ffi::OsStr;
use std::ffi::OsString;
Expand All @@ -21,6 +22,7 @@ pub struct Parser {
pub(crate) protoc: Option<PathBuf>,
pub(crate) protoc_extra_args: Vec<OsString>,
pub(crate) capture_stderr: bool,
pub(crate) custom_embedded: HashMap<String, String>,
}

impl Parser {
Expand Down Expand Up @@ -97,6 +99,19 @@ impl Parser {
self
}

/// Adds an embedded `.proto` file to the parser.
///
/// This allows the parser to use `.proto` files provided as in-memory
/// strings rather than reading them from disk.
pub fn add_custom_embedded(
&mut self,
name: impl Into<String>,
content: impl Into<String>,
) -> &mut Self {
self.custom_embedded.insert(name.into(), content.into());
self
}

/// Parse `.proto` files and typecheck them using pure Rust parser of `protoc` command.
pub fn parse_and_typecheck(&self) -> anyhow::Result<ParsedAndTypechecked> {
match &self.which_parser {
Expand Down
18 changes: 17 additions & 1 deletion protobuf-parse/src/pure/parse_and_typecheck.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::collections::HashMap;
use std::fmt;
use std::fs;
use std::io;
Expand Down Expand Up @@ -53,6 +54,7 @@ where
{
parsed_files: IndexMap<ProtoPathBuf, FileDescriptorPair>,
resolver: R,
custom_embedded: HashMap<String, String>,
}

impl<R> Run<R>
Expand Down Expand Up @@ -144,7 +146,19 @@ where
return self.add_file_content(protobuf_path, &resolved);
}

let embedded = match protobuf_path.to_str() {
let protobuf_path_str = protobuf_path.to_str();

if let Some(content) = self.custom_embedded.get(protobuf_path_str) {
return self.add_file_content(
protobuf_path,
&ResolvedProtoFile {
path: protobuf_path_str.to_string(),
content: content.as_bytes().to_vec(),
},
);
}

let embedded = match protobuf_path_str {
"rustproto.proto" => Some(proto::RUSTPROTO_PROTO),
"google/protobuf/any.proto" => Some(proto::ANY_PROTO),
"google/protobuf/api.proto" => Some(proto::API_PROTO),
Expand Down Expand Up @@ -253,6 +267,7 @@ pub fn parse_and_typecheck(parser: &Parser) -> anyhow::Result<ParsedAndTypecheck
let mut run = Run {
parsed_files: IndexMap::new(),
resolver: fs_resolver(&parser.includes),
custom_embedded: parser.custom_embedded.clone(),
};

let relative_paths = parser
Expand Down Expand Up @@ -294,6 +309,7 @@ pub fn parse_and_typecheck_custom(
let mut run = Run {
parsed_files: IndexMap::new(),
resolver,
custom_embedded: HashMap::new(),
};

for proto_path in input {
Expand Down
2 changes: 1 addition & 1 deletion protobuf/src/well_known_types/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub struct Any {
/// server that maps type URLs to message definitions as follows:
///
/// * If no scheme is provided, `https` is assumed.
/// * An HTTP GET on the URL must yield a [google.protobuf.Type][]
/// * An HTTP GET on the URL must yield a \[google.protobuf.Type\][]
/// value in binary format, or produce an error.
/// * Applications are allowed to cache lookup results based on the
/// URL, or have them precompiled into a binary to avoid any
Expand Down
Loading