Skip to main content

merge_files

Function merge_files 

Source
pub fn merge_files(
    fragments: Vec<KtFile>,
) -> Result<Vec<KtFile>, WriteKotlinError>
Expand description

Merge fragments into one KtFile per package (sorted package order; within a package, fragments keep their emission order), then validate each merged file with every check at its default severity.

A KtFile::banner override carries into the merged file — the first fragment of a package that sets one wins.

Use merge_files_with to change a check’s severity or to see warnings.

Examples found in repository?
examples/invalid.rs (line 32)
23fn main() {
24    println!("=== diagnostics (default: every check is an error) ===");
25    for file in broken_files() {
26        for d in file.validate() {
27            println!("{d}");
28        }
29    }
30
31    println!("\n=== merging refuses to produce anything ===");
32    match merge_files(broken_files()) {
33        Ok(_) => println!("unexpectedly merged"),
34        // The Display of the error is what a build script would surface.
35        Err(e) => print!("{e}"),
36    }
37
38    println!("\n=== the same model under `warn_all()` ===");
39    let (files, warnings) = merge_files_warning();
40    println!(
41        "merged {} file(s) with {} warning(s); generation continues",
42        files.len(),
43        warnings.len()
44    );
45
46    println!("\n=== one check downgraded, another switched off ===");
47    let policy = ValidationPolicy::new()
48        .warn(Check::DuplicateFunction)
49        .allow(Check::InvalidIdentifier);
50    for file in broken_files() {
51        for d in file.validate_with(&policy) {
52            println!("{d}");
53        }
54    }
55
56    println!("\n=== shapes the model will not build at all ===");
57    for (what, outcome) in refused_shapes() {
58        println!("{what}\n    -> {outcome}");
59    }
60}
More examples
Hide additional examples
examples/showcase.rs (line 30)
22fn main() {
23    let fragments = vec![
24        types_fragment(),
25        session_fragment(),
26        natives_fragment(),
27        root_fragment(),
28    ];
29
30    let files = merge_files(fragments).expect("the showcase model is valid");
31
32    println!("=== generated files ===");
33    for file in &files {
34        let path = merged_file_path(std::path::Path::new("kotlin"), file, "Generated");
35        println!("\n--- {} ---", path.display());
36        print!("{}", file.render());
37    }
38
39    println!("\n=== identifier helpers ===");
40    for raw in ["myValue", "my-field", "2fast", "object", "data", ""] {
41        let quoted = format!("{raw:?}");
42        let mangled = format!("{:?}", mangle_kotlin_ident(raw));
43        let escaped = format!("{:?}", escape_kotlin_ident(raw));
44        let valid = is_valid_kotlin_ident(raw);
45        let keyword = is_kotlin_hard_keyword(raw);
46        println!(
47            "{quoted:>10} -> valid={valid:<5} keyword={keyword:<5} \
48             mangled={mangled:<10} escaped={escaped}"
49        );
50    }
51    for raw in ["io.example.api", "fun.my-pkg", "io..jni."] {
52        let quoted = format!("{raw:?}");
53        let mangled = format!("{:?}", mangle_kotlin_package(raw));
54        let valid = is_valid_kotlin_package(raw);
55        println!("{quoted:>16} -> valid={valid:<5} mangled={mangled}");
56    }
57}