Skip to content

Get on non-existing key will panic #14

@liubin

Description

@liubin

Originally posted by @gnprice in #13 (comment):

Interesting, can you say more about the motivation for this subcommand [toml check]?

I think the way I'd expect to handle this use case in a CLI is by using the "get" command, and just redirecting the output to /dev/null if I don't actually want it.

Looking at git config — which I find to be a good source of inspiration for this command's interface, as mentioned in the README — it looks like that's exactly what it does. There's no specialized "check" action in git config --help, but instead there's this:

   --get
       Get the value for a given key (optionally filtered by a regex matching the
       value). Returns error code 1 if the key was not found and the last value if
       multiple key values were found.

I think that'd work well for the toml command, too.


Looks like our current behavior in toml get if there's no data at the given path is to panic, eep:

$ target/debug/toml get Cargo.toml a.b ; echo $?
thread 'main' panicked at 'attempted to leave type `linked_hash_map::Node<alloc::string::String, table::TableKeyValue>` uninitialized, which is invalid', /home/greg/.cargo/registry/src/github.com-1ecc6299db9ec823/linked-hash-map-0.5.2/src/lib.rs:174:52
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
101

Or after upgrading dependencies, the panic is a bit less noisy but still a panic:

$ target/debug/toml get Cargo.toml a.b ; echo $?
thread 'main' panicked at 'index not found', src/main.rs:188:32
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
101

It's panicking in the walk_tpath function.

So I think basically what we want here is to take your check_exists function [from #13], which is a non-panicky version of walk_tpath, and put that logic in walk_tpath so that toml get can give a clean error result (just like git config does) instead of panicking.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions