Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for lucid2 #26

Open
TristanCacqueray opened this issue Jun 29, 2022 · 4 comments
Open

Adding support for lucid2 #26

TristanCacqueray opened this issue Jun 29, 2022 · 4 comments

Comments

@TristanCacqueray
Copy link

Dear maintainers, would you mind adding an extra package to support lucid2 in servant, or would you prefer creating an extra repository for that?

@georgefst
Copy link

I don't know what the best solution here is. I personally think Chris' Immutable Publishing Policy, however well-meaning, is going to prove pretty painful in practice for downstream libraries, with current tooling.

@chreekat
Copy link

chreekat commented Feb 10, 2024

Well, here's one weird idea:

diff --git a/example/Main.hs b/example/Main.hs
index c73e24c..ece8b9f 100644
--- a/example/Main.hs
+++ b/example/Main.hs
@@ -3,11 +3,11 @@
 {-# LANGUAGE DataKinds         #-}
 {-# LANGUAGE OverloadedStrings #-}
 {-# LANGUAGE TypeOperators     #-}
+{-# LANGUAGE CPP #-}
 module Main (main) where
 
 import           Data.Maybe
                  (fromMaybe)
-import           Lucid
 import           Lucid.Servant
 import           Network.Wai
                  (Application)
@@ -20,6 +20,20 @@ import           Text.Read
 
 import qualified Network.Wai.Handler.Warp as Warp
 
+#if defined(LUCID2)
+import           Lucid
+import qualified Lucid
+#else
+import           Lucid hiding (Attribute)
+import qualified Lucid
+#endif
+
+#if defined(LUCID2)
+type Attribute = Lucid.Attributes
+#else
+type Attribute = Lucid.Attribute
+#endif
+
 type API = "string" :> Get '[HTML] String
     :<|> "nested" :> "html" :> Get '[HTML] (Html ())
 
diff --git a/servant-lucid.cabal b/servant-lucid.cabal
index 4c4d0bf..4594918 100644
--- a/servant-lucid.cabal
+++ b/servant-lucid.cabal
@@ -26,14 +26,22 @@ source-repository head
   type: git
   location: http://github.com/haskell-servant/servant-lucid.git
 
+flag lucid2
+  description: Use lucid2 instead of lucid1
+  default: False
+  manual: False
+
 library
   exposed-modules:     Servant.HTML.Lucid
                        Lucid.Servant
   build-depends:       base       >=4.9     && <5
                      , http-media >=0.6.4   && <0.9
-                     , lucid      >=2.9.8   && <2.12
                      , text       >=1.2.3.0 && <1.3 || >= 2 && < 2.1
                      , servant    >=0.17    && <0.21
+  if flag(lucid2)
+    build-depends:     lucid2
+  else
+    build-depends:     lucid      >=2.9.8   && <2.12
 
   if !impl(ghc >= 8.0)
     build-depends:     semigroups >=0.18.4  && <0.20
@@ -42,6 +50,9 @@ library
   default-language:    Haskell2010
   ghc-options: -Wall
 
+  if flag(lucid2)
+    cpp-options: -DLUCID2
+
 test-suite example
   type: exitcode-stdio-1.0
   main-is: Main.hs
@@ -50,9 +61,15 @@ test-suite example
   ghc-options: -Wall
   build-depends:
       base
-    , lucid
     , servant-lucid
     , servant-server >=0.14     && <0.21
     , wai            >=3.0.3.0  && <3.3
     , warp           >=3.0.13.1 && <3.4
+  if flag(lucid2)
+    build-depends: lucid2
+  else
+    build-depends: lucid
   default-language: Haskell2010
+
+  if flag(lucid2)
+    cpp-options: -DLUCID2
diff --git a/src/Lucid/Servant.hs b/src/Lucid/Servant.hs
index 176933d..0a63652 100644
--- a/src/Lucid/Servant.hs
+++ b/src/Lucid/Servant.hs
@@ -1,5 +1,6 @@
 {-# LANGUAGE ConstraintKinds   #-}
 {-# LANGUAGE OverloadedStrings #-}
+{-# LANGUAGE CPP #-}
 
 -- | Some helper functions for creating values in
 -- [lucid](https://hackage.haskell.org/package/lucid) DSLs that work
@@ -18,8 +19,6 @@ import           Data.Proxy
 import           Data.Semigroup
                  ((<>))
 import qualified Data.Text           as T
-import           Lucid
-                 (Attribute)
 import           Lucid.Html5
                  (href_)
 import           Servant.API
@@ -27,6 +26,18 @@ import           Servant.API
 import           Servant.Links
                  (HasLink, IsElem, Link, MkLink, safeLink')
 
+#if defined(LUCID2)
+import qualified Lucid
+#else
+import qualified Lucid
+#endif
+
+#if defined(LUCID2)
+type Attribute = Lucid.Attributes
+#else
+type Attribute = Lucid.Attribute
+#endif
+
 -- | 'safeLink' variant which creates lucid's 'Attribute' given base url.
 --
 -- >>> type API = "path" :> Get '[JSON] Int

The problem is that the only way I know how to use this is to add the +lucid2 flag to my local cabal.project. Which, of course, every downstream user would also have to do. Ideally there would be something I could do in my package.cabal file that would force the +lucid2 flag to get turned on automatically.

@chreekat
Copy link

(sorry, wrong patch got pasted in originally)

@srid
Copy link

srid commented Nov 15, 2024

Looks like there is a fork (by @danidiaz) that provides this:

https://github.com/danidiaz/dani-servant-lucid2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants