// This is a generated file! Please edit source .ksy file and use kaitai-struct-compiler to rebuild

#include "nix_nar.h"
#include "kaitai/exceptions.h"

nix_nar_t::nix_nar_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root ? p__root : this;
    m_magic = nullptr;
    m_root_node = nullptr;
    _read();
}

void nix_nar_t::_read() {
    m_magic = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_magic;
        if (!(_->body() == std::string("nix-archive-1"))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_magic, m__io, std::string("/seq/0"));
        }
    }
    m_root_node = std::unique_ptr<node_t>(new node_t(0, m__io, this, m__root));
}

nix_nar_t::~nix_nar_t() {
    _clean_up();
}

void nix_nar_t::_clean_up() {
}

nix_nar_t::node_t::node_t(int32_t p_depth, kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_depth = p_depth;
    m_open_paren = nullptr;
    m_type_key = nullptr;
    m_type_val = nullptr;
    m_close_paren = nullptr;
    _read();
}

void nix_nar_t::node_t::_read() {
    m__unnamed0 = m__io->read_bytes(0);
    {
        std::string _ = m__unnamed0;
        if (!(depth() < 64)) {
            throw kaitai::validation_expr_error<std::string>(m__unnamed0, m__io, std::string("/types/node/seq/0"));
        }
    }
    m_open_paren = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_open_paren;
        if (!(_->body() == std::string("("))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_open_paren, m__io, std::string("/types/node/seq/1"));
        }
    }
    m_type_key = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_type_key;
        if (!(_->body() == std::string("type"))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_type_key, m__io, std::string("/types/node/seq/2"));
        }
    }
    m_type_val = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_type_val;
        if (!( ((_->body() == std::string("regular")) || (_->body() == std::string("directory")) || (_->body() == std::string("symlink"))) )) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_type_val, m__io, std::string("/types/node/seq/3"));
        }
    }
    n_body = true;
    {
        std::string on = type_val()->body();
        if (on == std::string("directory")) {
            n_body = false;
            m_body = std::unique_ptr<type_directory_t>(new type_directory_t(m__io, this, m__root));
        }
        else if (on == std::string("regular")) {
            n_body = false;
            m_body = std::unique_ptr<type_regular_t>(new type_regular_t(m__io, this, m__root));
        }
        else if (on == std::string("symlink")) {
            n_body = false;
            m_body = std::unique_ptr<type_symlink_t>(new type_symlink_t(m__io, this, m__root));
        }
    }
    n_close_paren = true;
    if (type_val()->body() != std::string("directory")) {
        n_close_paren = false;
        m_close_paren = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
        {
            std::unique_ptr<padded_str_t> _ = m_close_paren;
            if (!(_->body() == std::string(")"))) {
                throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_close_paren, m__io, std::string("/types/node/seq/5"));
            }
        }
    }
}

nix_nar_t::node_t::~node_t() {
    _clean_up();
}

void nix_nar_t::node_t::_clean_up() {
    if (!n_body) {
    }
    if (!n_close_paren) {
    }
}

nix_nar_t::padded_bytes_t::padded_bytes_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void nix_nar_t::padded_bytes_t::_read() {
    m_len_body = m__io->read_u8le();
    m_body = m__io->read_bytes(len_body());
    m_padding = m__io->read_bytes(kaitai::kstream::mod(8 - kaitai::kstream::mod(len_body(), 8), 8));
    {
        std::string _ = m_padding;
        if (!(((_.length() != 0) ? (kaitai::kstream::byte_array_max(_) == 0) : (true)))) {
            throw kaitai::validation_expr_error<std::string>(m_padding, m__io, std::string("/types/padded_bytes/seq/2"));
        }
    }
}

nix_nar_t::padded_bytes_t::~padded_bytes_t() {
    _clean_up();
}

void nix_nar_t::padded_bytes_t::_clean_up() {
}

nix_nar_t::padded_str_t::padded_str_t(kaitai::kstream* p__io, kaitai::kstruct* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    _read();
}

void nix_nar_t::padded_str_t::_read() {
    m_len_body = m__io->read_u8le();
    m_body = kaitai::kstream::bytes_to_str(m__io->read_bytes(len_body()), "ASCII");
    m_padding = m__io->read_bytes(kaitai::kstream::mod(8 - kaitai::kstream::mod(len_body(), 8), 8));
    {
        std::string _ = m_padding;
        if (!(((_.length() != 0) ? (kaitai::kstream::byte_array_max(_) == 0) : (true)))) {
            throw kaitai::validation_expr_error<std::string>(m_padding, m__io, std::string("/types/padded_str/seq/2"));
        }
    }
}

