int loadExternalTOML(toml::value &root, ExternalConfig &ext)
{
    const auto &section = toml::find(root, "custom");

    find_if_exist(section,
                  "enable_rule_generator", ext.enable_rule_generator,
                  "overwrite_original_rules", ext.overwrite_original_rules,
                  "clash_rule_base", ext.clash_rule_base,
                  "surge_rule_base", ext.surge_rule_base,
                  "surfboard_rule_base", ext.surfboard_rule_base,
                  "mellow_rule_base", ext.mellow_rule_base,
                  "quan_rule_base", ext.quan_rule_base,
                  "quanx_rule_base", ext.quanx_rule_base,
                  "sssub_rule_base", ext.sssub_rule_base,
                  "add_emoji", ext.add_emoji,
                  "remove_old_emoji", ext.remove_old_emoji,
                  "include_remarks", ext.include,
                  "exclude_remarks", ext.exclude
    );

    if(ext.tpl_args != nullptr) operate_toml_kv_table(toml::find_or<std::vector<toml::table>>(section, "template_args", {}), "key", "value",
                                                      [&](const toml::value &key, const toml::value &value)
    {
        std::string val = toml::format(value);
        ext.tpl_args->local_vars[key.as_string()] = val;
    });

    auto groups = toml::find_or<std::vector<toml::value>>(root, "custom_groups", {});
    importItems(groups, "custom_groups", false);
    ext.custom_proxy_group = toml::get<ProxyGroupConfigs>(toml::value(groups));

    auto rulesets = toml::find_or<std::vector<toml::value>>(root, "rulesets", {});
    importItems(rulesets, "rulesets", false);
    if(global.maxAllowedRulesets && rulesets.size() > global.maxAllowedRulesets)
    {
        writeLog(0, "Ruleset count in external config has exceeded limit. ", LOG_LEVEL_WARNING);
        return -1;
    }
    ext.surge_ruleset = toml::get<RulesetConfigs>(toml::value(rulesets));

    auto emojiconfs = toml::find_or<std::vector<toml::value>>(root, "emoji", {});
    importItems(emojiconfs, "emoji", false);
    ext.emoji = toml::get<RegexMatchConfigs>(toml::value(emojiconfs));

    auto renameconfs = toml::find_or<std::vector<toml::value>>(root, "rename_node", {});
    importItems(renameconfs, "rename_node", false);
    ext.rename = toml::get<RegexMatchConfigs>(toml::value(renameconfs));

    return 0;
}
int loadExternalTOML(toml::value &root, ExternalConfig &ext)
{
    const auto &section = toml::find(root, "custom");

    find_if_exist(section,
                  "enable_rule_generator", ext.enable_rule_generator,
                  "overwrite_original_rules", ext.overwrite_original_rules,
                  "clash_rule_base", ext.clash_rule_base,
                  "surge_rule_base", ext.surge_rule_base,
                  "surfboard_rule_base", ext.surfboard_rule_base,
                  "mellow_rule_base", ext.mellow_rule_base,
                  "quan_rule_base", ext.quan_rule_base,
                  "quanx_rule_base", ext.quanx_rule_base,
                  "sssub_rule_base", ext.sssub_rule_base,
                  "add_emoji", ext.add_emoji,
                  "remove_old_emoji", ext.remove_old_emoji,
                  "include_remarks", ext.include,
                  "exclude_remarks", ext.exclude
    );

    if(ext.tpl_args != nullptr) operate_toml_kv_table(toml::find_or<std::vector<toml::table>>(section, "template_args", {}), "key", "value",
                                                      [&](const toml::value &key, const toml::value &value)
    {
        std::string val = toml::format(value);
        ext.tpl_args->local_vars[key.as_string()] = val;
    });

    auto groups = toml::find_or<std::vector<toml::value>>(root, "custom_groups", {});
    importItems(groups, "custom_groups", false);
    ext.custom_proxy_group = toml::get<ProxyGroupConfigs>(toml::value(groups));

    auto rulesets = toml::find_or<std::vector<toml::value>>(root, "rulesets", {});
    importItems(rulesets, "rulesets", false);
    if(global.maxAllowedRulesets && rulesets.size() > global.maxAllowedRulesets)
    {
        writeLog(0, "Ruleset count in external config has exceeded limit. ", LOG_LEVEL_WARNING);
        return -1;
    }
    ext.surge_ruleset = toml::get<RulesetConfigs>(toml::value(rulesets));

    auto emojiconfs = toml::find_or<std::vector<toml::value>>(root, "emoji", {});
    importItems(emojiconfs, "emoji", false);
    ext.emoji = toml::get<RegexMatchConfigs>(toml::value(emojiconfs));

    auto renameconfs = toml::find_or<std::vector<toml::value>>(root, "rename_node", {});
    importItems(renameconfs, "rename_node", false);
    ext.rename = toml::get<RegexMatchConfigs>(toml::value(renameconfs));

    return 0;
}