From b114125dfbb4c97580ae076c07c6816ccdca51b8 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Mon, 24 Aug 2020 18:04:20 -0700 Subject: Introduce builder infrastructure for assembling Nitrocli instance In the future we would like to provide more ways for tests to create a Nitrocli instance. In order to prevent explosion of with_XXX methods for each possible combination of arguments, this change introduces a Builder struct that can be used to create such an instance in an idiomatic way. --- src/tests/mod.rs | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'src/tests/mod.rs') diff --git a/src/tests/mod.rs b/src/tests/mod.rs index abf63e3..e86f42f 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -84,6 +84,30 @@ where } } +struct Builder(Nitrocli); + +impl Builder { + /// Set the model to use. + fn model(mut self, model: nitrokey::Model) -> Self { + self.0.model = Some(model); + self + } + + /// Set the password to use for certain operations. + fn password

(mut self, password: P) -> Self + where + P: Into, + { + self.0.password = Some(password.into()); + self + } + + /// Build the final `Nitrocli` object. + fn build(self) -> Nitrocli { + self.0 + } +} + struct Nitrocli { model: Option, admin_pin: Option, @@ -105,18 +129,8 @@ impl Nitrocli { } } - pub fn with_model(model: M) -> Self - where - M: Into, - { - Self { - model: Some(model.into()), - admin_pin: Some(nitrokey::DEFAULT_ADMIN_PIN.into()), - user_pin: Some(nitrokey::DEFAULT_USER_PIN.into()), - new_admin_pin: None, - new_user_pin: None, - password: Some("1234567".into()), - } + pub fn make() -> Builder { + Builder(Self::new()) } pub fn admin_pin(&mut self, pin: impl Into) { -- cgit v1.2.1