-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
78 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#pragma once | ||
|
||
#include <llvm/Support/Error.h> | ||
|
||
#include <exception> | ||
|
||
namespace nixd { | ||
|
||
class LLVMErrorException : public std::exception { | ||
llvm::Error E; | ||
|
||
public: | ||
LLVMErrorException(llvm::Error E) : E(std::move(E)) {} | ||
|
||
llvm::Error takeError() { return std::move(E); } | ||
}; | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#include "Exception.h" | ||
|
||
#include <llvm/ADT/StringRef.h> | ||
#include <llvm/Support/JSON.h> | ||
|
||
#include <exception> | ||
|
||
namespace nixd { | ||
|
||
class JSONParseException : public LLVMErrorException { | ||
public: | ||
JSONParseException(llvm::Error E) : LLVMErrorException(std::move(E)) {} | ||
|
||
[[nodiscard]] const char *what() const noexcept override { | ||
return "JSON result cannot be parsed"; | ||
} | ||
}; | ||
|
||
class JSONSchemaException : public LLVMErrorException { | ||
public: | ||
JSONSchemaException(llvm::Error E) : LLVMErrorException(std::move(E)) {} | ||
|
||
[[nodiscard]] const char *what() const noexcept override { | ||
return "JSON schema mismatch"; | ||
} | ||
}; | ||
|
||
llvm::json::Value parse(llvm::StringRef JSON); | ||
|
||
template <class T> T fromJSON(const llvm::json::Value &V) { | ||
llvm::json::Path::Root R; | ||
T Result; | ||
if (!fromJSON(V, Result, R)) | ||
throw JSONSchemaException(R.getError()); | ||
return Result; | ||
} | ||
|
||
} // namespace nixd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "nixd/Support/JSON.h" | ||
|
||
#include <llvm/ADT/StringRef.h> | ||
|
||
llvm::json::Value nixd::parse(llvm::StringRef JSON) { | ||
llvm::Expected<llvm::json::Value> E = llvm::json::parse(JSON); | ||
if (!E) | ||
throw JSONParseException(E.takeError()); | ||
return *E; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters