<commit_before>/**
@copyright MIT license; see @ref index or the accompanying LICENSE file.
*/

#include <Hord/aux.hpp>
#include <Hord/utility.hpp>
#include <Hord/Log.hpp>
#include <Hord/IO/Defs.hpp>
#include <Hord/Object/Defs.hpp>
#include <Hord/Object/Unit.hpp>
#include <Hord/Object/Ops.hpp>
#include <Hord/Cmd/Object.hpp>

#include <exception>

#include <Hord/detail/gr_core.hpp>

namespace Hord {
namespace Cmd {
namespace Object {

#define HORD_SCOPE_CLASS Cmd::Object::SetParent

#define HORD_SCOPE_FUNC exec // pseudo
HORD_SCOPE_CLASS::exec_result_type
HORD_SCOPE_CLASS::operator()(
	Hord::Object::Unit& object,
	Hord::Object::ID const new_parent
) noexcept try {
	m_object_id = object.get_id();

	// TODO: Check for collision in parent children
	auto& datastore = get_datastore();
	if (object.get_id() == new_parent) {
		return commit_error("object cannot be a child of itself");
	} else if (object.get_parent() == new_parent) {
		// NB: Circumvents Object::set_parent() making the parent ID_NULL
		return commit_with(Cmd::Result::success_no_action);
	}
	bool const success = Hord::Object::set_parent(object, datastore, new_parent);
	auto const end = datastore.get_root_objects().end();<commit_msg>Cmd::Object::SetParent: check for parent existence and slug collision.<commit_after>/**
@copyright MIT license; see @ref index or the accompanying LICENSE file.
*/

#include <Hord/aux.hpp>
#include <Hord/utility.hpp>
#include <Hord/Log.hpp>
#include <Hord/IO/Defs.hpp>
#include <Hord/Object/Defs.hpp>
#include <Hord/Object/Unit.hpp>
#include <Hord/Object/Ops.hpp>
#include <Hord/Cmd/Object.hpp>

#include <exception>

#include <Hord/detail/gr_core.hpp>

namespace Hord {
namespace Cmd {
namespace Object {

#define HORD_SCOPE_CLASS Cmd::Object::SetParent

#define HORD_SCOPE_FUNC exec // pseudo
HORD_SCOPE_CLASS::exec_result_type
HORD_SCOPE_CLASS::operator()(
	Hord::Object::Unit& object,
	Hord::Object::ID const new_parent
) noexcept try {
	m_object_id = object.get_id();

	auto& datastore = get_datastore();
	auto const* parent_obj = datastore.find_ptr(new_parent);
	if (object.get_id() == new_parent) {
		return commit_error("cannot parent object to itself");
	} else if (object.get_parent() == new_parent) {
		// NB: Circumvents Object::set_parent() making the parent ID_NULL
		return commit_with(Cmd::Result::success_no_action);
	} else if (!new_parent.is_null() && !parent_obj) {
		return commit_error("new parent does not exist");
	} else if (parent_obj) {
		auto const& parent_children = parent_obj ? parent_obj->get_children() : datastore.get_root_objects();
		for (auto child_id : parent_children) {
			auto child_obj = datastore.find_ptr(child_id);
			if (child_obj && object.get_slug() == child_obj->get_slug()) {
				return commit_error("slug would not be unique in new parent");
			}
		}
	}

	bool const success = Hord::Object::set_parent(object, datastore, new_parent);
	auto const end = datastore.get_root_objects().end();
{'fraction_non_alphanumeric': 0.13548387096774195, 'fraction_numerical': 0.0, 'mean_word_length': 5.605381165919282, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5096713', 'n_tokens_mistral': 1064, 'n_tokens_neox': 1030, 'n_words': 241}