These are chat archives for rust-lang/rust

13th
May 2018
apiraino
@apiraino
May 13 2018 08:40
Hi I'm trying something silly (which I can do in some other languages). Passing a &str parameter to a function; that function will invoke another function named like that parameter
but instead of passing a FnI'd pass a string of some kind
any suggestion?
the idea is to have a simple "executor" that trigger a function passed as parameter from the command line in env::args()
the point is to avoid a long match switching
apiraino
@apiraino
May 13 2018 08:47
I've also tried using a macro, but I did not understand how to do the function calling. as explained here:
https://rustbyexample.com/macros/designators.html#designators
Denis Lisov
@tanriol
May 13 2018 09:01
@apiraino No nice and easy way. This is way more difficult in compiled languages than in interpreted ones. Probably something proc-macro based could fly, but I haven't seen such a macro yet.
Furthermore, you'll still need matching code for arguments to your subcommands, won't you?
apiraino
@apiraino
May 13 2018 09:02
indeed
that is why I would not put that thing in production ;-)
tandrysyawaludin
@tandrysyawaludin
May 13 2018 11:33
I have column data with type JSON in postgres. What type in struct which can be fit with?
I have column data with type JSON in postgres. What type in struct which can be fit with?
apiraino
@apiraino
May 13 2018 13:47
@tanriol just "for the lulz" I did some more research. I ended up managing the mapping with an hashmap. Ugly as hell, but a bit more compact than a match:
https://play.rust-lang.org/?gist=37afa253d640a037ba54131743144507&version=stable&mode=debug
the compiler notices by itself when there's a reference to a undefined function. crazy.
tandrysyawaludin
@tandrysyawaludin
May 13 2018 14:00
i created pub struct but i got error message struct xxx is private. Anyone know why?
i created pub struct but i got error message struct xxx is private. Anyone know why?
Michal 'vorner' Vaner
@vorner
May 13 2018 14:10
Is it possibly inside a private sub-module?
tandrysyawaludin
@tandrysyawaludin
May 13 2018 14:15
i never set private module
ozgurakkurt
@ozgurakkurt
May 13 2018 14:24
if let DbError::ConnectionError(e) = res {
    if let Some(e) = e.code() {
        if let Some(e) = e.code() {
            if e == "23505" {
                // something
            }
        }
    }
} else {
    // something
}
Hi, can I someone help me make this prettier?
tandrysyawaludin
@tandrysyawaludin
May 13 2018 14:29
why you call if let Some(e) = e.code() { twice? @ozgurakkurt
ozgurakkurt
@ozgurakkurt
May 13 2018 14:31
I am using postgres crate and I want to check if a unique constraint fails. e.code() returns SqlState when I call .code() on it it returns string
oh
Denis
@mexus
May 13 2018 14:32
well, at the very least you can do if let Some(e) = e.code().and_then(|e| e.code()) {
unfortunately you can't combine if let with other conditions, like if let Some(e) = ... && e == "..."
but you could do that in match branches:
match e.code().and_then(|e| e.code()) {
  Some(e) if e == "23505" => {...},
  _ => {}
}
ozgurakkurt
@ozgurakkurt
May 13 2018 14:41
it works. thank you
tandrysyawaludin
@tandrysyawaludin
May 13 2018 14:44
pub enum User_role_enum {
    Customer,
    SigninLog,
}
i have this enum, how to create the value such as Customer = "customer_market"
Denis
@mexus
May 13 2018 14:48
@ozgurakkurt :+1:
@tandrysyawaludin you could declare Customer as Customer(String)
like
pub enum User_role_enum {
    Customer(String),
    SigninLog,
}
and use it like let customer = User_role_enum::Customer("customer-1".into())
(into converts &str into a String)
tandrysyawaludin
@tandrysyawaludin
May 13 2018 14:51
and how i set in this struct?
pub struct Example {
    pub user_group: ?
}
Denis
@mexus
May 13 2018 14:52
: User_role_enum
although i guess it's against rust naming policy
it should rather be UserRoleEnum
tandrysyawaludin
@tandrysyawaludin
May 13 2018 14:55
18 | let customer = UserRoleEnum::Customer("customer".into());
   | ^^^ expected item
why?
tandrysyawaludin
@tandrysyawaludin
May 13 2018 15:03
actualy i have the function
pub fn create(new_signin_log: NewSigninLog, connection: &PgConnection) -> SigninLog {
        diesel::insert_into(signin_log::table)
            .values(&new_signin_log)
            .execute(connection)
            .expect("Error creating new signin_log");

        signin_log::table.order(signin_log::id.desc()).first(connection).unwrap()
    }
where i have to put this
let customer = UserRoleEnum::Customer("customer".into());
Michal 'vorner' Vaner
@vorner
May 13 2018 15:11
Let's say I have my own type. Is there something I can do to make it initializable in global context, as part of static STUFF: MyType?
(I know about lazy_static, but it's not a good solution for me here)
Denis Lisov
@tanriol
May 13 2018 15:16
@apiraino Why is this not enough? It's a bit more compact :-)
apiraino
@apiraino
May 13 2018 15:18
that's more or less what I resorted in the first place: https://github.com/apiraino/mpdforwarder_async/blob/master/src/main.rs#L26
but yours is cleaner, I like it
I just tried an alternate way (like you said it's easier with interpreted lang.)
:+1: thanks
I still write a lot of redundant code :-/ long way to go ....
Denis Lisov
@tanriol
May 13 2018 15:22
When I need a command-line utility, I generally delegate argument parsing to structopt and then deal with a struct/enum of already parsed arguments...
apiraino
@apiraino
May 13 2018 15:26
nice to have on more structured projects. thanks, I'll keep it in mind