-- move a node in the tree
deviceTreeMove :: Text -> Text -> DeviceTree -> STM DeviceTree
deviceTreeMove src tgt root = case (bkPath src, bkPath tgt) of
   ((x,xs),(y,ys))
      -- we only modify the subtree concerned by the move
      | x == y    -> do
         case Map.lookup x (deviceNodeChildren root) of
            Nothing -> error "deviceTreeLookup: source node doesn't exists"
            Just p  -> deviceTreeMove xs ys p
      | otherwise -> do
         case deviceTreeLookup src root of
            Nothing -> error "deviceTreeLookup: source node doesn't exists"
            Just n  -> deviceTreeInsert tgt n (deviceTreeRemove src root)

-- lookup for a node
deviceTreeMove :: Text -> Text -> DeviceTree -> STM DeviceTree
deviceTreeMove src tgt root = case (bkPath src, bkPath tgt) of
   ((x,xs),(y,ys))
      -- we only modify the subtree concerned by the move
      | x == y    -> do
         case Map.lookup x (deviceNodeChildren root) of
            Nothing -> error "deviceTreeLookup: source node doesn't exists"
            Just p  -> deviceTreeMove xs ys p
      | otherwise -> do
         case deviceTreeLookup src root of
            Nothing -> error "deviceTreeLookup: source node doesn't exists"
            Just n  -> deviceTreeInsert tgt n (deviceTreeRemove src root)

-- lookup for a node
deviceTreeMove src tgt root = case (bkPath src, bkPath tgt) of
   ((x,xs),(y,ys))
      -- we only modify the subtree concerned by the move
      | x == y    -> do
         case Map.lookup x (deviceNodeChildren root) of
            Nothing -> error "deviceTreeLookup: source node doesn't exists"
            Just p  -> deviceTreeMove xs ys p
      | otherwise -> do
         case deviceTreeLookup src root of
            Nothing -> error "deviceTreeLookup: source node doesn't exists"
            Just n  -> deviceTreeInsert tgt n (deviceTreeRemove src root)

-- lookup for a node