aatxe / irc

the irc crate – usable, async IRC for Rust
Mozilla Public License 2.0
530 stars 97 forks source link

[help wanted] error on parsing a valid TOML configuration #101

Closed mklcp closed 6 years ago

mklcp commented 6 years ago

Hello,
I'm getting the following error:

Error(Io(Error { repr: Custom(Custom { kind: InvalidInput, error: StringError("Failed to decode TOML configuration file.") }) }), State { next_error: Some(Error { inner: ErrorInner { kind: Custom, line: None, col: 0, message: "invalid type: sequence, expected a string", key: ["options", "array"] } }), backtrace: None })', /checkout/src/libcore/result.rs:906:4
note: Run with `RUST_BACKTRACE=1` for a backtrace.

when I launch the configuration

nickname = "pine"
alt_nicks = ["apple","pen"]
server = "chat.freenode.net"
use_ssl = true
port = 6697
encoding = "UTF-8"
channels = ["broda","brode"]

[options]
string = "imastring"
array = ["i","m","a","array"]

with the method IrcServer::new().

But with this one:

nickname = "pine"
alt_nicks = ["apple","pen"]
server = "chat.freenode.net"
use_ssl = true
port = 6697
encoding = "UTF-8"
channels = ["broda","brode"]

[options]
string = "imastring"
array = "imnotanarray"

there's no problems.

This crate is using the crate toml; so I've tested if it was a problem of toml rather than irc, with this program:

extern crate toml;
use toml::Value;
use std::fs::File;
use std::io::Read;
use std::io;
use std::env;
fn main() {
    let args = env::args().collect::<Vec<_>>();
    let data = load(&args[1]).unwrap();
    let value = data.parse::<Value>().unwrap();
    println!("{}",value);
    println!("{:?}",value);
}

fn load(x: &str) -> Result<String, io::Error> {
    let mut s = String::new();
    File::open(x)?.read_to_string(&mut s)?;
    Ok(s)
}

And this program works for both config expressed above.
I was so wanting to know if it's really a bug in the use of toml by irc, or if there's any mistakes in my approach.

Thank you!

MTRNord commented 6 years ago

not sure but I think you might want to remove the field data of discord_token (just as a note)

mklcp commented 6 years ago

Oh yes, not needed for a minimal working example; but that doesn't change the problem either :)

aatxe commented 6 years ago

So, I wouldn't really call this a bug per se. It is actually expected behavior, but I recognize that that's not necessarily the best answer. The options field is just a HashMap of Strings to Strings. That's why you're getting an error when using the array.

That being said, I would like to fix this somehow, but I'm not sure what the best way of doing it is. Unfortunately, I think the best practice right now is to roll-your-own config if you want to do something particularly involved over what this library offers. There ought to be a better way to set up configurations. If nothing else, maybe just making it easier to build your own data structure (probably by shrinking the current Config somewhat, removing options, and adding some nice macros). I'll think about this some more though.

mklcp commented 6 years ago

Possibly found the problem: in the struct Config, the field options has the type Option<HashMap<String, String>>, which obviously not allow other toml types. For simplicity's sake, it's a understandable to use a <String, String>, but practically, since rust projects often use TOML, it may be good to provide more general features when loading a TOML, just to not have to load several TOML files, or modify the library to modify Config.
Or one can still make a multiline string and invoke a toml parser on it in the program containing irc; the proposal described is just a small feature (and I've understand the problem myself, so this issue becomes less useful); at least modifying the description of the [options] part to precise that it only allow strings would be good. [EDIT: you posted before me; then nothing to add I think; so I close the issue.]