Dynamically check `FsPath` invariants

Aperta
#73 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
haskell

Direzione di ricerca

Inizia leggendo la definizione di FsPath e la discussione sul design nella pull request 72. Confronta le API proposte invariant, unsafeFsPathFromList e fsPathFromList con il comportamento esistente dei percorsi e considera il compromesso tra force e assert. Il lavoro è completo quando si è stabilito se i controlli dinamici siano giustificati e si è definito un comportamento di costruzione validata e di escape hatch coerente tra i sistemi operativi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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

Lingua principale
Haskell
Stelle
2
Fork
6
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di IntersectMBO/fs-sim

Tutte le issue di IntersectMBO/fs-sim

Issue simili

Altre issue su Haskell

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.