From 5dc2a12f1ec8c23b89a6143ffbcbc6a2990b09ad Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Sat, 3 Jun 2023 18:52:24 +1200 Subject: [PATCH] Refactor config parsing code in main.d, rename esv.d -> esvapi.d, rename class EsvAPI to ESVApi, makefile changes --- Makefile | 14 +++++++------- esv.d => esvapi.d | 10 ++++++---- main.d | 30 +++++++++++------------------- 3 files changed, 24 insertions(+), 30 deletions(-) rename esv.d => esvapi.d (98%) diff --git a/Makefile b/Makefile index b03e8bf..0593c8c 100644 --- a/Makefile +++ b/Makefile @@ -2,9 +2,9 @@ IMPORT = import PREFIX = /usr/local MANPREFIX = /usr/share/man -DC = ldc2 -CFLAGS = -O -I${IMPORT} -w -OBJS = main.o esv.o ini.o +DC = ldc2 +CFLAGS = -O -I${IMPORT} -release -w +OBJS = main.o esvapi.o ini.o all: esv @@ -12,17 +12,17 @@ esv: ${OBJS} ${DC} ${CFLAGS} -of$@ ${OBJS} # main executable -main.o: main.d esv.o +main.o: main.d esvapi.o ${DC} ${CFLAGS} -c main.d -of$@ -esv.o: esv.d - ${DC} ${CFLAGS} -c esv.d -of$@ +esvapi.o: esvapi.d + ${DC} ${CFLAGS} -c esvapi.d -of$@ ini.o: ${IMPORT}/dini/*.d ${DC} ${CFLAGS} -c ${IMPORT}/dini/*.d -of$@ clean: - rm -f ${PROG} ${OBJS} + rm -f esv ${OBJS} install: esv install -m755 esv ${DESTDIR}${PREFIX}/bin/esv diff --git a/esv.d b/esvapi.d similarity index 98% rename from esv.d rename to esvapi.d index 1093b07..f47c7de 100644 --- a/esv.d +++ b/esvapi.d @@ -18,6 +18,8 @@ * along with esv. If not, see . */ +module esvapi; + import std.algorithm : filter, map; import std.array : appender; import std.ascii : isAlphaNum; @@ -106,12 +108,12 @@ const string[] BIBLE_BOOKS = [ "Revelation" ]; -class EsvAPI +class ESVApi { private string _key; private string _url; private string _mode; - EsvAPIOptions opts; + ESVApiOptions opts; string extraParameters; int delegate(size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) onProgress; string tmpDir; @@ -312,7 +314,7 @@ class EsvAPI } } -struct EsvAPIOptions +struct ESVApiOptions { bool[string] boolOpts; int[string] intOpts; @@ -337,7 +339,7 @@ struct EsvAPIOptions intOpts["indent_declares"] = 40; intOpts["indent_psalm_doxology"] = 30; intOpts["line_length"] = 0; - indent_using = "space"; + indent_using = "space"; } } diff --git a/main.d b/main.d index 4d3c6bf..624f8bd 100644 --- a/main.d +++ b/main.d @@ -20,6 +20,7 @@ import std.conv : to, ConvException; import std.file : exists, write, FileException; +import std.format : format; import std.getopt : getopt, GetOptException, config; import std.path : baseName, expandTilde, isValidPath; import std.process : environment, executeShell; @@ -27,7 +28,7 @@ import std.regex : regex, matchFirst, replaceAll, replaceFirst; import std.stdio : writef, writeln, writefln, stderr; import std.string : splitLines; -import esv; +import esvapi; import dini; enum VERSION = "0.2.0"; @@ -164,7 +165,7 @@ key = " ~ DEFAULT_APIKEY ~ " panic("API key not present in configuration file; cannot proceed"); // Initialise API object and validate the book and verse - EsvAPI esv = new EsvAPI(apiKey); + ESVApi esv = new ESVApi(apiKey); if (!esv.validateBook(args[1].extractBook())) panic("book '" ~ args[1] ~ "' does not exist"); if (!esv.validateVerse(args[2])) @@ -208,17 +209,17 @@ key = " ~ DEFAULT_APIKEY ~ " foreach (string key; ["footnotes", "headings", "passage_references", "verse_numbers"]) { try { esv.opts.boolOpts["include_" ~ key] = - returnValid("true", iniData["passage"].getKey(key)).catchConvException( - (in ConvException ex, in char[] str) - { - panic(configPath ~ ": value '" ~ cast(string)str ~ - "' is not convertible to a boolean value; must be either 'true' or 'false'"); - } - ); + returnValid("true", iniData["passage"].getKey(key)).to!bool(); + } catch (ConvException e) { + panic( + format!"%s: key '%s' of section 'passage' is not a boolean value (must be either 'true' or 'false')"( + configPath, key + ) + ); } catch (IniException e) {} // just do nothing; use the default settings } // Get line_length ([passage]) - try esv.opts.intOpts["line_length"] = returnValid("0", iniData["passage"].getKey("line_length")).to!int(); + try esv.opts.intOpts["line_length"] = returnValid("0", iniData["passage"].getKey("line_length")).to!int(); catch (ConvException e) { panic(configPath ~ ": value '" ~ iniData["passage"].getKey("line_length") ~ "' is not convertible to an integer value; must be a non-decimal number"); @@ -274,12 +275,3 @@ string extractBook(in string book) @safe { return book.replaceAll(regex("[-_]"), " "); } - -bool catchConvException(in char[] sb, void delegate(in ConvException ex, in char[] str) @system catchNet) -{ - try return sb.to!bool(); - catch (ConvException e) { - catchNet(e, sb); - return false; - } -}