Skip to content

Commit

Permalink
build: Add a way to source options from a arg string
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuetuopay committed Jun 25, 2021
1 parent 0fd0572 commit e2c5907
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions prost-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,25 @@ impl Default for BytesType {
}
}

const BTREE_MAP: &str = "btree_map";
const BYTES: &str = "bytes";
const FIELD_ATTR: &str = "field_attr";
const TYPE_ATTR: &str = "type_attr";
const COMPILE_WELL_KNOWN_TYPES: &str = "compile_well_known_types";
const DISABLE_COMMENTS: &str = "disable_comments";
const EXTERN_PATH: &str = "extern_path";
const RETAIN_ENUM_PREFIX: &str = "retain_enum_prefix";
pub const PROTOC_OPTS: [&str; 8] = [
BTREE_MAP,
BYTES,
FIELD_ATTR,
TYPE_ATTR,
COMPILE_WELL_KNOWN_TYPES,
DISABLE_COMMENTS,
EXTERN_PATH,
RETAIN_ENUM_PREFIX,
];

/// Configuration options for Protobuf code generation.
///
/// This configuration builder can be used to set non-default code generation options.
Expand All @@ -241,6 +260,41 @@ impl Config {
Config::default()
}

/// Creates a new code generator configuration with default options, overriden from parameters
/// passed to the protoc command line.
///
/// Format is opt1=value,opt2=value
pub fn new_from_opts(opts: &str, log_unknown: bool) -> Config {
let mut cfg = Config::new();
cfg.opts(opts, log_unknown);
cfg
}

/// Configure the code generator with the inlined options. To be used by protoc plugins.
///
/// Format is opt1=value,opt2=value
pub fn opts(&mut self, opts: &str, log_unknown: bool) -> &mut Config {
let opts = opts
.split(',')
.map(|opt| opt.split('=').collect::<Vec<_>>());
for opt in opts {
match opt.as_slice() {
[""] => (),
[BTREE_MAP, v] => self.map_type.insert(v.to_string(), MapType::BTreeMap),
[BYTES, v] => self.bytes_type.insert(v.to_string(), BytesType::Bytes),
[FIELD_ATTR, k, v] => self.field_attributes.insert(k.to_string(), v.to_string()),
[TYPE_ATTR, k, v] => self.type_attributes.insert(k.to_string(), v.to_string()),
[COMPILE_WELL_KNOWN_TYPES] => self.prost_types = false,
[DISABLE_COMMENTS, v] => self.disable_comments.insert(v.to_string(), ()),
[EXTERN_PATH, k, v] => self.extern_paths.push((k.to_string(), v.to_string())),
[RETAIN_ENUM_PREFIX] => self.strip_enum_prefix = false,
_ if log_unknown => eprintln!("prost: Unknown option `{}`", opt.join("=")),
_ => (),
}
}
self
}

/// Configure the code generator to generate Rust [`BTreeMap`][1] fields for Protobuf
/// [`map`][2] type fields.
///
Expand Down

0 comments on commit e2c5907

Please sign in to comment.