Skip to content

Commit

Permalink
Add definitions for internal TOML methods in Base
Browse files Browse the repository at this point in the history
Despite being internal, these interfaces are used in a number of places
downstream, so it's best for TOML's parser type to have their interface
defined.
  • Loading branch information
topolarity committed Sep 30, 2024
1 parent 6efad7c commit d4ac343
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions stdlib/TOML/src/TOML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Internals
end

# https://github.com/JuliaLang/julia/issues/36605
readstring(f::AbstractString) = isfile(f) ? read(f, String) : error(repr(f), ": No such file")
_readstring(f::AbstractString) = isfile(f) ? read(f, String) : error(repr(f), ": No such file")

"""
Parser()
Expand Down Expand Up @@ -55,9 +55,9 @@ Parse file `f` and return the resulting table (dictionary). Throw a
See also [`TOML.tryparsefile`](@ref).
"""
parsefile(f::AbstractString) =
Internals.parse(Internals.Parser{Dates}(readstring(f); filepath=abspath(f)))
Internals.parse(Internals.Parser{Dates}(_readstring(f); filepath=abspath(f)))
parsefile(p::Parser, f::AbstractString) =
Internals.parse(Internals.reinit!(p._p, readstring(f); filepath=abspath(f)))
Internals.parse(Internals.reinit!(p._p, _readstring(f); filepath=abspath(f)))

"""
tryparsefile(f::AbstractString)
Expand All @@ -69,9 +69,9 @@ Parse file `f` and return the resulting table (dictionary). Return a
See also [`TOML.parsefile`](@ref).
"""
tryparsefile(f::AbstractString) =
Internals.tryparse(Internals.Parser{Dates}(readstring(f); filepath=abspath(f)))
Internals.tryparse(Internals.Parser{Dates}(_readstring(f); filepath=abspath(f)))
tryparsefile(p::Parser, f::AbstractString) =
Internals.tryparse(Internals.reinit!(p._p, readstring(f); filepath=abspath(f)))
Internals.tryparse(Internals.reinit!(p._p, _readstring(f); filepath=abspath(f)))

"""
parse(x::Union{AbstractString, IO})
Expand Down Expand Up @@ -135,4 +135,17 @@ supported type.
"""
const print = Internals.Printer.print

public Parser, parsefile, tryparsefile, parse, tryparse, ParserError, print

# These methods are private Base interfaces, but we do our best to support them over
# the TOML stdlib types anyway to minimize downstream breakage.
Base.TOMLCache(p::Parser) = Base.TOMLCache(p._p, Dict{String, Base.CachedTOMLDict}())
Base.TOMLCache(p::Parser, d::Base.CachedTOMLDict) = Base.TOMLCache(p._p, d)
Base.TOMLCache(p::Parser, d::Dict{String, Dict{String, Any}}) = Base.TOMLCache(p._p, d)

Internals.reinit!(p::Parser, str::String; filepath::Union{Nothing, String}=nothing) =
Internals.reinit!(p._p, str; filepath)
Internals.parse(p::Parser) = Internals.parse(p._p)
Internals.tryparse(p::Parser) = Internals.tryparse(p._p)

end

0 comments on commit d4ac343

Please sign in to comment.