some more early thoughts about syntax

This commit is contained in:
Blue 2023-08-31 17:06:02 -03:00
parent 0c40906eca
commit 60b2220b97
Signed by: blue
GPG Key ID: 9B203B252A63EE38
7 changed files with 170 additions and 5 deletions

View File

@ -1,10 +1,12 @@
set(SOURCES set(SOURCES
main.cpp main.cpp
project.cpp project.cpp
dependency.cpp
) )
set(HEADERS set(HEADERS
project.h project.h
dependency.cpp
) )
target_sources(${PROJECT_NAME} PRIVATE ${SOURCES}) target_sources(${PROJECT_NAME} PRIVATE ${SOURCES})

25
src/dependency.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "dependency.h"
Dependency::Dependency(
const std::string& path,
Type type,
const std::optional<std::string>& name,
const std::optional<std::string>& version
):
path(path),
type(type),
name(name),
version(version)
{}
Dependency::Type Dependency::getType() const {
return type;
}
std::optional<std::string> Dependency::getName() const {
return name;
}
std::optional<std::string> Dependency::getVersion() const {
return version;
}

31
src/dependency.h Normal file
View File

@ -0,0 +1,31 @@
#pragma once
#include <string>
#include <optional>
class Dependency {
public:
enum class Type {
automatic,
simple,
mason
};
Dependency(
const std::string& path,
Type type = Type::automatic,
const std::optional<std::string>& name = std::nullopt,
const std::optional<std::string>& version = std::nullopt
);
Type getType() const;
std::optional<std::string> getName() const;
std::optional<std::string> getVersion() const;
const std::string path;
private:
Type type;
std::optional<std::string> name;
std::optional<std::string> version;
};

View File

@ -21,6 +21,7 @@ int main(int argc, char *argv[]) {
return -1; return -1;
} else { } else {
std::cout << "successfully parsed project " << root.getName() << std::endl; std::cout << "successfully parsed project " << root.getName() << std::endl;
std::cout << "dependencies count is " << root.dependenciesCount() << std::endl;
} }

View File

@ -1,15 +1,26 @@
#include "project.h" #include "project.h"
#include <nlohmann/json.hpp>
#include <string_view> #include <string_view>
#include <exception> #include <exception>
using json = nlohmann::json;
constexpr std::string_view entry("mason.json"); constexpr std::string_view entry("mason.json");
std::map<std::string, Dependency::Type> types({
{"simple", Dependency::Type::simple},
{"none", Dependency::Type::simple},
{"mason", Dependency::Type::mason},
{"auto", Dependency::Type::automatic},
{"automatic", Dependency::Type::automatic}
});
Project::Project(const std::filesystem::path& location): Project::Project(const std::filesystem::path& location):
location(location), location(location),
status(Status::unknown), status(Status::unknown),
name() name(),
logStorage(),
dependencies()
{} {}
bool Project::read() { bool Project::read() {
@ -32,9 +43,17 @@ bool Project::read() {
return false; return false;
} }
json data;
try { try {
nlohmann::json data = nlohmann::json::parse(file); data = json::parse(file);
name = data.at("name"); } catch (const json::exception& e) {
log(e.what());
status = Status::error;
return false;
}
try {
parse(data);
} catch (const std::exception& e) { } catch (const std::exception& e) {
log(e.what()); log(e.what());
status = Status::error; status = Status::error;
@ -44,6 +63,74 @@ bool Project::read() {
return true; return true;
} }
void Project::parse(const json& data) {
const json& nm = data.at("name");
if (!nm.is_string())
throw 1;
name = nm;
const json& dpd = data.at("dependencies");
if (!dpd.is_array())
throw 1;
for (const json& dep : dpd) {
switch (dep.type()) {
case json::value_t::string:
createDepencencyFromString(dep);
break;
case json::value_t::object:
createDepencencyFromObject(dep);
break;
default:
throw 1;
}
}
}
void Project::createDepencencyFromString(const std::string& entry) {
dependencies.emplace(entry, entry);
}
void Project::createDepencencyFromObject(const nlohmann::json& entry) {
std::string url;
json::const_iterator itr = entry.find("url");
if (itr == entry.end()) {
itr = entry.find("path");
if (itr == entry.end())
throw 1;
}
if (!itr->is_string())
throw 1;
url = *itr;
std::optional<std::string> name = std::nullopt;
itr = entry.find("name");
if (itr != entry.end() && itr->is_string())
name = *itr;
std::optional<std::string> version = std::nullopt;
itr = entry.find("version");
if (itr != entry.end() && itr->is_string())
version = *itr;
Dependency::Type type = Dependency::Type::automatic;
itr = entry.find("type");
if (itr != entry.end() && itr->is_string()) {
std::map<std::string, Dependency::Type>::const_iterator titr = types.find(*itr);
if (titr != types.end())
type = titr->second;
}
dependencies.emplace(url, Dependency{url, type, name, version});
}
uint32_t Project::dependenciesCount() const {
return dependencies.size();
}
void Project::log(const std::string& message) const { void Project::log(const std::string& message) const {
logStorage.emplace_back(message); logStorage.emplace_back(message);
} }

View File

@ -4,6 +4,12 @@
#include <string> #include <string>
#include <fstream> #include <fstream>
#include <list> #include <list>
#include <map>
#include <stdint.h>
#include <nlohmann/json.hpp>
#include "dependency.h"
class Project { class Project {
public: public:
@ -20,14 +26,19 @@ public:
Status getStatus() const; Status getStatus() const;
Log getLog() const; Log getLog() const;
std::string getName() const; std::string getName() const;
uint32_t dependenciesCount() const;
private: private:
void log(const std::string& message) const; void log(const std::string& message) const;
void parse(const nlohmann::json& json);
void createDepencencyFromString(const std::string& entry);
void createDepencencyFromObject(const nlohmann::json& entry);
private: private:
std::filesystem::path location; std::filesystem::path location;
Status status; Status status;
std::string name; std::string name;
mutable Log logStorage; mutable Log logStorage;
std::map<std::string, Dependency> dependencies;
}; };

View File

@ -1,4 +1,12 @@
{ {
"name" : "test", "name" : "test",
"version" : "0.0.1" "version" : "0.0.1",
"dependencies" : [
"somePath",
"https://git.macaw.me/blue/mason.git",
{
"name" : "aProject",
"path" : "https://nowhere.to"
}
]
} }