         
     }
     
+    var parsingError: NSError?
     
     
 }



extension Parser {
    
    func parserDidEndDocument(_ parser: XMLParser) {
        
        guard let feedType = self.feedType else {
            self.result?(Result.failure(ParserError.feedNotFound.value))
            return
        }
        
        switch feedType {
            
        case .Atom:
            self.result?(Result.atom(self.atomFeed!))
            
        case .RSS1, .RSS2:
            self.result?(Result.rss(self.rssFeed!))
            
        }
        
    }
    
    func parser(_ parser: XMLParser, didStartElement elementName: String, namespaceURI: String?, qualifiedName qName: String?, attributes attributeDict: [String : String]) {
        

     
     */
    func parse(_ result: @escaping (Result) -> Void) {
        
        self.result = result
        
        self.parse()
        
        if let error = parsingError {
            self.result?(Result.failure(error))
            return
        }
        
        guard let feedType = self.feedType else {
            self.result?(Result.failure(ParserError.feedNotFound.value))
            return
        }
        
        switch feedType {
            
        case .Atom:
            self.result?(Result.atom(self.atomFeed!))
            
        case .RSS1, .RSS2:
            self.result?(Result.rss(self.rssFeed!))
            
        }
        
    }
    
    
