Skip to content
Browse files

Sort database before saving.

Make CblPkg an instance of Ord so that a CblDB ([CblPkg]) can be sorted.  Do
that sort when saving a database, which should have the effect that databases
are easier to compare, and also make it easier to keep a database under
version control.

The implementation of the Ord instance for CblPkg is based on sorting of
tuples.

Signed-off-by: Magnus Therning <magnus@therning.org>
  • Loading branch information...
1 parent 4646e39 commit 6cfe84c0fa9982fec58e94bf3e791dcfc26c15d1 @magthe committed Jul 15, 2012
Showing with 20 additions and 3 deletions.
  1. +20 −3 src/PkgDB.hs
View
23 src/PkgDB.hs
@@ -46,8 +46,25 @@ data CblPkg = CP String Pkg
type CblDB = [CblPkg]
--- instance Ord CblPkg where
--- compare = undefined
+instance Ord CblPkg where
+ compare
+ (CP n1 GhcPkg { version = v1 })
+ (CP n2 GhcPkg { version = v2 }) =
+ compare (n1, v1) (n2, v2)
+ compare (CP _ GhcPkg {}) _ = LT
+ compare _ (CP _ GhcPkg {}) = GT
+
+ compare
+ (CP n1 DistroPkg { version = v1, release = r1 })
+ (CP n2 DistroPkg { version = v2, release = r2 }) =
+ compare (n1, v1, r1) (n2, v2, r2)
+ compare (CP _ DistroPkg {}) _ = LT
+ compare _ (CP _ DistroPkg {}) = GT
+
+ compare
+ (CP n1 RepoPkg { version = v1, release = r1 })
+ (CP n2 RepoPkg { version = v2, release = r2 }) =
+ compare (n1, v1, r1) (n2, v2, r2)
-- {{{1 packages
pkgName :: CblPkg -> String
@@ -162,7 +179,7 @@ readDb fp = (flip CE.catch)
saveDb :: CblDB -> FilePath -> IO ()
saveDb db fp = writeFile fp s
where
- s = unlines $ map encode db
+ s = unlines $ map encode $ sort db
-- {{{1 JSON instances
instance JSON CblPkg where

0 comments on commit 6cfe84c

Please sign in to comment.
Something went wrong with that request. Please try again.