link-cli 3.4.0

A CLI tool and reusable library for links manipulation backed by a LiNo-notation doublet storage engine.
Documentation
use anyhow::Result;
use link_cli::{import_lino_text, import_lino_text_with_max_depth, NamedTypeLinks};
#[cfg(not(target_arch = "wasm32"))]
#[path = "common/storage.rs"]
mod test_store;
#[cfg(target_arch = "wasm32")]
use crate::test_store;

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn library_import_rejects_invalid_limits_without_writing() -> Result<()> {
    let (_file, mut storage) = test_store::links()?;
    for depth in [0, 129] {
        let error = import_lino_text_with_max_depth(&mut storage, "(new: a b)", depth).unwrap_err();
        assert!(error.to_string().contains("integer from 1 to 128"));
        assert!(storage.lino_lines().is_empty());
    }
    import_lino_text_with_max_depth(&mut storage, "(new: a b)", 1)?;
    assert!(storage.get_by_name("new").is_some());
    Ok(())
}

fn import_at_max_depth() -> Result<()> {
    let (_file, mut storage) = test_store::links()?;
    let input = format!("(root: end {}end{}", "(item ".repeat(127), ")".repeat(128));
    import_lino_text_with_max_depth(&mut storage, &input, 128)?;
    assert!(storage.get_by_name("root").is_some());
    assert_eq!(storage.lino_lines().len(), 130);
    Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
#[test]
fn depth_override_works_with_a_one_mib_caller_stack() -> Result<()> {
    std::thread::Builder::new()
        .stack_size(1024 * 1024)
        .spawn(import_at_max_depth)?
        .join()
        .unwrap()
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen_test::wasm_bindgen_test]
fn depth_override_works_in_webassembly() -> Result<()> {
    import_at_max_depth()
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn invalid_import_leaves_links_and_names_unchanged() -> Result<()> {
    for populated in [false, true] {
        for invalid in [
            "(c: c",
            "(c: c)",
            "(c: c c c)",
            "(x y)",
            "(c: (nested: a b) (bad: a))",
            "(c: a ())",
        ] {
            let (_file, mut storage) = test_store::links()?;
            if populated {
                import_lino_text(&mut storage, "(a: a a)\n(b: b b)\n(existing: a b)")?;
            }
            let before = storage.lino_lines();

            assert!(
                import_lino_text(
                    &mut storage,
                    &format!("(a: b a)\n(new: fresh fresh)\n{invalid}")
                )
                .is_err(),
                "accepted {invalid}"
            );

            assert_eq!(storage.lino_lines(), before, "input: {invalid}");
            assert_eq!(storage.get_by_name("new"), None);
            assert_eq!(storage.get_by_name("fresh"), None);
        }
    }
    Ok(())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_accepts_named_and_anonymous_nested_links() -> Result<()> {
    let (_file, mut storage) = test_store::links()?;
    import_lino_text(
        &mut storage,
        "(a: a a)\n(b: b b)\n(child: (nested: a b) (b a))",
    )?;

    let nested = storage.get_by_name("nested").unwrap();
    let child = storage
        .get_link(storage.get_by_name("child").unwrap())
        .unwrap();
    assert_eq!(child.source, nested);
    let target = storage.get_link(child.target).unwrap();
    assert_eq!(target.source, storage.get_by_name("b").unwrap());
    assert_eq!(target.target, storage.get_by_name("a").unwrap());
    Ok(())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_reproduces_numbered_links_at_explicit_indexes() -> Result<()> {
    let (_temp_file, mut storage) = test_store::links()?;

    import_lino_text(
        &mut storage,
        r#"
        (1: 1 1)
        (2: 1 2)
        (3: 2 1)
        "#,
    )?;

    assert_eq!(
        storage.lino_lines(),
        vec!["(1: 1 1)", "(2: 1 2)", "(3: 2 1)"]
    );

    Ok(())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_creates_named_references_as_point_links() -> Result<()> {
    let (_temp_file, mut storage) = test_store::links()?;

    import_lino_text(&mut storage, "(child: father mother)")?;

    let father = storage.get_by_name("father").expect("father should exist");
    let mother = storage.get_by_name("mother").expect("mother should exist");
    let child = storage.get_by_name("child").expect("child should exist");

    let father_link = storage.get_link(father).expect("father link should exist");
    let mother_link = storage.get_link(mother).expect("mother link should exist");
    let child_link = storage.get_link(child).expect("child link should exist");

    assert_eq!(father_link.source, father);
    assert_eq!(father_link.target, father);
    assert_eq!(mother_link.source, mother);
    assert_eq!(mother_link.target, mother);
    assert_eq!(child_link.source, father);
    assert_eq!(child_link.target, mother);

    let lines = storage.lino_lines();
    assert!(lines.contains(&"(father: father father)".to_string()));
    assert!(lines.contains(&"(mother: mother mother)".to_string()));
    assert!(lines.contains(&"(child: father mother)".to_string()));

    Ok(())
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), test)]
fn import_lino_text_treats_out_of_range_numbers_as_names() -> Result<()> {
    let (_temp_file, mut storage) = test_store::links()?;
    let out_of_range_number = u64::from(u32::MAX) + 1;

    // Target `2` rather than `1` for the same reason the C# counterpart
    // (`LinoDatabaseInputTests.ImportText_TreatsOutOfRangeNumbersAsNames`)
    // uses it: the out-of-range number becomes the point link `(1: 1 1)`,
    // so `(child: <number> 1)` would ask for a second `(1, 1)` link and
    // uniqueness resolution would merge `child` into it.
    import_lino_text(&mut storage, &format!("(child: {out_of_range_number} 2)"))?;

    let numeric_name = storage
        .get_by_name(&out_of_range_number.to_string())
        .expect("out-of-range number should be named");
    let child = storage.get_by_name("child").expect("child should exist");

    let numeric_name_link = storage
        .get_link(numeric_name)
        .expect("named number link should exist");
    let child_link = storage.get_link(child).expect("child link should exist");

    assert_eq!(numeric_name_link.source, numeric_name);
    assert_eq!(numeric_name_link.target, numeric_name);
    assert_eq!(child_link.source, numeric_name);

    Ok(())
}