From 7e2adff71e293eef2570a6a8e43127bfab569cb3 Mon Sep 17 00:00:00 2001 From: Daniel Mueller Date: Sat, 31 Aug 2019 10:34:34 -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. --- nitrocli/src/tests/mod.rs | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'nitrocli/src/tests/mod.rs') diff --git a/nitrocli/src/tests/mod.rs b/nitrocli/src/tests/mod.rs index 1e2fe26..bc76d6d 100644 --- a/nitrocli/src/tests/mod.rs +++ b/nitrocli/src/tests/mod.rs @@ -76,6 +76,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, @@ -97,18 +121,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.3