Changeset 8699 for src/DrIFT/YAML.hs

Show
Ignore:
Timestamp:
01/16/06 13:08:43 (3 years ago)
Author:
audreyt
Message:

* DrIFT.YAML: Do not die when the Typeable instance is undefined.

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • src/DrIFT/YAML.hs

    r8694 r8699  
    99import UTF8 
    1010import Data.Typeable 
     11import Control.Exception 
    1112 
    1213type YAMLClass = String 
     
    1617showYaml :: YAML a => a -> IO String 
    1718showYaml x = do 
    18     rv <- emitYaml =<< asYAML x 
     19    node    <- asYAML x 
     20    rv      <- emitYaml node 
    1921    case rv of 
    2022        Left e  -> error e 
     
    2325class Typeable a => YAML a where 
    2426    asYAML :: a -> IO YamlNode 
    25     asYAML x | ty == "()" = return nilNode 
    26              | otherwise  = return $ mkTagNode (tagHs ty) YamlNil 
    27         where 
    28         ty = (reverse (takeWhile (/= '.') (reverse (show (typeOf x))))) 
     27    asYAML x = do 
     28        ty <- Control.Exception.handle (const $ return "()") $ 
     29            evaluate (reverse (takeWhile (/= '.') (reverse (show (typeOf x))))) 
     30        return $ case ty of 
     31            "()" -> nilNode 
     32            _    -> mkTagNode (tagHs ty) YamlNil 
    2933 
    3034asYAMLseq :: YAMLClass -> [IO YAMLVal] -> IO YamlNode 
     
    4852tagHs :: YAMLClass -> String 
    4953tagHs = ("tag:hs:" ++) 
     54 
     55instance YAML () where 
    5056 
    5157instance YAML Int where