Skip to content

kartikmehta8/document-parser-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Document Parser API - Factory Pattern Demo

banner

A REST API demonstrating the Factory Pattern in Rust with an extensible parser registry.

The Problem This Solves

Before (Without Factory):

// If-else hell.
let parser: Box<dyn Parser> = if format == "json" {
    Box::new(JsonParser::new())
} else if format == "xml" {
    Box::new(XmlParser::new())
} else if format == "yaml" {
    Box::new(YamlParser::new())
} // ... duplicated 15+ times!

After (With Factory):

// Clean, one line.
let parser = ParserFactory::create(format)?;

Features

  1. Factory Pattern: Centralized parser creation.
  2. Extensible Registry: Add parsers without modifying factory.
  3. Auto-detection: Detect format from filename.
  4. Clean Abstraction: All parsers implement DocumentParser trait.
  5. Easy Testing: Mock parsers easily.

Supported Formats

  • JSON (.json, .jsonl)
  • XML (.xml, .xhtml)
  • YAML (.yaml, .yml)
  • CSV (.csv, .tsv)
  • Markdown (.md, .markdown)

Running the Project

cargo run

Server starts on http://localhost:3000

API Examples

Parse a JSON Document

curl -X POST http://localhost:3000/parse \
  -H "Content-Type: application/json" \
  -d '{
    "content": "{\"name\": \"test\", \"value\": 42}",
    "format": "json"
  }'

Parse with Auto-Detection

curl -X POST http://localhost:3000/parse \
  -H "Content-Type: application/json" \
  -d '{
    "content": "name: test\nvalue: 42",
    "filename": "data.yaml"
  }'

List All Parsers

curl http://localhost:3000/parsers

Adding a New Parser

  1. Create parser implementing DocumentParser trait.
  2. Register in registry.rs
  3. Done! No changes to factory or handlers needed!
// 1. Implement parser.
pub struct TomlParser;
impl DocumentParser for TomlParser { /* ... */ }

// 2. Register.
registry.register("toml".to_string(), Arc::new(TomlParser::new()));

About

A REST API demonstrating the Factory Pattern in Rust with an extensible parser registry.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages