<commit_before>module Hyper.Routing.DataRouter
       ( GET
       , POST
       , Path
       , Route(..)
       , class Addressable
       , toPath
       , class Routable
       , fromRoute
       , class RoutableMethod
       , getMethod
       , router
       , linkTo
       , formFor
       ) where

import Prelude
import Hyper.Method as Method
import Data.Array (filter)
import Data.Leibniz (type (~))
import Data.Maybe (Maybe(Nothing, Just))
import Data.String (joinWith, Pattern(Pattern), split)
import Data.Tuple (Tuple(Tuple))
import Hyper.Core (TryMiddleware(TryMiddleware), Conn, Middleware)
import Hyper.HTML (element, a, form, HTML)
import Hyper.Method (Method)

data GET
data POST

type Path = Array String

pathToHtml :: Path -> String
pathToHtml = (<>) "/" <<< joinWith "/"

pathFromString :: String -> Path
pathFromString = filter ((/=) "") <<< split (Pattern "/")

data Route m = Route Path

class Addressable r where
  toPath :: forall m. r m -> Path

class Routable r m where
  fromRoute :: Route m -> Maybe (r m)

class RoutableMethod m where
  getMethod :: Method

instance routableMethodGET :: RoutableMethod GET where
  getMethod = Method.GET

instance routableMethodPOST :: RoutableMethod POST where
  getMethod = Method.POST

type MethodRequestHandler r method m req res c req' res' c' =
  r method -> Middleware
              m
              (Conn { method :: Method, url :: String | req } res c)
              (Conn req' res' c')

routeWith :: forall c r method m c'.
  ( Routable r method , Functor m , Applicative m ) =>
  c -> Route method -> (r method -> c -> m c') -> m (Maybe c')
routeWith conn route handler =
  case fromRoute route of
    Just requestable ->
      Just <$> handler requestable conn
    Nothing -> pure Nothing

router
  :: forall m r req res c req' res' c'.
     (Applicative m, Routable r GET, Routable r POST) =>
  { get :: MethodRequestHandler r GET m req res c req' res' c'<commit_msg>Remove RoutableMethod<commit_after>module Hyper.Routing.DataRouter
       ( GET
       , POST
       , Path
       , Route(..)
       , class Addressable
       , toPath
       , class Routable
       , fromRoute
       , router
       , linkTo
       , formFor
       ) where

import Prelude
import Hyper.Method as Method
import Data.Array (filter)
import Data.Leibniz (type (~))
import Data.Maybe (Maybe(Nothing, Just))
import Data.String (joinWith, Pattern(Pattern), split)
import Data.Tuple (Tuple(Tuple))
import Hyper.Core (TryMiddleware(TryMiddleware), Conn, Middleware)
import Hyper.HTML (element, a, form, HTML)
import Hyper.Method (Method)

data GET
data POST

type Path = Array String

pathToHtml :: Path -> String
pathToHtml = (<>) "/" <<< joinWith "/"

pathFromString :: String -> Path
pathFromString = filter ((/=) "") <<< split (Pattern "/")

data Route m = Route Path

class Addressable r where
  toPath :: forall m. r m -> Path

class Routable r m where
  fromRoute :: Route m -> Maybe (r m)

type MethodRequestHandler r method m req res c req' res' c' =
  r method -> Middleware
              m
              (Conn { method :: Method, url :: String | req } res c)
              (Conn req' res' c')

routeWith :: forall c r method m c'.
  ( Routable r method , Functor m , Applicative m ) =>
  c -> Route method -> (r method -> c -> m c') -> m (Maybe c')
routeWith conn route handler =
  case fromRoute route of
    Just requestable ->
      Just <$> handler requestable conn
    Nothing -> pure Nothing

router
  :: forall m r req res c req' res' c'.
     (Applicative m, Routable r GET, Routable r POST) =>
  { get :: MethodRequestHandler r GET m req res c req' res' c'
{'fraction_non_alphanumeric': 0.10792951541850221, 'fraction_numerical': 0.0, 'mean_word_length': 2.7185261003070624, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4718830', 'n_tokens_mistral': 1112, 'n_tokens_neox': 1075, 'n_words': 419}