Initial commit: Add curl-to-code-cli project
This commit is contained in:
80
.curl_to_code/generators/rust_gen.py
Normal file
80
.curl_to_code/generators/rust_gen.py
Normal file
@@ -0,0 +1,80 @@
|
||||
import json
|
||||
from ..parser.models import ParsedCurl
|
||||
|
||||
|
||||
def generate_rust(parsed: ParsedCurl) -> str:
|
||||
"""Generate Rust reqwest code from parsed curl command."""
|
||||
lines = [
|
||||
"use reqwest;",
|
||||
"use std::collections::HashMap;",
|
||||
"use tokio;",
|
||||
"",
|
||||
"#[tokio::main]",
|
||||
"async fn main() -> Result<(), reqwest::Error> {",
|
||||
f' let url = "{parsed.url}"',
|
||||
"",
|
||||
]
|
||||
|
||||
if parsed.headers or parsed.user_agent:
|
||||
lines.append(" let mut headers = HashMap::new();")
|
||||
for h in parsed.headers:
|
||||
lines.append(f' headers.insert("{h.key}", "{h.value}");')
|
||||
if parsed.user_agent:
|
||||
lines.append(f' headers.insert("User-Agent", "{parsed.user_agent}");')
|
||||
lines.append("")
|
||||
|
||||
method_name = parsed.method.value.lower()
|
||||
|
||||
if parsed.data:
|
||||
if parsed.data.data_type.value == "json" and parsed.data.json_data:
|
||||
json_str = json.dumps(parsed.data.json_data)
|
||||
lines.append(f" let json_data: serde_json::Value = {json_str}.parse().unwrap();")
|
||||
lines.append("")
|
||||
lines.append(" let client = reqwest::Client::new();")
|
||||
if parsed.headers or parsed.user_agent:
|
||||
lines.append(" let response = client")
|
||||
else:
|
||||
lines.append(" let response = reqwest::Client::new()")
|
||||
else:
|
||||
lines.append(f" let data = {repr(parsed.data.content)};")
|
||||
lines.append("")
|
||||
lines.append(" let client = reqwest::Client::new();")
|
||||
if parsed.headers or parsed.user_agent:
|
||||
lines.append(" let response = client")
|
||||
else:
|
||||
lines.append(" let response = reqwest::Client::new()")
|
||||
else:
|
||||
lines.append(" let client = reqwest::Client::new();")
|
||||
if parsed.headers or parsed.user_agent:
|
||||
lines.append(" let response = client")
|
||||
else:
|
||||
lines.append(" let response = reqwest::Client::new()")
|
||||
|
||||
lines.append(f" .{method_name}(url)")
|
||||
|
||||
if parsed.headers or parsed.user_agent:
|
||||
lines.append(" .headers(headers)")
|
||||
|
||||
if parsed.data and parsed.data.data_type.value == "json" and parsed.data.json_data:
|
||||
lines.append(" .json(&json_data)")
|
||||
elif parsed.data:
|
||||
lines.append(f" .body(data)")
|
||||
|
||||
if parsed.timeout:
|
||||
lines.append(f" .timeout(std::time::Duration::from_secs({parsed.timeout}))")
|
||||
|
||||
lines.append(" .send()")
|
||||
lines.append(" .await?;")
|
||||
lines.append("")
|
||||
lines.append(" if !response.status().is_success() {")
|
||||
f' eprintln!("HTTP error: {{}}", response.status());'
|
||||
lines.append(" return Ok(());")
|
||||
lines.append(" }")
|
||||
lines.append("")
|
||||
lines.append(" let text = response.text().await?;")
|
||||
lines.append(" println!(\"{}\", text);")
|
||||
lines.append("")
|
||||
lines.append(" Ok(())")
|
||||
lines.append("}")
|
||||
|
||||
return "\n".join(lines)
|
||||
Reference in New Issue
Block a user