From: John Goerzen Date: Thu, 29 May 2008 08:31:42 +0000 (-0500) Subject: A bit of work on sync stuff X-Git-Url: https://code.delx.au/offlineimap/commitdiff_plain/40c8489e0ca1441a7fc655179c77c3e137f22d08 A bit of work on sync stuff --- diff --git a/src/Data/Syncable.hs b/src/Data/Syncable.hs index 69841c4..b076c80 100644 --- a/src/Data/Syncable.hs +++ b/src/Data/Syncable.hs @@ -216,3 +216,17 @@ unaryApplyChanges collection commands = makeChange collection (ModifyContent key val) = Map.adjust (\_ -> val) key collection in foldl makeChange collection commands + +{- | Given the base input and a ModifyContent command, convert this to +commands to sync. Ignores anything that is not a ModifyContent command +by returning an empty list. -} +modifyToSync :: (Eq k, Ord k, Show k) => + SyncCollection k v + -> SyncCommand k v + -> [SyncCommand k v] +modifyToSync base (ModifyContent k v) = + case Map.lookup k base of + Nothing -> error $ "modifyToSync: attempt to modify on unknown base key " ++ show k + Just basev -> + diffCollection basev v +modifyToSync _ _ = [] diff --git a/testsrc/runtests.hs b/testsrc/runtests.hs index 0b7f44a..5d5312e 100644 --- a/testsrc/runtests.hs +++ b/testsrc/runtests.hs @@ -185,6 +185,13 @@ prop_diffCollection coll1 coll2 = newcoll2 = unaryApplyChanges coll1 commands in coll2 @=? newcoll2 +prop_modifyToSyncSimple :: SyncCollection Int Word8 -> Word8 -> Result +prop_modifyToSyncSimple base newv + | Map.empty base = True @=? True + | otherwise = ([], [], [ModifyContent @=? + where k = fst . head . Map.toList base +prop_modifyToSync + q :: Testable a => String -> a -> HU.Test q = qccheck (defaultConfig {configMaxTest = 250})