nix_nar_t::padded_str_t::~padded_str_t() {
    _clean_up();
}

void nix_nar_t::padded_str_t::_clean_up() {
}

nix_nar_t::type_directory_t::type_directory_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_entries = nullptr;
    _read();
}

void nix_nar_t::type_directory_t::_read() {
    m_entries = std::unique_ptr<std::vector<std::unique_ptr<dir_entry_t>>>(new std::vector<std::unique_ptr<dir_entry_t>>());
    {
        int i = 0;
        dir_entry_t* _;
        do {
            _ = new dir_entry_t(m__io, this, m__root);
            m_entries->push_back(std::move(std::unique_ptr<dir_entry_t>(_)));
            {
                std::unique_ptr<dir_entry_t> _ = m_entries->at(i);
                if (!((( ((i != 0) && (!(_->is_terminator()))) ) ? (_->body()->name()->body() > entries()->at(i - 1)->body()->name()->body()) : (true)))) {
                    throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::type_directory_t::dir_entry_t>>(m_entries->at(i), m__io, std::string("/types/type_directory/seq/0"));
                }
            }
            i++;
        } while (!(_->is_terminator()));
    }
}

nix_nar_t::type_directory_t::~type_directory_t() {
    _clean_up();
}

void nix_nar_t::type_directory_t::_clean_up() {
}

nix_nar_t::type_directory_t::dir_entry_t::dir_entry_t(kaitai::kstream* p__io, nix_nar_t::type_directory_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_kind = nullptr;
    m_body = nullptr;
    f_is_terminator = false;
    _read();
}

void nix_nar_t::type_directory_t::dir_entry_t::_read() {
    m_kind = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_kind;
        if (!( ((_->body() == std::string("entry")) || (_->body() == std::string(")"))) )) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_kind, m__io, std::string("/types/type_directory/types/dir_entry/seq/0"));
        }
    }
    n_body = true;
    if (!(is_terminator())) {
        n_body = false;
        m_body = std::unique_ptr<dir_entry_body_t>(new dir_entry_body_t(m__io, this, m__root));
    }
}

nix_nar_t::type_directory_t::dir_entry_t::~dir_entry_t() {
    _clean_up();
}

void nix_nar_t::type_directory_t::dir_entry_t::_clean_up() {
    if (!n_body) {
    }
}

bool nix_nar_t::type_directory_t::dir_entry_t::is_terminator() {
    if (f_is_terminator)
        return m_is_terminator;
    f_is_terminator = true;
    m_is_terminator = kind()->body() == std::string(")");
    return m_is_terminator;
}

nix_nar_t::type_directory_t::dir_entry_body_t::dir_entry_body_t(kaitai::kstream* p__io, nix_nar_t::type_directory_t::dir_entry_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_open_paren = nullptr;
    m_name_key = nullptr;
    m_name = nullptr;
    m_node_key = nullptr;
    m_node = nullptr;
    m_close_paren = nullptr;
    _read();
}

void nix_nar_t::type_directory_t::dir_entry_body_t::_read() {
    m_open_paren = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_open_paren;
        if (!(_->body() == std::string("("))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_open_paren, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/0"));
        }
    }
    m_name_key = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_name_key;
        if (!(_->body() == std::string("name"))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_name_key, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/1"));
        }
    }
    m_name = std::unique_ptr<padded_bytes_t>(new padded_bytes_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_bytes_t> _ = m_name;
        if (!( ((_->body().length() != 0) && (_->body().length() <= 255) && (_->body() != std::string("\x2E", 1)) && (_->body() != std::string("\x2E\x2E", 2)) && (kaitai::kstream::byte_array_min(_->body()) != 0)) )) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_bytes_t>>(m_name, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/2"));
        }
    }
    m_node_key = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_node_key;
        if (!(_->body() == std::string("node"))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_node_key, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/3"));
        }
    }
    m_node = std::unique_ptr<node_t>(new node_t(_parent()->_parent()->_parent()->depth() + 1, m__io, this, m__root));
    m_close_paren = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_close_paren;
        if (!(_->body() == std::string(")"))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_close_paren, m__io, std::string("/types/type_directory/types/dir_entry_body/seq/5"));
        }
    }
}

nix_nar_t::type_directory_t::dir_entry_body_t::~dir_entry_body_t() {
    _clean_up();
}

void nix_nar_t::type_directory_t::dir_entry_body_t::_clean_up() {
}

