#[cfg(feature = "tooling")]
mod example {
use anyhow::Context;
use force::api::RestOperation;
use force::api::tooling::{CompletionsType, RunTestsRequest, TestItem};
use force::auth::ClientCredentials;
use force::client::ForceClientBuilder;
use force::types::QueryResult;
fn required_env(name: &str) -> anyhow::Result<String> {
std::env::var(name).with_context(|| format!("{name} environment variable not set"))
}
pub async fn main() -> anyhow::Result<()> {
tracing_subscriber::fmt::init();
let client_id = required_env("SF_CLIENT_ID")?;
let client_secret = required_env("SF_CLIENT_SECRET")?;
let my_domain_url = required_env("SF_MY_DOMAIN_URL")?;
println!("Authenticating with Salesforce...");
let auth = ClientCredentials::new_my_domain(client_id, client_secret, my_domain_url);
let client = ForceClientBuilder::new().authenticate(auth).build().await?;
let tooling = client.tooling();
println!("Authentication successful\n");
println!("=== Querying Apex Classes ===");
let result: QueryResult<serde_json::Value> = tooling
.query("SELECT Id, Name, Status FROM ApexClass LIMIT 5")
.await?;
println!("Found {} Apex classes", result.total_size);
for record in &result.records {
println!(
" {} - {}",
record["Name"].as_str().unwrap_or("unknown"),
record["Status"].as_str().unwrap_or("unknown")
);
}
println!("\n=== Execute Anonymous Apex ===");
let exec_result = tooling
.execute_anonymous("System.debug('Hello from force-rs!');")
.await?;
if exec_result.is_success() {
println!(" Execution successful!");
} else if exec_result.is_compile_error() {
println!(
" Compile error: {}",
exec_result.compile_problem.unwrap_or_default()
);
} else if exec_result.is_runtime_error() {
println!(
" Runtime error: {}",
exec_result.exception_message.unwrap_or_default()
);
}
println!("\n=== Describe ApexClass ===");
let describe = tooling.describe("ApexClass").await?;
println!(" {} has {} fields", describe.name, describe.fields.len());
for field in describe.fields.iter().take(5) {
println!(" {} ({:?})", field.name, field.type_);
}
let _ = (RunTestsRequest {
tests: vec![TestItem {
class_id: String::new(),
test_methods: None,
}],
max_failed_tests: None,
},);
println!("\n=== Code Completions ===");
let completions = tooling
.completions(CompletionsType::Apex, "System.d")
.await?;
println!(" Got {} completion entries", completions.completions.len());
println!("\nDone!");
Ok(())
}
}
#[cfg(feature = "tooling")]
#[tokio::main]
async fn main() -> anyhow::Result<()> {
example::main().await
}
#[cfg(not(feature = "tooling"))]
fn main() {
println!("This example requires the 'tooling' feature.");
println!("Run with: cargo run --example tooling --features tooling");
}