Add in, @nogc and @safe attributes to functions and parameters

This commit is contained in:
Jeremy Baxter 2023-05-11 13:25:08 +12:00
parent f2e056bf06
commit dddf0db33f

48
esv.d
View file

@ -115,7 +115,7 @@ class EsvAPI
string extraParameters; string extraParameters;
int delegate(size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) onProgress; int delegate(size_t dlTotal, size_t dlNow, size_t ulTotal, size_t ulNow) onProgress;
string tmpDir; string tmpDir;
this(const string key) this(in string key)
{ {
this._url = ESVAPI_URL; this._url = ESVAPI_URL;
this._key = key; this._key = key;
@ -128,19 +128,19 @@ class EsvAPI
/* /*
* Returns the API URL currently in use. * Returns the API URL currently in use.
*/ */
final string getURL() const nothrow final string getURL() const nothrow @nogc @safe
{ {
return _url; return _url;
} }
/* /*
* If the url argument is a valid HTTP URL, sets the API URL currently in use * If the url argument is a valid HTTP URL, sets the API URL currently in use
* to the given url argument. Otherwise, throws a UrlException. * to the given url argument. Otherwise, throws an EsvException .
*/ */
final void setURL(const string url) final void setURL(in string url) @safe
{ {
auto matches = url.matchAll("^https?://.+\\..+(/.+)?"); auto matches = url.matchAll("^https?://.+\\..+(/.+)?");
if (matches.empty) if (matches.empty)
throw new UrlException("Invalid URL format"); throw new EsvException("Invalid URL format");
else else
this._url = url; this._url = url;
} }
@ -148,14 +148,14 @@ class EsvAPI
* Returns the API authentication key that was given when the API object was instantiated. * Returns the API authentication key that was given when the API object was instantiated.
* This authentication key cannot be changed after instantiation. * This authentication key cannot be changed after instantiation.
*/ */
final string getKey() const nothrow final string getKey() const nothrow @nogc @safe
{ {
return _key; return _key;
} }
/* /*
* Returns the API authentication key currently in use. * Returns the API authentication key currently in use.
*/ */
final string getMode() const nothrow final string getMode() const nothrow @nogc @safe
{ {
return _mode; return _mode;
} }
@ -165,7 +165,7 @@ class EsvAPI
* If the mode argument is not one of those, * If the mode argument is not one of those,
* then this function will do nothing. * then this function will do nothing.
*/ */
final void setMode(const string mode) nothrow final void setMode(in string mode) nothrow @nogc @safe
{ {
foreach (string m; ["text", "html"] ) foreach (string m; ["text", "html"] )
{ {
@ -180,7 +180,7 @@ class EsvAPI
* Returns true if the argument book is a valid book of the Bible. * Returns true if the argument book is a valid book of the Bible.
* Otherwise, returns false. * Otherwise, returns false.
*/ */
final bool validateBook(const string book) const nothrow final bool validateBook(in string book) const nothrow @safe
{ {
foreach (string b; ESVAPI_BIBLE_BOOKS) foreach (string b; ESVAPI_BIBLE_BOOKS)
{ {
@ -193,9 +193,9 @@ class EsvAPI
* Returns true if the argument book is a valid verse format. * Returns true if the argument book is a valid verse format.
* Otherwise, returns false. * Otherwise, returns false.
*/ */
final bool validateVerse(const string verse) const final bool validateVerse(in string verse) const @safe
{ {
bool attemptRegex(const string re) const bool attemptRegex(string re) const @safe
{ {
auto matches = verse.matchAll(re); auto matches = verse.matchAll(re);
return !matches.empty; return !matches.empty;
@ -220,12 +220,12 @@ class EsvAPI
* *
* Example: getVerses("John", "3:16-21") * Example: getVerses("John", "3:16-21")
*/ */
final string getVerses(const string book, const string verse) const final string getVerses(in string book, in string verse) const
{ {
if (!this.validateBook(book)) if (!this.validateBook(book))
throw new EsvPassageException("Invalid book"); throw new EsvException("Invalid book");
if (!this.validateVerse(verse)) if (!this.validateVerse(verse))
throw new EsvPassageException("Invalid verse format"); throw new EsvException("Invalid verse format");
string apiURL = format!"%s/%s/?q=%s+%s%s%s"(this._url, this._mode, string apiURL = format!"%s/%s/?q=%s+%s%s%s"(this._url, this._mode,
book.capitalize().replaceAll(regex("_"), "+"), verse, this.assembleParameters(), this.extraParameters); book.capitalize().replaceAll(regex("_"), "+"), verse, this.assembleParameters(), this.extraParameters);
@ -250,12 +250,12 @@ class EsvAPI
* *
* Example: getVerses("John", "3:16-21") * Example: getVerses("John", "3:16-21")
*/ */
final string getAudioVerses(const string book, const string verse) final string getAudioVerses(in string book, in string verse) const
{ {
if (!this.validateBook(book)) if (!this.validateBook(book))
throw new EsvPassageException("Invalid book"); throw new EsvException("Invalid book");
if (!this.validateVerse(verse)) if (!this.validateVerse(verse))
throw new EsvPassageException("Invalid verse format"); throw new EsvException("Invalid verse format");
string apiURL = format!"%s/audio/?q=%s+%s"(this._url, book.capitalize().replaceAll(regex("_"), "+"), verse); string apiURL = format!"%s/audio/?q=%s+%s"(this._url, book.capitalize().replaceAll(regex("_"), "+"), verse);
auto request = HTTP(apiURL); auto request = HTTP(apiURL);
@ -272,7 +272,7 @@ class EsvAPI
tmpFile.write(response); tmpFile.write(response);
return tmpFile; return tmpFile;
} }
private string assembleParameters() const private string assembleParameters() const @safe
{ {
string params = ""; string params = "";
string addParam(string param, string value) const string addParam(string param, string value) const
@ -317,7 +317,7 @@ struct EsvAPIOptions
bool[string] boolOpts; bool[string] boolOpts;
int[string] intOpts; int[string] intOpts;
string indent_using; string indent_using;
void setDefaults() nothrow void setDefaults() nothrow @safe
{ {
this.boolOpts["include_passage_references"] = true; this.boolOpts["include_passage_references"] = true;
this.boolOpts["include_verse_numbers"] = true; this.boolOpts["include_verse_numbers"] = true;
@ -341,15 +341,7 @@ struct EsvAPIOptions
} }
} }
class UrlException : Exception class EsvException : Exception
{
this(string msg, string file = __FILE__, size_t line = __LINE__) @safe pure
{
super(msg, file, line);
}
}
class EsvPassageException : Exception
{ {
this(string msg, string file = __FILE__, size_t line = __LINE__) @safe pure this(string msg, string file = __FILE__, size_t line = __LINE__) @safe pure
{ {