rust-cli / rexpect

.github/workflows/ci.yml
https://docs.rs/rexpect
MIT License
328 stars 56 forks source link

get the index of element that was matched by exp_any #14

Open pbartyik opened 4 years ago

pbartyik commented 4 years ago

find fn returns a tuple with the unread buffer and the matched string by needle. The question is if it could also return the position of the expression used in ReadUntil::Any(vec)? Right now I'm looping through the passed expressions and checking which one matches the returned matched string.

Think "did I match username or password prompt, what to send back to the process".

philippkeller commented 4 years ago

I think this is a good idea. But it would need to split up find for the ReadUntil variants without ReadUntil::Any (as this extra return doesn't make sense in this context) and a find for the ReadUntil::Any explicitly.

Do you have anything in mind? I didn't use rust lately so I don't know if there's any construct which could help here or if the best way forward would be just to create a separate function which would just be for ::Any and would support the new parameter

pbartyik commented 4 years ago

honestly I'm still in the beginning of the learning curve of rust, so I may not be the best input for this. Not sure if a hacky solution would be to return an Option as the third element in the tuple, but that would save the need for a separate function. The separate fn seems more reasonable as that won't mess up the return values and keeps the feature explicit.

@TyPR124 what do you think? You are working on windows support, and this might affect you too.

TyPR124 commented 4 years ago

To be honest I haven't been looking closely at this part of the code, as I've been focusing mostly on the platform-specific parts and this code is mostly platform-agnostic. That said, I will give it some thought, as it sounds like a good idea.

To make sure I'm on the same page here, you're wanting to do something like session.exp_any(my_vec) and have it return an index into my_vec, correct?

pbartyik commented 4 years ago

Yep, along with the unread buffer till the match. The matched string have no benefit to me personally, but since it is currently returned I imagine it would be needed too.