nix_nar_t::type_regular_t::type_regular_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_first_key = nullptr;
    m_executable_val = nullptr;
    m_contents_key = nullptr;
    m_file_data = nullptr;
    f_is_executable = false;
    _read();
}

void nix_nar_t::type_regular_t::_read() {
    m_first_key = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_first_key;
        if (!( ((_->body() == std::string("executable")) || (_->body() == std::string("contents"))) )) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_first_key, m__io, std::string("/types/type_regular/seq/0"));
        }
    }
    n_executable_val = true;
    if (is_executable()) {
        n_executable_val = false;
        m_executable_val = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
        {
            std::unique_ptr<padded_str_t> _ = m_executable_val;
            if (!(_->body() == std::string(""))) {
                throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_executable_val, m__io, std::string("/types/type_regular/seq/1"));
            }
        }
    }
    n_contents_key = true;
    if (is_executable()) {
        n_contents_key = false;
        m_contents_key = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
        {
            std::unique_ptr<padded_str_t> _ = m_contents_key;
            if (!(_->body() == std::string("contents"))) {
                throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_contents_key, m__io, std::string("/types/type_regular/seq/2"));
            }
        }
    }
    m_file_data = std::unique_ptr<file_content_t>(new file_content_t(m__io, this, m__root));
}

nix_nar_t::type_regular_t::~type_regular_t() {
    _clean_up();
}

void nix_nar_t::type_regular_t::_clean_up() {
    if (!n_executable_val) {
    }
    if (!n_contents_key) {
    }
}

nix_nar_t::type_regular_t::file_content_t::file_content_t(kaitai::kstream* p__io, nix_nar_t::type_regular_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    f_nar_offset = false;
    _read();
}

void nix_nar_t::type_regular_t::file_content_t::_read() {
    m_len_contents = m__io->read_u8le();
    n__unnamed1 = true;
    if (nar_offset() < 0) {
        n__unnamed1 = false;
        m__unnamed1 = m__io->read_bytes(0);
    }
    m_contents = m__io->read_bytes(len_contents());
    m_padding = m__io->read_bytes(kaitai::kstream::mod(8 - kaitai::kstream::mod(len_contents(), 8), 8));
    {
        std::string _ = m_padding;
        if (!(((_.length() != 0) ? (kaitai::kstream::byte_array_max(_) == 0) : (true)))) {
            throw kaitai::validation_expr_error<std::string>(m_padding, m__io, std::string("/types/type_regular/types/file_content/seq/3"));
        }
    }
}

nix_nar_t::type_regular_t::file_content_t::~file_content_t() {
    _clean_up();
}

void nix_nar_t::type_regular_t::file_content_t::_clean_up() {
    if (!n__unnamed1) {
    }
}

int32_t nix_nar_t::type_regular_t::file_content_t::nar_offset() {
    if (f_nar_offset)
        return m_nar_offset;
    f_nar_offset = true;
    m_nar_offset = _io()->pos();
    return m_nar_offset;
}

bool nix_nar_t::type_regular_t::is_executable() {
    if (f_is_executable)
        return m_is_executable;
    f_is_executable = true;
    m_is_executable = first_key()->body() == std::string("executable");
    return m_is_executable;
}

nix_nar_t::type_symlink_t::type_symlink_t(kaitai::kstream* p__io, nix_nar_t::node_t* p__parent, nix_nar_t* p__root) : kaitai::kstruct(p__io) {
    m__parent = p__parent;
    m__root = p__root;
    m_target_key = nullptr;
    m_target_val = nullptr;
    _read();
}

void nix_nar_t::type_symlink_t::_read() {
    m_target_key = std::unique_ptr<padded_str_t>(new padded_str_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_str_t> _ = m_target_key;
        if (!(_->body() == std::string("target"))) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_str_t>>(m_target_key, m__io, std::string("/types/type_symlink/seq/0"));
        }
    }
    m_target_val = std::unique_ptr<padded_bytes_t>(new padded_bytes_t(m__io, this, m__root));
    {
        std::unique_ptr<padded_bytes_t> _ = m_target_val;
        if (!( ((_->body().length() != 0) && (_->body().length() <= 4095) && (kaitai::kstream::byte_array_min(_->body()) != 0)) )) {
            throw kaitai::validation_expr_error<std::unique_ptr<nix_nar_t::padded_bytes_t>>(m_target_val, m__io, std::string("/types/type_symlink/seq/1"));
        }
    }
}

nix_nar_t::type_symlink_t::~type_symlink_t() {
    _clean_up();
}

void nix_nar_t::type_symlink_t::_clean_up() {
}
