nameFromBytes :: BS.ByteString -> Maybe Name
nameFromBytes bytes = if valid then Just $ Name bytes else Nothing
  where
    valid = BS.all validByte bytes &&
        not adjacentPeriods &&
        not leadingPeriod &&
        not trailingPeriod
    validByte byte =
        isAsciiPeriod byte ||
        isAsciiAlphaNum byte ||
        isAsciiSeparator byte
    adjacentPeriods = ".." `BS.isInfixOf` bytes
    (leadingPeriod, trailingPeriod) = if not $ BS.null bytes
        then (asciiPeriod == BS.head bytes, asciiPeriod == BS.last bytes)
        else (False, False)

nameFromBytes :: BS.ByteString -> Maybe Name
nameFromBytes bytes = if valid then Just $ Name bytes else Nothing
  where
    valid = BS.all validByte bytes &&
        not adjacentPeriods &&
        not leadingPeriod &&
        not trailingPeriod
    validByte byte =
        isAsciiPeriod byte ||
        isAsciiAlphaNum byte ||
        isAsciiSeparator byte
    adjacentPeriods = ".." `BS.isInfixOf` bytes
    (leadingPeriod, trailingPeriod) = if not $ BS.null bytes
        then (asciiPeriod == BS.head bytes, asciiPeriod == BS.last bytes)
        else (False, False)

nameFromBytes bytes = if valid then Just $ Name bytes else Nothing
  where
    valid = BS.all validByte bytes &&
        not adjacentPeriods &&
        not leadingPeriod &&
        not trailingPeriod
    validByte byte =
        isAsciiPeriod byte ||
        isAsciiAlphaNum byte ||
        isAsciiSeparator byte
    adjacentPeriods = ".." `BS.isInfixOf` bytes
    (leadingPeriod, trailingPeriod) = if not $ BS.null bytes
        then (asciiPeriod == BS.head bytes, asciiPeriod == BS.last bytes)
        else (False, False)
