From 24f897be04b5da8d7a1e27b9e6af028907068102 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20H=C3=B8jberg?= Date: Wed, 6 Apr 2022 14:55:07 -0400 Subject: [PATCH] Move OperatingSystem to Lib and add CodebaseApi --- src/Code/CodebaseApi.elm | 37 ++++++++++++++++++++++++++ src/Code/EntityId.elm | 19 +++++++++++++ src/Code/Workspace.elm | 3 ++- src/Env.elm | 47 +++++++++++++-------------------- src/UI/KeyboardShortcut.elm | 2 +- src/UI/KeyboardShortcut/Key.elm | 2 +- src/UnisonLocal/App.elm | 3 ++- 7 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 src/Code/CodebaseApi.elm create mode 100644 src/Code/EntityId.elm diff --git a/src/Code/CodebaseApi.elm b/src/Code/CodebaseApi.elm new file mode 100644 index 0000000..e14c96d --- /dev/null +++ b/src/Code/CodebaseApi.elm @@ -0,0 +1,37 @@ +module Code.CodebaseApi exposing (..) + +import Code.EntityId exposing (EntityId) +import Code.FullyQualifiedName exposing (FQN) +import Code.Perspective exposing (Perspective) +import Code.Syntax as Syntax +import Lib.Api as Api + + + +{- + + CodebaseApi + =========== + + The CodebaseApi module, describes the endpoints used for the Code library to + connect to a codebase, but is merely that; a description. Consumers of the Code + library will need to provide an implementation of `ToApiEndpointUrl` in order + to perform the actual HTTP requests. + +-} + + +type CodebaseEndpoint + = Find + { perspective : Perspective + , withinFqn : Maybe FQN + , limit : Int + , sourceWidth : Syntax.Width + , query : String + } + | Browse { perspective : Perspective, namespaceId : Maybe EntityId } + | Definition { perspective : Perspective, definitionId : EntityId } + + +type alias ToApiEndpointUrl = + CodebaseEndpoint -> Api.EndpointUrl diff --git a/src/Code/EntityId.elm b/src/Code/EntityId.elm new file mode 100644 index 0000000..aac0bc6 --- /dev/null +++ b/src/Code/EntityId.elm @@ -0,0 +1,19 @@ +module Code.EntityId exposing (EntityId, toString) + +import Code.FullyQualifiedName as FQN exposing (FQN) +import Code.Hash as Hash exposing (Hash) + + +type EntityId + = HashId Hash + | NameId FQN + + +toString : EntityId -> String +toString id = + case id of + HashId h -> + Hash.toString h + + NameId fqn -> + FQN.toString fqn diff --git a/src/Code/Workspace.elm b/src/Code/Workspace.elm index 8aa5059..b7d4b48 100644 --- a/src/Code/Workspace.elm +++ b/src/Code/Workspace.elm @@ -24,6 +24,7 @@ import Env exposing (Env) import Html exposing (Html, article, div, section) import Html.Attributes exposing (class, id) import Http +import Lib.OperatingSystem as OperatingSystem import Task import UI.Button as Button import UI.Icon as Icon @@ -356,7 +357,7 @@ handleKeyboardShortcut env ({ workspaceItems } as model) shortcut = ( model, Cmd.none, ShowFinderRequest Nothing ) KeyboardShortcut.Chord Meta (K _) -> - if env.operatingSystem == Env.MacOS then + if env.operatingSystem == OperatingSystem.MacOS then ( model, Cmd.none, ShowFinderRequest Nothing ) else diff --git a/src/Env.elm b/src/Env.elm index d9ce724..c1159ee 100644 --- a/src/Env.elm +++ b/src/Env.elm @@ -2,16 +2,11 @@ module Env exposing (..) import Api exposing (ApiBasePath(..)) import Browser.Navigation as Nav +import Code.CodebaseApi as CodebaseApi +import Code.Config import Code.Perspective exposing (Perspective) - - -type OperatingSystem - = MacOS - | Windows - | Linux - | Android - | IOS - | Unknown +import Lib.Api +import Lib.OperatingSystem as OS exposing (OperatingSystem) type alias Env = @@ -32,7 +27,7 @@ type alias Flags = init : Flags -> Nav.Key -> Perspective -> Env init flags navKey perspective = - { operatingSystem = operatingSystemFromString flags.operatingSystem + { operatingSystem = OS.fromString flags.operatingSystem , basePath = flags.basePath , apiBasePath = ApiBasePath flags.apiBasePath , navKey = navKey @@ -40,23 +35,17 @@ init flags navKey perspective = } -operatingSystemFromString : String -> OperatingSystem -operatingSystemFromString rawOs = - case rawOs of - "macOS" -> - MacOS - - "iOS" -> - IOS - - "Windows" -> - Windows +toCodeConfig : CodebaseApi.ToApiEndpointUrl -> Env -> Code.Config.Config +toCodeConfig toApiEndpointUrl env = + let + (ApiBasePath path) = + env.apiBasePath - "Android" -> - Android - - "Linux" -> - Linux - - _ -> - Unknown + apiBasePath = + Lib.Api.ApiBasePath path + in + { operatingSystem = env.operatingSystem + , perspective = env.perspective + , toApiEndpointUrl = toApiEndpointUrl + , apiBasePath = apiBasePath + } diff --git a/src/UI/KeyboardShortcut.elm b/src/UI/KeyboardShortcut.elm index 53a1f72..b4a6ed7 100644 --- a/src/UI/KeyboardShortcut.elm +++ b/src/UI/KeyboardShortcut.elm @@ -37,9 +37,9 @@ module UI.KeyboardShortcut exposing (..) -import Env exposing (OperatingSystem) import Html exposing (Html, span, text) import Html.Attributes exposing (class, classList) +import Lib.OperatingSystem exposing (OperatingSystem) import List.Nonempty as NEL import Process import Task diff --git a/src/UI/KeyboardShortcut/Key.elm b/src/UI/KeyboardShortcut/Key.elm index 80680cb..7a38f02 100644 --- a/src/UI/KeyboardShortcut/Key.elm +++ b/src/UI/KeyboardShortcut/Key.elm @@ -25,8 +25,8 @@ module UI.KeyboardShortcut.Key exposing , view ) -import Env exposing (OperatingSystem(..)) import Json.Decode as Decode +import Lib.OperatingSystem exposing (OperatingSystem(..)) type LetterCase diff --git a/src/UnisonLocal/App.elm b/src/UnisonLocal/App.elm index 5f59645..53104c9 100644 --- a/src/UnisonLocal/App.elm +++ b/src/UnisonLocal/App.elm @@ -13,11 +13,12 @@ import Code.Namespace as Namespace exposing (NamespaceDetails) import Code.Perspective as Perspective exposing (Perspective(..)) import Code.Workspace as Workspace import Code.Workspace.WorkspaceItems as WorkspaceItems -import Env exposing (Env, OperatingSystem(..)) +import Env exposing (Env) import Html exposing (Html, a, div, h1, h2, h3, nav, p, section, span, strong, text) import Html.Attributes exposing (class, classList, href, id, rel, target, title) import Html.Events exposing (onClick) import Http +import Lib.OperatingSystem exposing (OperatingSystem(..)) import Lib.Util as Util import RemoteData import UI