| /*
|
| * Copyright (C) 2024 Cow
|
| *
|
| * This program is free software: you can redistribute it and/or
|
| * modify it under the terms of the GNU General Public License as
|
| * published by the Free Software Foundation, version 3.
|
| *
|
| * This program is distributed in the hope that it will be useful,
|
| * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
| * General Public License for more details.
|
| *
|
| * You should have received a copy of the GNU General Public License
|
| * along with this program. If not, see https://www.gnu.org/licenses/.
|
| *
|
| * SPDX-License-Identifier: GPL-3.0-only
|
| */
|
|
|
| #include <algorithm>
|
| #include <expected>
|
| #include <filesystem>
|
| #include <print>
|
| #include <string_view>
|
| #include <toml++/toml.hpp>
|
|
|
| #include "sandboxmanager/load.hh"
|
| #include "sandboxmanager/profile.hh"
|
|
|
| namespace rv = std::ranges;
|
| namespace fs = std::filesystem;
|
|
|
| using std::unexpected;
|
|
|
| namespace SandboxManager {
|
|
|
| std::expected<Profile, LoadError>
|
| load_profile([[maybe_unused]] std::string_view profile_name) {
|
| for (const auto &path : PROFILE_SEARCH_DIRS) {
|
| fs::path searchdir(path);
|
|
|
| auto it = rv::find_if(
|
| fs::directory_iterator(searchdir), [&profile_name](const auto &entry) {
|
| return entry.is_regular_file() &&
|
| entry.path().filename().string() == profile_name;
|
| });
|
|
|
| if (it != fs::directory_iterator{}) {
|
| const auto &entry = *it;
|
| try {
|
| toml::table config = toml::parse_file(entry.path().string());
|
| auto profile = SandboxManager::Profile::from_toml(config);
|
| if (profile.has_value()) {
|
| return profile.value();
|
| }
|
| return unexpected(
|
| LoadError(LoadError::Type::InvalidProfile, profile.error()));
|
| } catch (const toml::parse_error &err) {
|
| return unexpected(LoadError(LoadError::Type::InvalidToml, err));
|
| }
|
| }
|
| }
|
| // No profile was found
|
| return unexpected(LoadError(LoadError::Type::NotFound));
|
| }
|
|
|
| } // namespace SandboxManager
|