• R/O
  • HTTP
  • SSH
  • HTTPS

提交

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

CLI interface to medialist (fossil mirror)


Commit MetaInfo

修订版cd3f096cf3e950eb716945cb852ccb834e7f91b0 (tree)
时间2023-03-14 10:15:17
作者mio <stigma@disr...>
Commitermio

Log Message

Add nothrow version of ml_send_command

FossilOrigin-Name: 5d8042cc5ef8fb8df4e39a86b152a293507d05c2f095fcccc84e339668dd336d

更改概述

差异

--- a/medialist.d
+++ b/medialist.d
@@ -147,14 +147,14 @@ struct MediaListItem
147147 /// Returns: A string containing the TSV representation of *item*.
148148 ///
149149 string tsvRepresentation() const {
150- import std.format : format;
151-
152- return "%s\t%s\t%s\t%s\t%s\t%s".format(this.title,
153- this.progress,
154- this.status,
155- this.startDate,
156- this.endDate,
157- this.lastUpdated);
150+ import std.format : format;
151+
152+ return "%s\t%s\t%s\t%s\t%s\t%s".format(this.title,
153+ this.progress,
154+ this.status,
155+ this.startDate,
156+ this.endDate,
157+ this.lastUpdated);
158158 }
159159 }
160160
@@ -221,7 +221,9 @@ enum MLCommand
221221 /// `["(Optional) Item ID", "(Optional) Item ID", ...]`
222222 ///
223223 /// If no there are no "Item ID" values in `args`, then the entire
224- /// list is irreversibly deleted.
224+ /// list is irreversibly deleted. **NOTE**: This functionality has
225+ /// been deprecated from version 0.2.0, it'll be removed in a later
226+ /// version.
225227 ///
226228 delete_,
227229 ///
@@ -455,7 +457,7 @@ MediaList* ml_open_list(string filePath, out MLError error) nothrow
455457 ///
456458 /// See_Also: ml_open_list
457459 ///
458-void ml_free_list(MediaList* list)
460+void ml_free_list(MediaList* list) nothrow
459461 {
460462 import core.memory : GC;
461463
@@ -1018,7 +1020,7 @@ MediaListItem[] ml_fetch_items(MediaList* list, size_t[] ids ...)
10181020 /// ml_fetch_item, ml_fetch_all
10191021 ///
10201022 MediaListItem[] ml_fetch_items(MediaList* list, out MLError err,
1021- size_t[] ids ...) nothrow
1023+ size_t[] ids ...) nothrow
10221024 {
10231025 import core.stdc.errno : errno, EACCES;
10241026
@@ -1190,8 +1192,7 @@ MediaListItem[] ml_fetch_all(MediaList* list, out MLError error) nothrow
11901192 ///
11911193 /// This is the main way to interact with a MediaList. The value of
11921194 /// *args* will depend on the value of *command*. See `MLCommand`
1193-/// for more information. If there are any errors in the way *args*
1194-/// is formatted, `MLError.invalidArgs` will be returned.
1195+/// for more information.
11951196 ///
11961197 /// Params:
11971198 /// list = The MediaList to perform the action on.
@@ -1201,33 +1202,68 @@ MediaListItem[] ml_fetch_all(MediaList* list, out MLError error) nothrow
12011202 /// Returns: If no error occurred, then `MLError.success` will be
12021203 /// returned. Otherwise, a value of `MLError`.
12031204 ///
1205+/// Throws: `MLException` if there was an error in the way the *args* were
1206+/// formatted.
1207+///
12041208 /// See_Also:
12051209 /// MLCommand
12061210 ///
1207-MLError ml_send_command(MediaList* list, MLCommand command, string[] args)
1211+void ml_send_command(MediaList* list, MLCommand command, string[] args)
12081212 {
1209- MLError res;
1213+ switch (command)
1214+ {
1215+ case MLCommand.add:
1216+ _ml_add(list, args);
1217+ break;
1218+ case MLCommand.delete_:
1219+ _ml_delete(list, args);
1220+ break;
1221+ case MLCommand.update:
1222+ _ml_update(list, args);
1223+ break;
1224+ default:
1225+ throw new MLException(MLError.unknownCommand);
1226+ }
1227+}
12101228
1229+///
1230+/// Perform a *command* on *list*.
1231+///
1232+/// This is the main way to interact with a MediaList. The value of
1233+/// *args* will depend on the value of *command*. See `MLCommand`
1234+/// for more information. If there are any errors, *error* will be
1235+/// set to a value other than `MLError.success`.
1236+///
1237+/// Params:
1238+/// list = The MediaList to perform the action on.
1239+/// command = The type of action to perform.
1240+/// args = The arguments that *command* will use.
1241+/// error = The out-parameter for determining any errrors.
1242+///
1243+/// See_Also:
1244+/// MLCommand
1245+///
1246+void ml_send_command(MediaList* list, MLCommand command, string[] args,
1247+ out MLError error) nothrow
1248+{
12111249 switch (command)
12121250 {
12131251 case MLCommand.add:
1214- res = _ml_add(list, args);
1252+ _ml_add(list, args, error);
12151253 break;
12161254 case MLCommand.delete_:
1217- res = _ml_delete(list, args);
1255+ _ml_delete(list, args, error);
12181256 break;
12191257 case MLCommand.update:
1220- res = _ml_update(list, args);
1258+ _ml_update(list, args, error);
12211259 break;
12221260 default:
1223- res = MLError.unknownCommand;
1261+ error = MLError.unknownCommand;
12241262 break;
12251263 }
1226-
1227- return res;
12281264 }
12291265
1230-private MLError _ml_add(MediaList* list, string[] args)
1266+private void _ml_add(MediaList* list, string[] args)
12311267 {
12321268 string title;
12331269 string progress = "-/-";
@@ -1236,7 +1272,7 @@ private MLError _ml_add(MediaList* list, string[] args)
12361272 DateTime currentDate = cast(DateTime)Clock.currTime;
12371273
12381274 if (args.length < 1)
1239- return MLError.invalidArgs;
1275+ throw new MLException(MLError.invalidArgs);
12401276
12411277 title = args[0];
12421278
@@ -1247,7 +1283,7 @@ private MLError _ml_add(MediaList* list, string[] args)
12471283 status = (args[2] is null) ? "UNKNOWN" : args[2];
12481284
12491285 if (true == list.isOpen)
1250- return MLError.fileAlreadyOpen;
1286+ throw new MLException(MLError.fileAlreadyOpen);
12511287
12521288 size_t[2][6] headerPositions = _ml_get_header_positions(list);
12531289 int currentIndent = 0;
@@ -1275,8 +1311,8 @@ private MLError _ml_add(MediaList* list, string[] args)
12751311 listFile.write(status);
12761312 break;
12771313 case MLHeaders.lastUpdated:
1278- listFile.writef("%d-%02d-%02d", currentDate.year, currentDate.month,
1279- currentDate.day);
1314+ listFile.writef("%d-%02d-%02d", currentDate.year,
1315+ currentDate.month, currentDate.day);
12801316 break;
12811317 default:
12821318 break;
@@ -1284,8 +1320,25 @@ private MLError _ml_add(MediaList* list, string[] args)
12841320 }
12851321
12861322 listFile.write("\n");
1323+}
12871324
1288- return MLError.success;
1325+private void _ml_add(MediaList* list, string[] args, out MLError error) nothrow
1326+{
1327+ import core.stdc.errno : errno, EACCES;
1328+
1329+ error = MLError.success;
1330+
1331+ try {
1332+ _ml_add(list, args);
1333+ } catch (MLException e) {
1334+ error = e.error;
1335+ } catch (StdioException e) {
1336+ error = (errno == EACCES) ?
1337+ MLError.permissionError :
1338+ MLError.unspecifiedError;
1339+ } catch (Exception e) {
1340+ error = MLError.unspecifiedError;
1341+ }
12891342 }
12901343
12911344 /**
@@ -1311,21 +1364,21 @@ private size_t[] _ml_conv_sort_num_list(const string[] args)
13111364 return ids;
13121365 }
13131366
1314-private MLError _ml_delete(MediaList* list, string[] args)
1367+private void _ml_delete(MediaList* list, string[] args)
13151368 {
13161369 if (true == list.isOpen)
1317- return MLError.fileAlreadyOpen;
1370+ throw new MLException(MLError.fileAlreadyOpen);
13181371
13191372 if (0 == args.length) {
1320- // TODO: Use system recycle bin (6518ca25e4)
1373+ // NOTE: This will be removed in a version after 0.2.0.
13211374 remove(list.filePath);
1322- return MLError.success;
1375+ return;
13231376 }
13241377
13251378 size_t[] ids = _ml_conv_sort_num_list(args);
13261379
13271380 if (null is ids)
1328- return MLError.invalidArgs;
1381+ throw new MLException(MLError.invalidArgs);
13291382
13301383 File listFile = File(list.filePath);
13311384 list.isOpen = true;
@@ -1383,17 +1436,34 @@ private MLError _ml_delete(MediaList* list, string[] args)
13831436 tempFile.close();
13841437
13851438 remove(tempFilePath);
1439+}
1440+
1441+private void _ml_delete(MediaList* list, string[] args, out MLError error) nothrow
1442+{
1443+ import core.stdc.errno : errno, EACCES;
13861444
1387- return MLError.success;
1445+ error = MLError.success;
1446+
1447+ try {
1448+ _ml_delete(list, args);
1449+ } catch (MLException me) {
1450+ error = me.error;
1451+ } catch (StdioException se) {
1452+ error = (errno == EACCES) ?
1453+ MLError.permissionError :
1454+ MLError.unspecifiedError;
1455+ } catch (Exception e) {
1456+ error = MLError.unspecifiedError;
1457+ }
13881458 }
13891459
1390-private MLError _ml_update(MediaList* list, string[] args)
1460+private void _ml_update(MediaList* list, string[] args)
13911461 {
13921462 if (list.isOpen)
1393- return MLError.fileAlreadyOpen;
1463+ throw new MLException(MLError.fileAlreadyOpen);
13941464
13951465 if (2 > args.length)
1396- return MLError.invalidArgs;
1466+ throw new MLException(MLError.invalidArgs);
13971467
13981468 string title = null;
13991469 string progress = null;
@@ -1406,7 +1476,7 @@ private MLError _ml_update(MediaList* list, string[] args)
14061476 try {
14071477 id = to!size_t(args[0]);
14081478 } catch (Exception e) {
1409- return MLError.invalidArgs;
1479+ throw new MLException(MLError.invalidArgs);
14101480 }
14111481
14121482 foreach(string arg; args) {
@@ -1569,8 +1639,23 @@ private MLError _ml_update(MediaList* list, string[] args)
15691639 listFile.write(line);
15701640 }
15711641 listFile.flush();
1642+}
15721643
1573- return MLError.success;
1644+private void _ml_update(MediaList* list, string[] args, out MLError error) nothrow
1645+{
1646+ error = MLError.success;
1647+
1648+ try {
1649+ _ml_update(list, args);
1650+ } catch (MLException me) {
1651+ error = me.error;
1652+ } catch (StdioException se) {
1653+ error = (errno == EACCESS) ?
1654+ MLError.permissionError :
1655+ MLError.unspecifiedError;
1656+ } catch (Exception e) {
1657+ error = MLError.unspecifiedError;
1658+ }
15741659 }
15751660
15761661 private enum MLHeaders