Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,6 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
env:
cache-name: cache-artifacts
with:
path: ~/.julia/artifacts
key: ${{ runner.os }}-test-${{ env.cache-name }}-${{ hashFiles('**/Project.toml') }}
restore-keys: |
${{ runner.os }}-test-${{ env.cache-name }}-
${{ runner.os }}-test-
${{ runner.os }}-
- uses: julia-actions/julia-buildpkg@v1
- uses: julia-actions/julia-runtest@v1
- uses: julia-actions/julia-processcoverage@v1
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "StructHelpers"
uuid = "4093c41a-2008-41fd-82b8-e3f9d02b504f"
authors = ["Jan Weidner <jw3126@gmail.com> and contributors"]
version = "1.3.1"
version = "1.4.0"

[deps]
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down
43 changes: 39 additions & 4 deletions src/StructHelpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,22 +315,29 @@ end
function enum_from_string end
function enum_from_symbol end
function string_from_enum(x)::String
string_from_enum_generic(x)
end
function string_from_enum_generic(x)::String
string(x)
end

function symbol_from_enum(x)::Symbol
Symbol(string_from_enum(x))
symbol_from_enum_generic(x)
end
function symbol_from_enum_generic(x)::Symbol
Symbol(string_from_enum_generic(x))
end

function def_enum_from_string(T)::Expr
body = def_symbol_or_enum_from_string_body(string_from_enum, T)
body = def_enum_from_string_or_symbol_body(string_from_enum, T)
:(
function $SH.enum_from_string(::Type{$T}, s::String)::$T
$body
end
)
end
function def_enum_from_symbol(T)::Expr
body = def_symbol_or_enum_from_string_body(QuoteNode∘symbol_from_enum, T)
body = def_enum_from_string_or_symbol_body(QuoteNode∘symbol_from_enum, T)
:(
function $SH.enum_from_symbol(::Type{$T}, s::Symbol)::$T
$body
Expand All @@ -348,7 +355,33 @@ end
throw(ArgumentError(msg))
end

function def_symbol_or_enum_from_string_body(f,T)
function def_symbol_or_string_from_enum_body(s_from_enum_generic, T)::Expr
default = :($s_from_enum_generic(obj))
matcharms = [
:(obj === $inst) => QuoteNode(s_from_enum_generic(inst)) for inst in instances(T)
]
ifelsechain(matcharms, default)
end

function def_symbol_from_enum(T)::Expr
body = def_symbol_or_string_from_enum_body(symbol_from_enum_generic, T)
:(
function $SH.symbol_from_enum(obj::$T)::Symbol
$body
end
)
end

function def_string_from_enum(T)::Expr
body = def_symbol_or_string_from_enum_body(string_from_enum_generic, T)
:(
function $SH.string_from_enum(obj::$T)::String
$body
end
)
end

function def_enum_from_string_or_symbol_body(f,T)
err = :($throw_no_matching_instance($f,$T,s))
matcharms = [
:(s === $(f(inst))) => inst for inst in instances(T)
Expand Down Expand Up @@ -440,6 +473,8 @@ macro enumbatteries(T, kw...)
push!(ret.args, :(import StructHelpers as $SH))
push!(ret.args, def_enum_from_symbol(TT))
push!(ret.args, def_enum_from_string(TT))
push!(ret.args, def_symbol_from_enum(TT))
push!(ret.args, def_string_from_enum(TT))
if nt.string_conversion
ex1 = :(Base.convert(::Type{$TT}, s::AbstractString) = $SH.enum_from_string($TT, String(s)))
ex2 = :($T(s::AbstractString) = $SH.enum_from_string($TT, String(s)))
Expand Down
Loading