1
votes

I need a library to encode data in cp1251 to work with an api which use it.

I found only 2 libs for this purpose. The encoding lib and text-icu lib. The first one looks better just because it can be statically linked to my program.

But I can't even compile it! I am using stack so I add encoding to build-deps in my .cabal file, and then stack solver update my stack yaml file with encoding-0.8 in extra-deps.

but after running stack build I a got an error

Data/Encoding/Preprocessor/XMLMapping.hs:11:8:
    Could not find module ‘Text.XML.HaXml.Types’
    Use -v to see a list of the files searched for.

Data/Encoding/Preprocessor/XMLMappingBuilder.hs:15:8:
    Could not find module ‘Text.XML.HaXml.OneOfN’
    Use -v to see a list of the files searched for.

Data/Encoding/Preprocessor/XMLMappingBuilder.hs:16:8:
    Could not find module ‘Text.XML.HaXml.XmlContent’
    Use -v to see a list of the files searched for.

I tried to use older version of HaXml and encoding but got the same error. For example I tried to use encoding-0.6.7 with HaXml-1.22.3, after reading changelog https://hackage.haskell.org/package/encoding-0.7.0.2/changelog but got the same error. In hackage documentation all this modules which encoding can't import exists.

How can I compile this lib? I am using stack 1.0.4, lts-5.6 and ghc-7.10.3 on linux mint x64

stack.yaml

flags: {}
extra-package-dbs: []
packages:
- '.'
extra-deps:
- encoding-0.8
- text-1.2.2.0
resolver: lts-5.6

cabal:

name:                hapidry
version:             0.1.1.0
synopsis:            Initial project template from stack
description:         Please see README.md
homepage:            -
license:             GPL-2
license-file:        LICENSE
author:              -
maintainer:          -
copyright:           GPL
category:            network
build-type:          Simple
-- extra-source-files:
cabal-version:       >=1.10

library
  hs-source-dirs:      src
  exposed-modules:     Lib
  build-depends:       base >= 4.7 && < 5
  default-language:    Haskell2010

executable hapidry-exe
  hs-source-dirs:      app
  main-is:             Main.hs
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  build-depends:       base
                     , hapidry
                     , wreq
                     , ConfigFile
                     , aeson
                     , lens
                     , cryptohash
                     , binary
                     , mtl
                     , base16-bytestring
                     , bytestring
                     , containers
                     , text
                     , lens-aeson
                     , data-default
                     , optparse-applicative
                     , encoding
  default-language:    Haskell2010

test-suite hapidry-test
  type:                exitcode-stdio-1.0
  hs-source-dirs:      test
  main-is:             Spec.hs
  build-depends:       base
                     , hapidry
  ghc-options:         -threaded -rtsopts -with-rtsopts=-N
  default-language:    Haskell2010

source-repository head
  type:     git
  location: -
1
could you provide your cabal file and stack.yaml fileepsilonhalbe
Yes. I have added them to the question.capgelka
I have once reproduced the error - then tried to install encoding alone (note: this takes ages to build), then tried to add all your libraries to the lib part (which seems reasonable) then it built, and after moviing the dependencies back to the app part it keeps on building - so now I cannot reproduce your errorepsilonhalbe
Thanks for your help! I reproduced it even in new vm created spectial for this( Finally I wrote my own function github.com/capgelka/hapidry/blob/master/src/Api.hs#L78 for converting to cp1251, it was much simpleer then I expected.capgelka
Could you add this as an answer?epsilonhalbe

1 Answers

0
votes

Finally I wrote my own funtion for encoding. It looks like acceptable workaround. And functions for another one-byte encodings can be implemented in the same way.

toCP1251 :: Text -> B.ByteString
toCP1251 = B.pack . T.unpack . T.map replace where
  replace l = case (Map.lookup l table) of
      (Just x) -> x
      (Nothing) -> l

  table = Map.fromList $ zip rus cpCodes
  cpCodes = map toEnum (168:184:[192 .. 255]) :: [Char]
  rus =  ['Ё', 'ё', 'А', 'Б', 'В', 'Г', 'Д', 'Е', 'Ж', 'З', 'И', 'Й', 'К', 'Л', 'М',
         'Н', 'О', 'П', 'Р', 'С', 'Т', 'У', 'Ф', 'Х', 'Ц', 'Ч', 'Ш', 'Щ', 'Ъ', 'Ы',
         'Ь', 'Э', 'Ю', 'Я', 'а', 'б', 'в', 'г', 'д', 'е', 'ж', 'з', 'и', 'й', 'к',
         'л', 'м', 'н', 'о', 'п', 'р', 'с', 'т', 'у', 'ф', 'х', 'ц', 'ч', 'ш', 'щ',
         'ъ', 'ы', 'ь', 'э', 'ю', 'я']  :: [Char]