Conditional compilation for mocked implementations


#1

Hi,

I’m interfacing with a C-library (wxwidgets library to ask a pincode to access smart cards), but I don’t always have a reader at disposal or a GUI (during travis compilation)
So I wanted to write an interface (a trait) and during compilation I wanted to choose an implementation. The real pcsc interface and pincode GUI implementation for normal compilation and a mocked versions when I use a feature.
So I can give a command line option --feature “mock_pcsc mock_pincode”. I tried with pincode, but I did not succeeded to find a nice solution. Is there a pattern which can be used for what I want to do?

When you want to look at the code: https://github.com/ddt-tdd/eidonkey . You have to check the digital-signature feature branch.

I’m writing rust for 2 weeks now, so I don’t know all the tricks yet :slight_smile: . I don’t mind to refactor some things.

Greetings,
DDT


#2

Did you try inserting:

#[cfg(not(feature = "mock_pincode"))]

at line 14 in pin.rs? Or maybe right before the extern? I’m not sure but I suspect that the line 11 is only affecting line 12, not the extern code.


#3

Yes and the conditional compilation works for DonkeyCard through its cargo.yml.
But I want to set the feature in the main cargo of eidonkey and not through the dependency, but give it as command line parameter.
This way I can build and test the whole project on travis with the mocked implementation. At home I can test the implementation with real smart card.
I thought the --feature parameter in the ‘cargo’ command line would be inherited by the other cargo.yml’s

I will refactor my code to support mock implementations. I’ll remove the conditional compilation and try to implement dependency injection.

Thanks