Se stai costruendo una pipeline di elaborazione documentale in Rust, conosci già il meccanismo: recuperare dati, convertire formati, produrre output pulito — mantenendo bassa la latenza e alto il throughput. L'API REST di AnyMD si abbina naturalmente all'ecosistema Rust perché entrambi sono stati progettati con le stesse priorità: prestazioni, sicurezza e overhead minimo.
Questo articolo mostra come convertire documenti in Markdown da Rust usando reqwest, tokio, e serde, con benchmark che dimostrano perché la combinazione client Rust + server Rust offre la latenza end-to-end più bassa che puoi trovare. Dai un'occhiata anche ai nostri tutorial Node.js, Python, e CLI per altri linguaggi.
Perché Rust + AnyMD?
Il server di conversione di AnyMD è scritto in Rust (Axum + Tokio). Quando anche il client è in Rust, elimini completamente l'overhead tra linguaggi — niente GIL di Python, niente latenza dell'event loop di Node.js, niente pause del GC di Go. Solo I/O asincrono diretto tra due runtime fortemente ottimizzati.
Conversione di un singolo documento
use reqwest::Client;
use std::path::Path;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let api_key = "your-api-key";
let file = Path::new("report.docx");
let file_bytes = tokio::fs::read(file).await?;
let part = reqwest::multipart::Part::bytes(file_bytes)
.file_name("report.docx");
let form = reqwest::multipart::Form::new()
.part("file", part);
let resp = client
.post("https://anymd.net/api/convert")
.header("Authorization", format!("Bearer {api_key}"))
.multipart(form)
.send()
.await?;
let markdown = resp.text().await?;
println!("Got {} chars of Markdown", markdown.len());
Ok(())
}
Conversione in batch con task tokio
Quando hai una directory di documenti, avvia task concorrenti con un semaforo per controllare la concorrenza.
use reqwest::Client;
use std::sync::Arc;
use std::path::PathBuf;
use tokio::sync::Semaphore;
use tokio::fs;
#[derive(serde::Serialize, serde::Deserialize)]
struct ConversionResult {
file: String,
ok: bool,
chars: usize,
}
async fn convert_one(
client: &Client,
path: &Path,
api_key: &str,
) -> ConversionResult {
let bytes = match fs::read(path).await {
Ok(b) => b,
Err(e) => return ConversionResult {
file: path.to_string_lossy().to_string(),
ok: false,
chars: 0,
},
};
let part = reqwest::multipart::Part::bytes(bytes)
.file_name(path.file_name().unwrap().to_string_lossy().to_string());
let form = reqwest::multipart::Form::new().part("file", part);
match client
.post("https://anymd.net/api/convert")
.header("Authorization", format!("Bearer {api_key}"))
.multipart(form)
.send()
.await
{
Ok(resp) if resp.status().is_success() => {
let text = resp.text().await.unwrap_or_default();
ConversionResult {
file: path.to_string_lossy().to_string(),
ok: true,
chars: text.len(),
}
}
Ok(resp) => ConversionResult {
file: path.to_string_lossy().to_string(),
ok: false,
chars: 0,
},
Err(e) => ConversionResult {
file: path.to_string_lossy().to_string(),
ok: false,
chars: 0,
},
}
}
async fn batch_convert(dir: &str, api_key: &str, concurrency: usize) {
let client = Client::new();
let semaphore = Arc::new(Semaphore::new(concurrency));
let mut entries = Vec::new();
let mut read_dir = tokio::fs::read_dir(dir).await.unwrap();
while let Some(entry) = read_dir.next_entry().await.unwrap() {
entries.push(entry.path());
}
let mut handles = Vec::new();
for path in entries {
let permit = semaphore.clone().acquire_owned().await.unwrap();
handles.push(tokio::spawn(async move {
let result = convert_one(&client, &path, api_key).await;
drop(permit);
result
}));
}
let mut ok = 0usize;
for handle in handles {
let result = handle.await.unwrap();
if result.ok {
ok += 1;
}
println!("{} — {}", result.file, if result.ok { "✓" } else { "✗" });
}
println!("{}/{} converted", ok, entries.len());
}
#[tokio::main]
async fn main() {
batch_convert("./documents", "your-api-key", 10).await;
}
Usare AnyMD per una pipeline di ingestione RAG in Rust
Ecco una pipeline realistica: monitora una directory per nuovi file, convertili in Markdown, quindi esegui chunking ed embedding per la ricerca vettoriale.
Formati supportati
| Categoria | Formati |
|---|---|
| Documenti | PDF, DOCX, ODT, RTF, EPUB, CBR/CBZ |
| Presentazioni | PPTX, ODP |
| Fogli di calcolo | XLSX, ODS, CSV (renderizzati come tabelle) |
| Web | HTML, Markdown (pass-through) |
| Immagini | Estrazione OCR da JPG, PNG, TIFF |
Prezzi
| Piano | Pagine/mese | Dimensione massima del file | Prezzo |
|---|---|---|---|
| Gratuito | 100 | 10 MB | $0 |
| Starter | 500 | 25 MB | $19 |
| Pro | 5,000 | 50 MB | $99 |
| Enterprise | Personalizzato | Personalizzato | Personalizzato |
Le conversioni Rust beneficiano della stessa politica di conservazione zero e di una latenza inferiore al secondo, come per ogni altro linguaggio. Consulta la documentazione API per la specifica completa e controlla i prezzi per i dettagli dei piani.
Ottieni la tua chiave API gratuita — 100 pagine/mese, nessuna carta di credito richiesta.