Skip to content

Commit

Permalink
Create context_errors.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
jakobjpeters committed Nov 7, 2024
1 parent 0b302bb commit 8f173ff
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 59 deletions.
2 changes: 1 addition & 1 deletion src/Commands.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function show(io::IO, m::Union{
output = input * "." * format(m)

render(t; input, output, open = false, ignorestatus = false,
preamble = unwrap(io, TypstString, :preamble, preamble))
preamble = unwrap(io, :preamble, preamble))
write(io, read(output))

nothing
Expand Down
48 changes: 1 addition & 47 deletions src/Strings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Typstry.Strings
"""
module Strings

import Base: IOBuffer, ==, codeunit, isvalid, iterate, ncodeunits, pointer, repr, show, showerror
import Base: IOBuffer, ==, codeunit, isvalid, iterate, ncodeunits, pointer, repr, show
using ..Typstry: enclose, join_with, unwrap
using .Docs: HTML, Text
using .Meta: isexpr, parse
Expand All @@ -25,25 +25,6 @@ using Dates:

# `Typstry`

"""
ContextError <: Exception
ContextError(::Type, ::Type, ::Symbol)
An `Exception` indicating that a [`context`](@ref) key returned a value of an incorrect type.
# Examples
```jldoctest
julia> ContextError(Mode, String, :mode)
ContextError(Mode, String, :mode)
```
"""
struct ContextError <: Exception
expected::Type
received::Type
key::Symbol
end

"""
Mode
Expand Down Expand Up @@ -238,19 +219,6 @@ context(x::Typst) = merge!(Dict(
), context(x.value))
context(::Any) = Dict{Symbol, Union{}}()

"""
show(::IO, ::MIME"text/plain", ::ContextError)
# Examples
```jldoctest
julia> show(stdout, "text/plain", ContextError(Mode, String, :mode))
ContextError(Mode, String, :mode)
```
"""
show(io::IO, ::MIME"text/plain", ce::ContextError) =
print(io, ContextError, "(", ce.expected, ", ", ce.received, ", :", ce.key, ")")

"""
show(::IO, ::MIME"text/typst", ::Union{Typst, TypstString, TypstText})
Expand Down Expand Up @@ -284,20 +252,6 @@ function show(io::IOContext, ::MIME"text/typst", t::Union{Typst, TypstString, Ty
show_typst(io, t)
end

"""
showerror(::IO, ::ContextError)
# Examples
```jldoctest
julia> showerror(stdout, ContextError(Mode, String, :mode))
ContextError: the `context` key `:mode` expected a value of type `Mode` but received `String`
```
"""
showerror(io::IO, ce::ContextError) = print(io, "ContextError: the `",
context, "` key `:", ce.key, "` expected a value of type `",
ce.expected, "` but received `", ce.received, "`")

# Internals

"""
Expand Down
8 changes: 6 additions & 2 deletions src/Typstry.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@

module Typstry

import Base: show, showerror
using .Iterators: Stateful
using PrecompileTools: @compile_workload

include("utilities.jl")

include("context_errors.jl")
export ContextError

include("Strings.jl")
using .Strings: ContextError, Mode, Typst, TypstString, TypstText, @typst_str, code, markup, math, context, show_typst
export ContextError, Mode, Typst, TypstString, TypstText, @typst_str, code, markup, math, context, show_typst
using .Strings: Mode, Typst, TypstString, TypstText, @typst_str, code, markup, math, context, show_typst
export Mode, Typst, TypstString, TypstText, @typst_str, code, markup, math, context, show_typst

include("Commands.jl")
using .Commands: TypstCommand, TypstCommandError, @typst_cmd, julia_mono, preamble, render, set_preamble, typst
Expand Down
58 changes: 58 additions & 0 deletions src/context_errors.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

"""
ContextError <: Exception
ContextError(::Type, ::Type, ::Symbol)
An `Exception` indicating that a [`context`](@ref) key returned a value of an incorrect type.
# Examples
```jldoctest
julia> ContextError(Mode, String, :mode)
ContextError(Mode, String, :mode)
```
"""
struct ContextError <: Exception
expected::Type
received::Type
key::Symbol
end

"""
show(::IO, ::MIME"text/plain", ::ContextError)
# Examples
```jldoctest
julia> show(stdout, "text/plain", ContextError(Mode, String, :mode))
ContextError(Mode, String, :mode)
```
"""
show(io::IO, ::MIME"text/plain", ce::ContextError) =
print(io, ContextError, "(", ce.expected, ", ", ce.received, ", :", ce.key, ")")

"""
showerror(::IO, ::ContextError)
# Examples
```jldoctest
julia> showerror(stdout, ContextError(Mode, String, :mode))
ContextError: the `context` key `:mode` expected a value of type `Mode` but received `String`
```
"""
showerror(io::IO, ce::ContextError) = print(io, "ContextError: the `",
context, "` key `:", ce.key, "` expected a value of type `",
ce.expected, "` but received `", ce.received, "`")

_unwrap(type, key, value) = value isa type ? value : throw(ContextError(type, typeof(value), key))

"""
unwrap(io, key::Symbol, default)
unwrap(io, type::Type, key)
"""
unwrap(io, key::Symbol, default) = _unwrap(typeof(default), key, get(io, key, default))
function unwrap(io, type::Type, key)
value = io[key]
_unwrap(type, key, value)
end
9 changes: 0 additions & 9 deletions src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,3 @@ function join_with(f, io, xs, delimeter; kwargs...)
isempty(_xs) || print(io, delimeter)
end
end

_unwrap(type, key, value::T) where T = value isa type ? value : throw(ContextError(type, T, key))

"""
unwrap(io, type, key)
unwrap(io, type, key, default)
"""
unwrap(io, type, key) = _unwrap(type, key, io[key])
unwrap(io, type, key, default) = _unwrap(type, key, get(io, key, default))

0 comments on commit 8f173ff

Please sign in to comment.