Detecting if list of fences form a closed rectangle

In the game im working on the player can build sections of fencing. They can start at any position and extend the fence as far as they want provided that have enough money. They can only rotate which direction the fence is going in 90 degree increments, so only 4 possible directions. I need to make a function that can detect if a given list of these fence segments form an enclosed area like a rectangle. However im not really sure how to go about doing this or how this would work, could anyone provide some insight on this? Thanks for any help

You could try and see when the user connects each fence together, and if there is a connection between 4 fences to 2 other fences in the array, it would be a square/rectangle. (this is just a theory, I don’t know if it would actually work)

local FenceConnections = {}

function addConnection(fence1,fence2)
    local added = false
    for _,v in pairs (FenceConnections) do
        for _,f in pairs(v) do
            if f == fence1 or v == fence2 then
                local alreadyadded1,alreadyadded2 = false,false
                for _,v in pairs(v) do
                    if f == fence1 then
                        alreadyadded1 = true
                    end
                    if f == fence2 then
                        alreadyadded2 = true
                    end
                end
                if alreadyadded1 and alreadyadded2 then
                    -- IT'S A SQUARE (i think)
                else
                    if alreadyadded1 then
                        table.insert(v,1,fence2)
                    elseif alreadyadded2 then
                        table.insert(v,1,fence1)
                    end
                end
            end
        end
    end
    table.insert(FenceConnections,1,{fence1,fence2})
end

NOTE: I haven’t actually tested this code so don’t treat it as if it will be how you code it, but it should give you some ideas of how you would figure out if a square is connected. I hope this at least could give you a decent idea of where to start, this is a pretty interesting question and I hope you can figure it out!