Skip to content

Instantly share code, notes, and snippets.

@terasakisatoshi
Last active November 18, 2021 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terasakisatoshi/1d823708035c82352866de96abdfee17 to your computer and use it in GitHub Desktop.
Save terasakisatoshi/1d823708035c82352866de96abdfee17 to your computer and use it in GitHub Desktop.
using TOML
using Configurations
abstract type FontSize end
struct Small <: FontSize end
struct Medium <: FontSize end
struct Large <: FontSize end
@option "dial" struct Dial{T<:FontSize}
one::Vector{String}
two::Vector{String}
#three::Vector{String}
#four::Vector{String}
#five::Vector{String}
#six::Vector{String}
#seven::Vector{String}
#eight::Vector{String}
#nine::Vector{String}
#zero::Vector{String}
colon::Vector{String}
end
function n2d(dial::Dial, n::Int)
n == 0 && return getfield(dial, :zero)
n == 1 && return getfield(dial, :one)
n == 2 && return getfield(dial, :two)
n == 3 && return getfield(dial, :three)
n == 4 && return getfield(dial, :four)
n == 5 && return getfield(dial, :five)
n == 6 && return getfield(dial, :six)
n == 7 && return getfield(dial, :seven)
n == 8 && return getfield(dial, :eight)
n == 9 && return getfield(dial, :nine)
DomainError("n should satisfy 0≤ n ≤9, actual $n")
end
colon(dial::Dial) = getfield(dial, :colon)
# example
file = "MyDial.toml"
toml = TOML.parsefile(file)
d_small = from_dict(Dial{Small}, toml[string(nameof(Small))])
d_medium = from_dict(Dial{Medium}, toml[string(nameof(Medium))])
d_large = from_dict(Dial{Large}, toml[string(nameof(Large))])
# do something e.g. clock, stopwatch, countdown
@show n2d(d_small, 1)
#=
3-element Vector{String}:
"smallone"
"++++++++"
"--------"
=#
@show colon(d_medium)
#=
1-element Vector{String}:
"mediumcolon"
=#
[Large]
one = [
"largeone",
"++++++++",
"--------",
]
two = [
"largetwo",
"++++++++",
"--------",
]
colon = [
"largecolon",
]
[Medium]
one = [
"mediumone",
"++++++++",
"--------",
]
two = [
"mediumtwo",
"+++++++++",
"---------",
]
colon = [
"mediumcolon",
]
[Small]
one = [
"smallone",
"++++++++",
"--------",
]
two = [
"smalltwo",
"++++++++",
"--------",
]
colon = [
"smallcolon"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment