From 9ca98ac6644e97f0c689e14e5f6f7c4b8b1e4279 Mon Sep 17 00:00:00 2001 From: "Tristan B. Velloza Kildaire" Date: Sat, 25 Mar 2023 13:37:18 +0200 Subject: [PATCH] ConnectionInfo - Updated `string getDB(string)` to `T getDB(T)(string)` which will now attempt conversion to type `T`, on failure to do so it will return `T.init` ConnInfo - Added `setDefaults(ref ConnectionInfo)` which will set defaults in the `db`, namely so far it sets the `MAXNICKLEN` to `9` as per rfc1459 Package (`birchwood.config`) - Now publically imports `setDefaults()` --- source/birchwood/config/conninfo.d | 28 ++++++++++++++++++++++++++-- source/birchwood/config/package.d | 2 +- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/source/birchwood/config/conninfo.d b/source/birchwood/config/conninfo.d index 73b9839..a4427fe 100644 --- a/source/birchwood/config/conninfo.d +++ b/source/birchwood/config/conninfo.d @@ -5,6 +5,7 @@ module birchwood.config.conninfo; import std.socket : SocketException, Address, getAddress; import birchwood.client.exceptions; +import std.conv : to, ConvException; /** * Represents the connection details for a server @@ -127,11 +128,25 @@ public shared struct ConnectionInfo db[key] = value; } - public string getDB(string key) + public T getDB(T)(string key) { + import std.stdio; + writeln("GETDB: '"~key~"' with len ", key.length); if(key in db) { - return db[key]; + /* Attempt conversion into T */ + try + { + /* Fetch and convert */ + T value = to!(T)(db[key]); + return value; + } + /* If conversion to type T fails */ + catch(ConvException e) + { + /* Return the initial value for such a paremeter */ + return T.init; + } } else { @@ -205,4 +220,13 @@ public shared struct ConnectionInfo } } +} + +public void setDefaults(ref ConnectionInfo connInfo) +{ + /* Set the `MAXNICKLEN` to a default of 9 */ + connInfo.updateDB("MAXNICKLEN", "9"); + assert(connInfo.getDB!(ulong)("MAXNICKLEN") == 9); + import std.stdio; + writeln("MAXKAK: ", connInfo.getDB!(ulong)("MAXNICKLEN")); } \ No newline at end of file diff --git a/source/birchwood/config/package.d b/source/birchwood/config/package.d index 1bb8dfd..82b1e2d 100644 --- a/source/birchwood/config/package.d +++ b/source/birchwood/config/package.d @@ -6,4 +6,4 @@ module birchwood.config; /** * Connection information */ -public import birchwood.config.conninfo : ConnectionInfo; \ No newline at end of file +public import birchwood.config.conninfo : ConnectionInfo, setDefaults; \ No newline at end of file