Skip to content

Commit

Permalink
feat: common crate name config
Browse files Browse the repository at this point in the history
  • Loading branch information
junhaideng committed Feb 16, 2024
1 parent 8c497c6 commit 7ffb7c6
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
14 changes: 14 additions & 0 deletions pilota-build/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ pub struct Builder<MkB, P> {
keep_unknown_fields: Vec<std::path::PathBuf>,
dedups: Vec<FastStr>,
nonstandard_snake_case: bool,
common_crate_name: FastStr
}

impl Builder<MkThriftBackend, ThriftParser> {
Expand All @@ -105,6 +106,7 @@ impl Builder<MkThriftBackend, ThriftParser> {
keep_unknown_fields: Vec::default(),
dedups: Vec::default(),
nonstandard_snake_case: false,
common_crate_name: "common".into()
}
}
}
Expand All @@ -126,6 +128,7 @@ impl Builder<MkProtobufBackend, ProtobufParser> {
keep_unknown_fields: Vec::default(),
dedups: Vec::default(),
nonstandard_snake_case: false,
common_crate_name: "common".into(),
}
}
}
Expand All @@ -144,6 +147,12 @@ where
self.nonstandard_snake_case = flag;
self
}

pub fn common_crate_name(mut self, name: FastStr) -> Self {
self.parser.common_crate_name(name.clone());
self.common_crate_name = name;
self
}
}

impl<MkB, P> Builder<MkB, P> {
Expand All @@ -159,6 +168,7 @@ impl<MkB, P> Builder<MkB, P> {
keep_unknown_fields: self.keep_unknown_fields,
dedups: self.dedups,
nonstandard_snake_case: self.nonstandard_snake_case,
common_crate_name: self.common_crate_name
}
}

Expand Down Expand Up @@ -262,6 +272,7 @@ where
keep_unknown_fields: Vec<PathBuf>,
dedups: Vec<FastStr>,
nonstandard_snake_case: bool,
common_crate_name: FastStr,
) -> Context {
let mut db = RootDatabase::default();
parser.inputs(services.iter().map(|s| &s.path));
Expand Down Expand Up @@ -336,6 +347,7 @@ where
change_case,
dedups,
nonstandard_snake_case,
common_crate_name
)
}

Expand All @@ -353,6 +365,7 @@ where
self.keep_unknown_fields,
self.dedups,
self.nonstandard_snake_case,
self.common_crate_name
);

cx.exec_plugin(BoxedPlugin);
Expand Down Expand Up @@ -434,6 +447,7 @@ where
self.keep_unknown_fields,
self.dedups,
self.nonstandard_snake_case,
self.common_crate_name
);

std::thread::scope(|_scope| {
Expand Down
6 changes: 5 additions & 1 deletion pilota-build/src/middle/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct Context {
pub plugin_gen: DashMap<DefLocation, String>,
pub(crate) dedups: Vec<FastStr>,
pub(crate) nonstandard_snake_case: bool,
pub(crate) common_crate_name: FastStr
}

impl Clone for Context {
Expand All @@ -89,6 +90,7 @@ impl Clone for Context {
plugin_gen: self.plugin_gen.clone(),
dedups: self.dedups.clone(),
nonstandard_snake_case: self.nonstandard_snake_case,
common_crate_name: self.common_crate_name.clone(),
}
}
}
Expand Down Expand Up @@ -426,6 +428,7 @@ impl ContextBuilder {
change_case: bool,
dedups: Vec<FastStr>,
nonstandard_snake_case: bool,
common_crate_name: FastStr,
) -> Context {
Context {
adjusts: Default::default(),
Expand All @@ -445,6 +448,7 @@ impl ContextBuilder {
plugin_gen: Default::default(),
dedups,
nonstandard_snake_case,
common_crate_name
}
}
}
Expand Down Expand Up @@ -956,7 +960,7 @@ impl Context {
.into()
})
}
DefLocation::Dynamic => "common".into(),
DefLocation::Dynamic => self.common_crate_name.clone(),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion pilota-build/src/middle/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl PathResolver for WorkspacePathResolver {
path.extend(prefix.iter().cloned());
path
}
super::context::DefLocation::Dynamic => ["common".into()]
super::context::DefLocation::Dynamic => [cx.common_crate_name.clone().into()]
.iter()
.chain(DefaultPathResolver.mod_prefix(cx, def_id).iter())
.cloned()
Expand Down
3 changes: 3 additions & 0 deletions pilota-build/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{ir::File, symbol::FileId};
pub(crate) mod protobuf;
pub(crate) mod thrift;

use faststr::FastStr;
use rustc_hash::FxHashMap;
pub use thrift::ThriftParser;

Expand All @@ -30,5 +31,7 @@ pub trait Parser {

fn nonstandard_snake_case(&mut self, _nonstandard: bool) {}

fn common_crate_name(&mut self, _name: FastStr) {}

fn parse(self) -> ParseResult;
}

0 comments on commit 7ffb7c6

Please sign in to comment.