Skip to content

Commit

Permalink
Refactor: Convert some Option[string] to string (exercism#90)
Browse files Browse the repository at this point in the history
There isn't much reason to use `Option[string]` for `exercise` and
`probSpecsDir` - we can just test whether the string is empty.

Note that (both before and after this commit) if we run any of:
    canonical_data_syncer -p=
    canonical_data_syncer -p
    canonical_data_syncer -e=
    canonical_data_syncer -e

Then we see an error message like:
    Error: '-p' was given without a value
  • Loading branch information
ee7 authored Oct 30, 2020
1 parent 17902af commit 6b0ba0c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 17 deletions.
16 changes: 8 additions & 8 deletions src/cli.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[options, os, strformat, strutils, terminal]
import std/[os, strformat, strutils, terminal]
import pkg/[cligen/parseopt3]

type
Expand All @@ -13,11 +13,11 @@ type
verDetailed = "detailed"

Conf* = object
exercise*: Option[string]
exercise*: string
check*: bool
mode*: Mode
verbosity*: Verbosity
probSpecsDir*: Option[string]
probSpecsDir*: string
offline*: bool

Opt = enum
Expand Down Expand Up @@ -107,11 +107,11 @@ proc prefix(kind: CmdLineKind): string =

proc initConf: Conf =
result = Conf(
exercise: none(string),
exercise: "",
check: false,
mode: modeChoose,
verbosity: verNormal,
probSpecsDir: none(string),
probSpecsDir: "",
offline: false,
)

Expand Down Expand Up @@ -174,15 +174,15 @@ proc processCmdLine*: Conf =
of cmdLongOption, cmdShortOption:
case parseOption(kind, key, val)
of optExercise:
result.exercise = some(val)
result.exercise = val
of optCheck:
result.check = true
of optMode:
result.mode = parseVal[Mode](kind, key, val)
of optVerbosity:
result.verbosity = parseVal[Verbosity](kind, key, val)
of optProbSpecsDir:
result.probSpecsDir = some(val)
result.probSpecsDir = val
of optOffline:
result.offline = true
of optHelp:
Expand All @@ -199,5 +199,5 @@ proc processCmdLine*: Conf =
of cmdEnd, cmdError:
discard

if result.offline and result.probSpecsDir.isNone():
if result.offline and result.probSpecsDir.len == 0:
showError(&"'{list(optOffline)}' was given without passing '{list(optProbSpecsDir)}'")
8 changes: 4 additions & 4 deletions src/probspecs.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[json, options, os, osproc, sequtils, strformat, strscans, strutils]
import std/[json, os, osproc, sequtils, strformat, strscans, strutils]
import cli, logger

type
Expand Down Expand Up @@ -99,7 +99,7 @@ proc initProbSpecsExercise(repoExercise: ProbSpecsRepoExercise): ProbSpecsExerci

proc findProbSpecsExercises(repo: ProbSpecsRepo, conf: Conf): seq[ProbSpecsExercise] =
for repoExercise in repo.exercisesWithCanonicalData():
if conf.exercise.isNone or conf.exercise.get() == repoExercise.slug:
if conf.exercise.len == 0 or conf.exercise == repoExercise.slug:
result.add(initProbSpecsExercise(repoExercise))

template withDir(dir: string; body: untyped): untyped =
Expand Down Expand Up @@ -175,8 +175,8 @@ proc validate(probSpecsRepo: ProbSpecsRepo) =
&"up-to-date: '{probSpecsDir}'")

proc findProbSpecsExercises*(conf: Conf): seq[ProbSpecsExercise] =
if conf.probSpecsDir.isSome():
let probSpecsRepo = ProbSpecsRepo(dir: conf.probSpecsDir.get())
if conf.probSpecsDir.len > 0:
let probSpecsRepo = ProbSpecsRepo(dir: conf.probSpecsDir)
if not conf.offline:
probSpecsRepo.validate()
result = probSpecsRepo.findProbSpecsExercises(conf)
Expand Down
4 changes: 2 additions & 2 deletions src/tracks.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import std/[json, options, os, sets]
import std/[json, os, sets]
import pkg/parsetoml
import cli

Expand Down Expand Up @@ -79,7 +79,7 @@ proc newTrackExercise(exercise: TrackRepoExercise): TrackExercise =

proc findTrackExercises(repo: TrackRepo, conf: Conf): seq[TrackExercise] =
for repoExercise in repo.exercises:
if conf.exercise.isNone or conf.exercise.get() == repoExercise.slug:
if conf.exercise.len == 0 or conf.exercise == repoExercise.slug:
result.add(newTrackExercise(repoExercise))

proc findTrackExercises*(conf: Conf): seq[TrackExercise] =
Expand Down
6 changes: 3 additions & 3 deletions tests/test_probspecs.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This module contains tests for `src/probspecs.nim`
import std/[json, options, os, osproc, strformat, unittest]
import std/[json, os, osproc, strformat, unittest]
import cli, probspecs

type
Expand All @@ -23,8 +23,8 @@ proc main =

let probSpecsDir =
case ps
of psFresh: none(string)
of psExisting: some(existingDir)
of psFresh: ""
of psExisting: existingDir

let conf = Conf(probSpecsDir: probSpecsDir)
let probSpecsExercises = findProbSpecsExercises(conf)
Expand Down

0 comments on commit 6b0ba0c

Please sign in to comment.