Skip to content

Commit

Permalink
Support toml-parser-2.0.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
glguy authored and jgm committed Feb 27, 2024
1 parent 4a02c9b commit fb673bf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 19 deletions.
9 changes: 4 additions & 5 deletions src/Typst/Evaluate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ import Typst.Syntax
import Typst.Types
import Typst.Util (makeFunction, nthArg)
import qualified Toml as Toml
import qualified Toml.FromValue as Toml
import qualified Toml.FromValue.Generic as Toml
import qualified Toml.Schema as Toml

-- import Debug.Trace

Expand Down Expand Up @@ -930,11 +929,11 @@ data PackageConfig = PackageConfig {

-- | Derived generically from record field names
instance Toml.FromValue Config where
fromValue = Toml.parseTableFromValue Toml.genericParseTable
fromValue = Toml.genericFromTable

-- | Derived generically from record field names
instance Toml.FromValue PackageConfig where
fromValue = Toml.parseTableFromValue Toml.genericParseTable
fromValue = Toml.genericFromTable

findPackageEntryPoint :: Monad m => Text -> MP m FilePath
findPackageEntryPoint modname = do
Expand Down Expand Up @@ -978,7 +977,7 @@ findPackageEntryPoint modname = do
(localDir </> subpath) ++ "\n" ++ (cacheDir </> subpath) ++
"\nCompile with typst compile to bring the package into your local cache."
-- TODO? fetch from CDN if not present in cache?
tomlString <- T.unpack . TE.decodeUtf8 <$> lift (loadBytes operations tomlPath)
tomlString <- TE.decodeUtf8 <$> lift (loadBytes operations tomlPath)
case Toml.decode tomlString of
Toml.Failure e -> fail (unlines ("Failure loading typst.toml" : e))
Toml.Success _warnings cfg -> -- ignores warnings like unused keys in TOML
Expand Down
2 changes: 1 addition & 1 deletion src/Typst/Module/Standard.hs
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ dataLoading =
makeFunction $ do
fp <- nthArg 1
t <- lift $ loadFileText fp
case Toml.decode (T.unpack t) of
case Toml.decode t of
Toml.Failure e -> fail (unlines ("toml errors:" : e))
Toml.Success _ v -> pure v
),
Expand Down
24 changes: 12 additions & 12 deletions src/Typst/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ import Data.Vector (Vector)
import qualified Data.Vector as V
import Text.Parsec
import qualified Toml
import qualified Toml.FromValue as Toml
import qualified Toml.Schema as Toml
import qualified Toml.Pretty as Toml
import qualified Text.PrettyPrint as P
import Text.Read (readMaybe)
Expand Down Expand Up @@ -129,18 +129,18 @@ instance FromJSON Val where
instance Toml.FromValue Val where
fromValue = pure . tomlToVal

tomlToVal :: Toml.Value -> Val
tomlToVal (Toml.Bool x) = VBoolean x
tomlToVal (Toml.Integer x) = VInteger x
tomlToVal (Toml.String x) = VString (T.pack x)
tomlToVal (Toml.Float x) = VFloat x
tomlToVal (Toml.TimeOfDay x) = VDateTime Nothing (Just (timeOfDayToTime x))
tomlToVal (Toml.Day x) = VDateTime (Just x) Nothing
tomlToVal (Toml.LocalTime x) = VDateTime (Just (localDay x)) (Just (timeOfDayToTime (localTimeOfDay x)))
tomlToVal (Toml.Array x) = VArray (V.fromList (map tomlToVal x))
tomlToVal (Toml.Table x) = VDict (OM.fromList [(Identifier (T.pack k), tomlToVal v) | (k,v) <- M.assocs x])
tomlToVal :: Toml.Value' a -> Val
tomlToVal (Toml.Bool' _ x) = VBoolean x
tomlToVal (Toml.Integer' _ x) = VInteger x
tomlToVal (Toml.Text' _ x) = VString x
tomlToVal (Toml.Double' _ x) = VFloat x
tomlToVal (Toml.TimeOfDay' _ x) = VDateTime Nothing (Just (timeOfDayToTime x))
tomlToVal (Toml.Day' _ x) = VDateTime (Just x) Nothing
tomlToVal (Toml.LocalTime' _ x) = VDateTime (Just (localDay x)) (Just (timeOfDayToTime (localTimeOfDay x)))
tomlToVal (Toml.List' _ x) = VArray (V.fromList (map tomlToVal x))
tomlToVal (Toml.Table' _ (Toml.MkTable x)) = VDict (OM.fromList [(Identifier k, tomlToVal v) | (k,(_,v)) <- M.assocs x])
-- typst specifies that unsupported datetimes map to strings and we don't have a place for the timezone
tomlToVal v@Toml.ZonedTime{} = VString (T.pack (show (Toml.prettyValue v)))
tomlToVal v@Toml.ZonedTime'{} = VString (T.pack (show (Toml.prettyValue v)))

data ValType
= TNone
Expand Down
2 changes: 1 addition & 1 deletion typst.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ library
scientific,
xml-conduit,
yaml,
toml-parser ^>= 1.3.0.0,
toml-parser ^>= 2.0.0.0,
regex-tdfa,
array,
time,
Expand Down

0 comments on commit fb673bf

Please sign in to comment.