~singpolyma/dhall-ruby

e5560786a4ddbde7b6b1fc5e869bb3ef4e8b9126 — Stephen Paul Weber 2 years ago 1fdb39d
Allow imports to be either binary or source form
3 files changed, 22 insertions(+), 10 deletions(-)

M lib/dhall.rb
M lib/dhall/ast.rb
M test/test_resolve.rb
M lib/dhall.rb => lib/dhall.rb +16 -2
@@ 1,8 1,22 @@
# frozen_string_literal: true

module Dhall
	def self.load_raw(source)
		begin
			return from_binary(source) if source.encoding == Encoding::BINARY
		rescue
			# Parsing CBOR failed, so guess this is source text in standard UTF-8
			return load_raw(source.force_encoding("UTF-8"))
		end

		Parser.parse(source.encode("UTF-8")).value
	end
end

require "dhall/ast"
require "dhall/builtins"
require "dhall/parser"
require "dhall/binary"
require "dhall/builtins"
require "dhall/normalize"
require "dhall/parser"
require "dhall/resolve"
require "dhall/typecheck"

M lib/dhall/ast.rb => lib/dhall/ast.rb +2 -2
@@ 835,7 835,7 @@ module Dhall

	class Text < Expression
		include(ValueSemantics.for_attributes do
			value ::String, coerce: ->(s) { s.encode("utf-8") }
			value ::String, coerce: ->(s) { s.encode("UTF-8") }
		end)

		def <<(other)


@@ 1102,7 1102,7 @@ module Dhall

		class Expression
			def self.call(import_value)
				Dhall.from_binary(import_value)
				Dhall.load_raw(import_value)
			end
		end


M test/test_resolve.rb => test/test_resolve.rb +4 -6
@@ 4,9 4,7 @@ require "base64"
require "webmock/minitest"
require "minitest/autorun"

require "dhall/resolve"
require "dhall/normalize"
require "dhall/parser"
require "dhall"

class TestResolve < Minitest::Test
	def setup


@@ 187,7 185,7 @@ class TestResolve < Minitest::Test
	def test_headers
		stub_request(:get, "http://e.td/t")
			.with(headers: { "Th" => "tv" })
			.to_return(status: 200, body: "\x00")
			.to_return(status: 200, body: "\x00".b)

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,


@@ 200,7 198,7 @@ class TestResolve < Minitest::Test

	def test_ipfs
		stub_request(:get, "http://localhost:8000/ipfs/TESTCID")
			.to_return(status: 200, body: "\x00")
			.to_return(status: 200, body: "\x00".b)

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,


@@ 216,7 214,7 @@ class TestResolve < Minitest::Test
			.to_return(status: 500)

		stub_request(:get, "https://cloudflare-ipfs.com/ipfs/TESTCID")
			.to_return(status: 200, body: "\x00")
			.to_return(status: 200, body: "_")

		expr = Dhall::Import.new(
			Dhall::Import::IntegrityCheck.new,