Create sub module with atom name

Ahh, Thank you yes you are right

  defmacro sub_field(name, _type, opts \\ [], do: block) do
    ast = register_struct(block, opts)

    name =
      name
      |> Atom.to_string()
      |> Macro.camelize()
      |> String.to_atom()

    %Macro.Env{module: mod} = __CALLER__
    module_name = Module.concat(mod, name)

    quote do
      defmodule unquote(module_name) do
        unquote(ast)
      end
    end
  end

but I think it just supports one level nested, because I use __CALLER__

I think it does not support

  defmodule TestNestedStruct do
    use GuardedStruct

    guardedstruct do
      field(:title, String.t())
      field(:subject, String.t())

      sub_field(:oop, String.t(), enforce: true) do
        field(:title, String.t())
        field(:fam, String.t())
        sub_field(:sop, String.t(), enforce: true) do
          field(:title, String.t())
        end
      end

      field(:site, String.t())
    end
  end

so I wont be able to fix it I create another post for it. thank you