Skip to content

Commit

Permalink
Merge pull request #38 from EvolveArt/fix/crate-name
Browse files Browse the repository at this point in the history
fix: underscore crate name
  • Loading branch information
ProbablyClem authored Aug 17, 2024
2 parents f49d451 + 5c823c9 commit eb04cba
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 2 deletions.
9 changes: 9 additions & 0 deletions acceptance/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion acceptance/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crate_segment_path"]
members = ["crate_segment_path", "folder_in_src"]
resolver = "2"

[workspace.package]
Expand Down
3 changes: 2 additions & 1 deletion acceptance/crate_segment_path/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ path = "crate_folder/lib.rs"

[dependencies]
utoipa.workspace = true
utoipauto.workspace = true
utoipauto.workspace = true
folder-in-src = { path = "../folder_in_src" }
13 changes: 13 additions & 0 deletions acceptance/crate_segment_path/crate_folder/sub_folder/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ use utoipauto::utoipauto;
#[allow(dead_code)]
pub struct CrateInAnotherPath {}

#[utoipauto(
paths = "( ./folder_in_src/crate_folder/new_sub_folder/paths.rs from folder-in-src::new_sub_folder )"
)]
#[derive(OpenApi)]
#[openapi(info(title = "Percentage API", version = "1.0.0"))]
#[allow(dead_code)]
pub struct CrateInAnotherCrate {}

#[test]
fn test_crate_in_another_path() {
assert_eq!(CrateInAnotherPath::openapi().paths.paths.len(), 2)
}

#[test]
fn test_crate_in_another_crate() {
assert_eq!(CrateInAnotherCrate::openapi().paths.paths.len(), 2)
}
18 changes: 18 additions & 0 deletions acceptance/folder_in_src/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "folder-in-src"
description = "Test library that tests folders in the src directory."
authors.workspace = true
version.workspace = true
edition.workspace = true
publish.workspace = true
readme.workspace = true
license.workspace = true
repository.workspace = true
homepage.workspace = true

[lib]
path = "crate_folder/lib.rs"

[dependencies]
utoipa.workspace = true
utoipauto.workspace = true
1 change: 1 addition & 0 deletions acceptance/folder_in_src/crate_folder/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod new_sub_folder;
2 changes: 2 additions & 0 deletions acceptance/folder_in_src/crate_folder/new_sub_folder/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod paths;
mod test;
7 changes: 7 additions & 0 deletions acceptance/folder_in_src/crate_folder/new_sub_folder/paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![allow(dead_code)] // This code is used in the tests

#[utoipa::path(post, path = "/route1new")]
pub fn route1new() {}

#[utoipa::path(post, path = "/route2new")]
pub fn route2new() {}
13 changes: 13 additions & 0 deletions acceptance/folder_in_src/crate_folder/new_sub_folder/test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use utoipa::OpenApi;
use utoipauto::utoipauto;

#[utoipauto(paths = "( ./folder_in_src/crate_folder/new_sub_folder/paths.rs from crate::new_sub_folder )")]
#[derive(OpenApi)]
#[openapi(info(title = "Percentage API", version = "1.0.0"))]
#[allow(dead_code)]
pub struct CrateInAnotherPath {}

#[test]
fn test_crate_in_another_path() {
assert_eq!(CrateInAnotherPath::openapi().paths.paths.len(), 2)
}
9 changes: 9 additions & 0 deletions utoipauto-core/src/file_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ pub fn extract_module_name_from_path(path: &str, crate_name: &str) -> String {
// Also skip fragments that are already out of the crate name. For example,
// `./src/lib/my/module/name from crate::my::module` should turn into `crate::my::module:name`,
// and not into `crate::lib::my::module::name`.
let crate_name = crate_name.replace("-", "_");
let mut crate_segments = crate_name.split("::");
let first_crate_fragment = crate_segments.next().expect("Crate should not be empty");
let segments_inside_crate = match crate_segments.next() {
Expand Down Expand Up @@ -192,4 +193,12 @@ mod tests {
"crate::routes::asset"
);
}

#[test]
fn test_extract_module_name_from_workspace_with_external_crate_and_underscore() {
assert_eq!(
extract_module_name_from_path("./src/applications/src/retail-api/controllers/mod.rs", "other-crate"),
"other_crate::retail-api::controllers"
);
}
}

0 comments on commit eb04cba

Please sign in to comment.