-- This file is part of the Wire Server implementation.
--
-- Copyright (C) 2023 Wire Swiss GmbH <opensource@wire.com>
--
-- This program is free software: you can redistribute it and/or modify it under
-- the terms of the GNU Affero General Public License as published by the Free
-- Software Foundation, either version 3 of the License, or (at your option) any
-- later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT
-- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
-- FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
-- details.
--
-- You should have received a copy of the GNU Affero General Public License along
-- with this program. If not, see <https://www.gnu.org/licenses/>.

-- | Servant combinators related to Swagger docs
module Wire.API.SwaggerServant
  ( OmitDocs,
  )
where

import Data.Metrics.Servant
import Data.Proxy
import Imports hiding (head)
import Servant
import Servant.OpenApi (HasOpenApi (toOpenApi))

-- | A type-level tag that lets us omit any branch from Swagger docs.
--
-- FUTUREWORK(fisx): this is currently only used for the spar internal api
-- and spar scim, and we should probably eliminate those uses and this combinator.
-- it's only justification is laziness.
data OmitDocs

instance HasOpenApi (OmitDocs :> a) where
  toOpenApi :: Proxy (OmitDocs :> a) -> OpenApi
toOpenApi Proxy (OmitDocs :> a)
_ = OpenApi
forall a. Monoid a => a
mempty

instance (HasServer api ctx) => HasServer (OmitDocs :> api) ctx where
  type ServerT (OmitDocs :> api) m = ServerT api m

  route :: forall env.
Proxy (OmitDocs :> api)
-> Context ctx
-> Delayed env (Server (OmitDocs :> api))
-> Router env
route Proxy (OmitDocs :> api)
_ = Proxy api
-> Context ctx
-> Delayed env (Server api)
-> Router' env RoutingApplication
forall env.
Proxy api -> Context ctx -> Delayed env (Server api) -> Router env
forall {k} (api :: k) (context :: [*]) env.
HasServer api context =>
Proxy api
-> Context context -> Delayed env (Server api) -> Router env
route (Proxy api
forall {k} (t :: k). Proxy t
Proxy :: Proxy api)
  hoistServerWithContext :: forall (m :: * -> *) (n :: * -> *).
Proxy (OmitDocs :> api)
-> Proxy ctx
-> (forall x. m x -> n x)
-> ServerT (OmitDocs :> api) m
-> ServerT (OmitDocs :> api) n
hoistServerWithContext Proxy (OmitDocs :> api)
_ Proxy ctx
pc forall x. m x -> n x
nt ServerT (OmitDocs :> api) m
s =
    Proxy api
-> Proxy ctx
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
forall {k} (api :: k) (context :: [*]) (m :: * -> *) (n :: * -> *).
HasServer api context =>
Proxy api
-> Proxy context
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
forall (m :: * -> *) (n :: * -> *).
Proxy api
-> Proxy ctx
-> (forall x. m x -> n x)
-> ServerT api m
-> ServerT api n
hoistServerWithContext (Proxy api
forall {k} (t :: k). Proxy t
Proxy :: Proxy api) Proxy ctx
pc m x -> n x
forall x. m x -> n x
nt ServerT api m
ServerT (OmitDocs :> api) m
s

instance (RoutesToPaths api) => RoutesToPaths (OmitDocs :> api) where
  getRoutes :: Forest PathSegment
getRoutes = forall api. RoutesToPaths api => Forest PathSegment
forall {k} (routes :: k).
RoutesToPaths routes =>
Forest PathSegment
getRoutes @api