Skip to content

Commit a436bb5

Browse files
authored
Allow ignoring paths in client gen (#29)
* Allow ignoring paths in client gen * debug * fix filter * update * fix * fix * fix * fmt
1 parent 412550c commit a436bb5

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ impl Error {
6262

6363
pub type Result<T> = result::Result<T, Error>;
6464

65+
#[allow(clippy::too_many_arguments)]
6566
pub fn gen(
6667
openapi_specs: Vec<OpenAPI>,
6768
target: &Path,
@@ -70,6 +71,7 @@ pub fn gen(
7071
overwrite_cargo: bool,
7172
disable_clippy: bool,
7273
mapping: &[(&str, &str)],
74+
ignored_paths: &[&str],
7375
) -> Result<()> {
7476
let mapping: HashMap<&str, &str> = HashMap::from_iter(mapping.iter().cloned());
7577

@@ -90,7 +92,14 @@ pub fn gen(
9092
let modules: Result<Vec<Module>> = open_api
9193
.tags
9294
.iter()
93-
.map(|tag| rust::client_gen::client_gen(&open_api, Some(tag.clone()), &mut ref_cache))
95+
.map(|tag| {
96+
rust::client_gen::client_gen(
97+
&open_api,
98+
Some(tag.clone()),
99+
&mut ref_cache,
100+
ignored_paths,
101+
)
102+
})
94103
.collect();
95104

96105
let mut api_module_defs = Vec::new();

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ fn main() {
6565
true,
6666
false,
6767
&[],
68+
&[],
6869
)
6970
.unwrap();
7071
}

src/rust/client_gen.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,22 +1072,31 @@ pub fn client_gen(
10721072
open_api: &OpenAPI,
10731073
tag: Option<Tag>,
10741074
ref_cache: &mut RefCache,
1075+
ignored_paths: &[&str],
10751076
) -> Result<Module> {
1076-
let paths: HashSet<String> = open_api
1077+
let ignored_paths: HashSet<String> =
1078+
HashSet::from_iter(ignored_paths.iter().map(|ip| ip.to_string()));
1079+
1080+
let paths: HashMap<String, ReferenceOr<PathItem>> = open_api
10771081
.paths
10781082
.iter()
1079-
.filter(|(_, path_item)| match_tag(&tag, path_item))
1080-
.map(|(p, _)| p.clone())
1083+
.filter_map(|(path_key, path_item)| {
1084+
if !ignored_paths.contains(path_key) && match_tag(&tag, path_item) {
1085+
Some((path_key.clone(), path_item.clone()))
1086+
} else {
1087+
None
1088+
}
1089+
})
10811090
.collect();
10821091

1083-
let paths: Vec<Path> = paths.into_iter().map(|p| Path::from_string(&p)).collect();
1084-
1085-
let common_prefix = paths.into_iter().reduce(|acc, e| e.common_prefix(acc));
1092+
let common_prefix = paths
1093+
.keys()
1094+
.map(|p| Path::from_string(p))
1095+
.reduce(|acc, e| e.common_prefix(acc));
10861096

10871097
let prefix_length = common_prefix.map(|p| p.0.len()).unwrap_or(0);
10881098

1089-
let operations: Vec<PathOperation> = open_api
1090-
.paths
1099+
let operations: Vec<PathOperation> = paths
10911100
.iter()
10921101
.flat_map(|(path, op)| tag_operations(&tag, path, op))
10931102
.collect();

0 commit comments

Comments
 (0)