You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: lib.rs
    path of file: ./repos/wezterm/lua-api-crates/serde-funcs/src
    
    the code of the file until where you have to start completion: use config::lua::mlua::{self, IntoLua, Lua, Value as LuaValue};
use config::lua::{get_or_create_module, get_or_create_sub_module};
use luahelper::lua_value_to_dynamic;
use serde_json::{Map, Value as JValue};
use std::collections::HashSet;
use wezterm_dynamic::{FromDynamic, Value
    
fn lua_value_to_json_value(value: LuaValue, visited: &mut HashSet<usize>) -> mlua::Result<JValue> {
    if let LuaValue::Table(_) = &value {
        let ptr = value.to_pointer() as usize;
        if visited.contains(&ptr) {
            // Skip this one, as we've seen it before.
            // Treat it as a Null value.
            return Ok(JValue::Null);
        }
        visited.insert(ptr);
    }
    Ok(match value {
        LuaValue::Nil => JValue::Null,
        LuaValue::String(s) => JValue::String(s.to_str()?.to_string()),
        LuaValue::Boolean(b) => JValue::Bool(b),
        LuaValue::Integer(i) => JValue::Number(i.into()),
        LuaValue::Number(i) => {
            if let Some(n) = serde_json::value::Number::from_f64(i) {
                JValue::Number(n)
            } else {
                return Err(mlua::Error::FromLuaConversionError {
                    from: "number",
                    to: "JsonValue",
                    message: Some(format!("unable to represent {i} as json float")),
                });
            }
        }
        LuaValue::UserData(ud) => match ud.get_metatable() {
            Ok(mt) => {
                if let Ok(to_dynamic) = mt.get::<mlua::Function>("__wezterm_to_dynamic") {
                    match to_dynamic.call(LuaValue::UserData(ud.clone())) {
                        Ok(value) => {
                            let dyn_value = lua_value_to_dynamic(value)?;
                            return dyn_to_json(dyn_value).map_err(mlua::Error::external);
                        }
                        Err(err) => {
                            return Err(mlua::Error::FromLuaConversionError {
                                from: "userdata",
                                to: "wezterm_dynamic::Value",
                                message: Some(format!(
                                    "error calling __wezterm_to_dynamic: {err:#}"
                                )),
                            })
                        }
                    }
                } else {
                    return Err(mlua::Error::FromLuaConversionError {
                        from: "userdata",
                        to: "wezterm_dynamic::Value",
                        message: Some(format!("no __wezterm_to_dynamic metadata")),
                    });
                }
            }
            Err(err) => {
                return Err(mlua::Error::FromLuaConversionError {
                    from: "userdata",
                    to: "wezterm_dynamic::Value",
                    message: Some(format!("error getting metatable: {err:#}")),
                })
            }
        },
        // Handle our special Null userdata case and map it to Null
        LuaValue::LightUserData(ud) if ud.0.is_null() => JValue::Null,
        LuaValue::LightUserData(_) => {
            return Err(mlua::Error::FromLuaConversionError {
                from: "userdata",
                to: "JsonValue",
                message: None,
            })
        }
        LuaValue::Function(_) => {
            return Err(mlua::Error::FromLuaConversionError {
                from: "function",
                to: "JsonValue",
                message: None,
            })
        }
        LuaValue::Thread(_) => {
            return Err(mlua::Error::FromLuaConversionError {
                from: "thread",
                to: "JsonValue",
                message: None,
            })
        }
        LuaValue::Error(e) => return Err(e),
        LuaValue::Table(table) => {
            if let Ok(true) = table.contains_key(1) {
                let mut array = vec![];
                let pairs = table.clone();
                for value in table.sequence_values() {
                    array.push(lua_value_to_json_value(value?, visited)?);
                }

                for pair in pairs.pairs::<LuaValue, LuaValue>() {
                    let (key, _value) = pair?;
                    match &key {
                        LuaValue::Integer(n) if *n >= 1 && *n as usize <= array.len() => {
                            // Ok!
                        }
                        _ => {
                            let type_name = key.type_name();
                            let key = luahelper::ValuePrinter(key);
                            return Err(mlua::Error::FromLuaConversionError {
                                from: type_name,
                                to: "numeric array index",
                                message: Some(format!(
                                    "Unexpected key {key:?} for array style table"
                                )),
                            });
                        }
                    }
                }

                JValue::Array(array.into())
            } else {
                let mut obj = Map::default();
                for pair in table.pairs::<LuaValue, LuaValue>() {
                    let (key, value) = pair?;
                    let key_type = key.type_name();
                    let key = match lua_value_to_json_value(key, visited)? {
                        JValue::String(s) => s,
                        _ => {
                            return Err(mlua::Error::FromLuaConversionError {
                                from: key_type,
                                to: "string",
                                message: Some("json object keys must be strings".to_string()),
                            });
                        }
                    };
                    let lua_type = value.type_name();
                    let value = lua_value_to_json_value(value, visited).map_err(|e| {
                        mlua::Error::FromLuaConversionError {
                            from: lua_type,
                            to: "value",
                            message: Some(format!("while processing {key:?}: {e}")),
                        }
                    })?;
                    obj.insert(key, value);
                }
                JValue::Object(obj.into())
            }
        }
    })
}