Conditional compilation for mocked implementations



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: . 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.



Did you try inserting:

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

at line 14 in 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.


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.