Dynamically check `FsPath` invariants

Abierto
#73 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
25/100
Tipo de issue
Nueva funcionalidad
Claridad
Necesita aclaración
Estado de actividad
Estancado
Stack tecnológico
haskell

Línea de trabajo

Empieza leyendo la definición de FsPath y el debate de diseño en el pull request 72. Compara las APIs propuestas invariant, unsafeFsPathFromList y fsPathFromList con el comportamiento existente de las rutas y considera la disyuntiva entre force y assert. Se considera terminado cuando se haya acordado si las comprobaciones dinámicas están justificadas y se haya definido un comportamiento de construcción validada y de escape hatch coherente en todos los sistemas operativos.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

First suggested in https://github.com/input-output-hk/fs-sim/pull/72#issue-2337874221. FsPath has a number of invariants, and we currently rely on the user to satisfy them. It might be nice to check these invariants dynamically, while also providing an escape hatch for users in case they want to skip the check. For example:

newtype FsPath = UnsafeFsPath { fsPathToList :: [Strict.Text] }
  deriving (Eq, Ord, Generic)
  deriving newtype NFData

invariant :: FsPath -> Bool
invariant (UnsafeFsPath xs) = all p xs
 where p x = -- Paths are monotonic
             x /= ".."
          && -- Paths can not have empty directory/file names
             not (any Strict.null xs)
          && -- There are no path separators in individual names
             not (Text.any (`Text.elem` allPathSeparators) x)

allPathSeparators :: Text
allPathSeparators = Text.pack (Posix.pathSeparators ++ Windows.pathSeparators)

unsafeFsPathFromList :: [Strict.Text] -> FsPath
unsafeFsPathFromList xs = assert (invariant fsp) $ fsp
  where fsp = UnsafeFsPath (force xs)

fsPathFromList :: [Strict.Text] -> Maybe FsPath
fsPathFromList xs
  | invariant fsp = Just fsp
  | otherwise     = Nothing
  where fsp = UnsafeFsPath (force xs)

Note two things in the example above:

  • Both unsafeFsPathFromList and fsPathFromList ensure that the resulting FsPath contains no thunks my using force. Maybe we want to assert instead that the FsPath contains no thunks in unsafeFsPathFromList, and have the user ensure that fact.
  • To ensure similar behaviour across OS distributions, invariant checks do not depend on the underlying OS distribution, hence allPathSeparators.

We should also consider whether this design pattern is overkill: in practice, users are probably unlikely to use non-sensible path names

Lenguaje dominante
Haskell
Estrellas
2
Forks
6
Métricas de merge de PR
Sin PR fusionados en 30 d

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de IntersectMBO/fs-sim

Todos los issues de IntersectMBO/fs-sim

Issues similares

Más issues de Haskell

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.