Skip to content

Commit

Permalink
Reject dot-containing package name (#320)
Browse files Browse the repository at this point in the history
  • Loading branch information
yutannihilation authored Oct 27, 2024
1 parent 1b256b7 commit 6221b2a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

* savvy now generates a reduced number of R functions.

* savvy-cli now rejects a package name containing `.`.

## [v0.7.1] (2024-10-21)

### Bug fixes
Expand Down
18 changes: 17 additions & 1 deletion savvy-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,26 @@ fn parse_description(path: &Path) -> PackageDescription {
}

if package_name_orig.is_empty() {
eprintln!("{} is not an R package root", path.to_string_lossy());
eprintln!(
"
{} is not an R package root.
",
path.to_string_lossy()
);
std::process::exit(4);
}

if package_name_orig.contains('.') {
let package_name_new = dot_containing_to_camel_case(package_name_orig);
eprintln!(
r#"
savvy doesn't support a package name with "." ({package_name_orig}).
Please consider renaming it to "{package_name_new}".
"#,
);
std::process::exit(5);
}

PackageDescription {
package_name: package_name_orig.to_string(),
has_sysreq,
Expand Down
30 changes: 30 additions & 0 deletions savvy-cli/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,29 @@ pub(crate) fn to_snake_case(x: &str) -> String {
out
}

pub(crate) fn dot_containing_to_camel_case(x: &str) -> String {
x.split('.')
.enumerate()
.map(|(i_split, split)| {
if i_split == 0 {
return split.to_string();
}
split
.chars()
.enumerate()
.map(|(i_char, c)| {
if i_char == 0 {
c.to_uppercase().next().unwrap()
} else {
c.to_lowercase().next().unwrap()
}
})
.collect()
})
.collect::<Vec<String>>()
.join("")
}

pub(crate) fn canonicalize(path: &Path) -> Result<String, std::io::Error> {
let crate_dir_abs = path.canonicalize()?;
let crate_dir_abs = crate_dir_abs.to_string_lossy();
Expand All @@ -42,4 +65,11 @@ mod tests {
assert_eq!(&to_snake_case("FooBar"), "foo_bar");
assert_eq!(&to_snake_case("fooBarBaz"), "foo_bar_baz");
}

#[test]
fn test_camel_case() {
assert_eq!(&dot_containing_to_camel_case("foo.bar"), "fooBar");
assert_eq!(&dot_containing_to_camel_case("foo.bar.baz"), "fooBarBaz");
assert_eq!(&dot_containing_to_camel_case("foo.BAR.baz"), "fooBarBaz");
}
}

0 comments on commit 6221b2a

Please sign in to comment.