Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,11 @@ pub fn merge_files(fragments: Vec<KtFile>) -> Result<Vec<KtFile>, WriteKotlinErr
Ok(groups.into_values().collect())
}

/// The flattened on-disk path of one merged file under `kotlin_root`.
/// The flattened on-disk path of one merged file under `kotlin_root`:
/// `io.zenoh.jni.bytes` becomes `<kotlin_root>/io/zenoh/jni/bytes.kt`.
///
/// [`write_files`] uses this to lay out its output; it is public so a consumer
/// can predict, report or post-process those paths without writing anything.
/// `fallback_name` names the file when the package is empty.
pub fn merged_file_path(kotlin_root: &Path, file: &KtFile, fallback_name: &str) -> PathBuf {
if file.package.is_empty() {
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ mod types;
mod tests;

pub use code::KtCode;
pub use file::{merge_files, write_files, WriteKotlinError};
pub use file::{merge_files, merged_file_path, write_files, WriteKotlinError};
pub use ident::{
escape_kotlin_ident, is_kotlin_hard_keyword, is_valid_kotlin_ident, is_valid_kotlin_package,
mangle_kotlin_ident, mangle_kotlin_package, KOTLIN_HARD_KEYWORDS,
Expand All @@ -32,5 +32,6 @@ pub use model::{
KtFile, KtFun, KtFunInterface, KtFunSig, KtParam, KtProperty, KtSuperclass, KtSupertypes,
KtVis,
};
pub use render::KOTLIN_BANNER;
pub use slot::KtPropertyValue;
pub use types::{ImportSet, KtType};
7 changes: 6 additions & 1 deletion src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ use super::{
types::{ImportSet, KtType},
};

/// First line of every generated file.
/// The default first line of every generated file — the "do not edit" marker
/// a reader needs to know the file is machine-written.
///
/// [`KtFile::banner`] overrides it per file, and `""` suppresses it entirely;
/// this constant is what an override falls back to, and what a consumer
/// prepending its own header should match against.
Comment on lines +16 to +18
pub const KOTLIN_BANNER: &str = "// Auto-generated by kotlin-codegen — do not edit by hand.";

/// When a function's single-line signature (from the indentation through the
Expand Down
20 changes: 18 additions & 2 deletions src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,10 +543,10 @@ fn merge_files_groups_by_package_and_rejects_duplicates() {
#[test]
fn merged_file_path_is_flattened() {
let f = KtFile::new("io.zenoh.jni.bytes");
let p = file::merged_file_path(std::path::Path::new("/root"), &f, "X");
let p = merged_file_path(std::path::Path::new("/root"), &f, "X");
assert_eq!(p, std::path::PathBuf::from("/root/io/zenoh/jni/bytes.kt"));
let empty = KtFile::new("");
let p2 = file::merged_file_path(std::path::Path::new("/root"), &empty, "NativeHandle");
let p2 = merged_file_path(std::path::Path::new("/root"), &empty, "NativeHandle");
assert_eq!(p2, std::path::PathBuf::from("/root/NativeHandle.kt"));
}

Expand Down Expand Up @@ -927,3 +927,19 @@ fn external_and_a_body_are_mutually_exclusive() {
fn external_cannot_be_passed_as_a_modifier_string() {
let _ = KtFun::new("f").modifier("external");
}

/// Both of these were `pub` in private modules — reachable from inside the
/// crate but not from a consumer. This test is written against the public
/// paths, so it fails to compile if either is un-exported again.
#[test]
fn banner_and_path_helper_are_reachable_from_the_crate_root() {
use crate as kotlin_codegen;
assert!(kotlin_codegen::KOTLIN_BANNER.starts_with("//"));
let f = KtFile::new("io.p");
assert_eq!(
kotlin_codegen::merged_file_path(std::path::Path::new("/root"), &f, "X"),
std::path::PathBuf::from("/root/io/p.kt")
);
// The default banner is what an un-overridden file actually renders.
assert!(f.render().starts_with(kotlin_codegen::KOTLIN_BANNER));
}
Loading