This commit is contained in:
30
docs/snippets/rust/cli/basic_cli.rs
Normal file
30
docs/snippets/rust/cli/basic_cli.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
```rust title="basic_cli.rs"
|
||||
use std::process::Command;
|
||||
use anyhow::Result;
|
||||
|
||||
fn extract_with_cli(file_path: &str, output_format: &str) -> Result<String> {
|
||||
let output = Command::new("kreuzberg")
|
||||
.args(&["extract", file_path, "--format", output_format])
|
||||
.output()?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
anyhow::bail!("CLI error: {}", stderr);
|
||||
}
|
||||
|
||||
Ok(String::from_utf8(output.stdout)?)
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let document = "document.pdf";
|
||||
|
||||
let text_output = extract_with_cli(document, "text")?;
|
||||
println!("Extracted: {} characters", text_output.len());
|
||||
|
||||
let json_output = extract_with_cli(document, "json")?;
|
||||
let parsed: serde_json::Value = serde_json::from_str(&json_output)?;
|
||||
println!("Format: {}", parsed.get("format").unwrap_or(&"unknown".into()));
|
||||
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
39
docs/snippets/rust/cli/cli_with_config.rs
Normal file
39
docs/snippets/rust/cli/cli_with_config.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
```rust title="cli_with_config.rs"
|
||||
use std::process::Command;
|
||||
use anyhow::Result;
|
||||
|
||||
fn extract_with_config(file_path: &str, config_path: &str) -> Result<serde_json::Value> {
|
||||
let output = Command::new("kreuzberg")
|
||||
.args(&[
|
||||
"extract",
|
||||
file_path,
|
||||
"--config",
|
||||
config_path,
|
||||
"--format",
|
||||
"json",
|
||||
])
|
||||
.output()?;
|
||||
|
||||
if !output.status.success() {
|
||||
let stderr = String::from_utf8_lossy(&output.stderr);
|
||||
anyhow::bail!("CLI error: {}", stderr);
|
||||
}
|
||||
|
||||
let result: serde_json::Value = serde_json::from_slice(&output.stdout)?;
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let config_file = "kreuzberg.toml";
|
||||
let document = "document.pdf";
|
||||
|
||||
println!("Extracting {} with config {}", document, config_file);
|
||||
let result = extract_with_config(document, config_file)?;
|
||||
|
||||
println!("Content length: {}", result["content"].as_str().unwrap_or("").len());
|
||||
println!("Format: {}", result["format"].as_str().unwrap_or("unknown"));
|
||||
println!("Languages: {}", result["languages"].to_string());
|
||||
|
||||
Ok(())
|
||||
}
|
||||
```
|
||||
Reference in New Issue
Block a user