1 /** 2 * Definition for the provider interface 3 * 4 * Authors: Tristan Brice Velloza Kildaire (deavmi) 5 */ 6 module hummus.provider; 7 8 version(unittest) 9 { 10 import gogga.mixins; 11 import std.string : format; 12 } 13 14 import niknaks.functional : Optional; 15 16 /** 17 * Describes a provider of 18 * values which, when requested 19 * by a name will yield the 20 * corresponding value 21 */ 22 public interface Provider 23 { 24 /** 25 * The implementation method for a provider. 26 * This must return `true` when an entry by 27 * the name of `n` is found and then set its 28 * associated value via `v_out`. Otherwise, 29 * `false` must be returned. 30 * 31 * Params: 32 * n = the name being queried 33 * v_out = the associated value (if found) 34 * Returns: `true` if found, `false` otherwise 35 */ 36 protected bool provideImpl(string n, ref string v_out); 37 38 /** 39 * Provides us the value that maps 40 * to the given name 41 * 42 * Params: 43 * name = the name of the value 44 * to lookup 45 * Returns: an `Optional` containing 46 * the value (if a mapping exists) 47 */ 48 public final Optional!(string) provide(string name) 49 { 50 version(unittest) 51 DEBUG(format("Looking up configuration entry for '%s'...", name)); 52 53 string _v; 54 if (!provideImpl(name, _v)) 55 { 56 version(unittest) 57 ERROR(format("No value mapping for '%s'", name)); 58 return Optional!(string).empty(); 59 } 60 61 version(unittest) 62 INFO(format("Mapped name '%s' to value '%s'", name, _v)); 63 return Optional!(string)(_v); 64 } 65 }