xdk-rs 0.2.0

Async Rust client for the X (Twitter) API: OAuth1, OAuth2 PKCE, bearer tokens, media upload, streaming. xdk is X's SDK name; this is an independent project, not affiliated with X Corp.
Documentation
//! Shared by the examples: credentials come from the environment, and a
//! failure exits with the code the crate maps the error to.

use xdk::Error;

/// Reads `name`, or fails as a missing credential so the process exits with
/// the auth-required code.
pub fn required_env(name: &str) -> xdk::Result<String> {
    std::env::var(name)
        .ok()
        .filter(|value| !value.is_empty())
        .ok_or_else(|| {
            Error::auth(format!(
                "{name} is not set; export it before running this example"
            ))
        })
}

/// Prints the error the way a CLI built on the crate would, then exits with
/// its code.
pub fn exit_with(err: Error) -> ! {
    eprintln!("{}: {err}", err.kind());
    if let Some(action) = err.next_action() {
        eprintln!("next: {action:?}");
    }
    if let Some(url) = err.docs_url() {
        eprintln!("see {url}");
    }
    std::process::exit(err.exit_code())
}