PmcClient

Struct PmcClient 

Source
pub struct PmcClient { /* private fields */ }
Expand description

Client for interacting with PMC (PubMed Central) API

Implementations§

Source§

impl PmcClient

Source

pub fn new() -> Self

Create a new PMC client with default configuration

Uses default NCBI rate limiting (3 requests/second) and no API key. For production use, consider using with_config() to set an API key.

§Example
use pubmed_client::PmcClient;

let client = PmcClient::new();
Source

pub fn get_pmc_config(&self) -> &ClientConfig

Source

pub fn get_tar_client_config(&self) -> &ClientConfig

Source

pub fn with_config(config: ClientConfig) -> Self

Create a new PMC client with custom configuration

§Arguments
  • config - Client configuration including rate limits, API key, etc.
§Example
use pubmed_client::{PmcClient, ClientConfig};

let config = ClientConfig::new()
    .with_api_key("your_api_key_here")
    .with_email("researcher@university.edu");

let client = PmcClient::with_config(config);
Source

pub fn with_client(client: Client) -> Self

Create a new PMC client with custom HTTP client and default configuration

§Arguments
  • client - Custom reqwest client with specific configuration
§Example
use pubmed_client::PmcClient;
use reqwest::Client;
use std::time::Duration;

let http_client = Client::builder()
    .timeout(Duration::from_secs(30))
    .build()
    .unwrap();

let client = PmcClient::with_client(http_client);
Source

pub fn with_base_url(self, base_url: String) -> Self

Set a custom base URL for the PMC API

§Arguments
  • base_url - The base URL for the PMC API
Source

pub async fn fetch_full_text(&self, pmcid: &str) -> Result<PmcArticle>

Fetch full text from PMC using PMCID

§Arguments
  • pmcid - PMC ID (with or without “PMC” prefix)
§Returns

Returns a Result<PmcArticle> containing the structured full text

§Errors
  • ParseError::PmcNotAvailable - If PMC full text is not available
  • PubMedError::RequestError - If the HTTP request fails
  • ParseError::XmlError - If XML parsing fails
§Example
use pubmed_client::PmcClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = PmcClient::new();
    let full_text = client.fetch_full_text("PMC7906746").await?;
    println!("Title: {}", full_text.title);
    println!("Sections: {}", full_text.sections.len());
    Ok(())
}
Source

pub async fn fetch_xml(&self, pmcid: &str) -> Result<String>

Fetch raw XML content from PMC

§Arguments
  • pmcid - PMC ID (with or without “PMC” prefix)
§Returns

Returns a Result<String> containing the raw XML content

Source

pub async fn check_pmc_availability(&self, pmid: &str) -> Result<Option<String>>

Check if PMC full text is available for a given PMID

§Arguments
  • pmid - PubMed ID
§Returns

Returns Result<Option<String>> containing the PMCID if available

§Example
use pubmed_client::PmcClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = PmcClient::new();
    if let Some(pmcid) = client.check_pmc_availability("33515491").await? {
        println!("PMC available: {}", pmcid);
        let full_text = client.fetch_full_text(&pmcid).await?;
        println!("Title: {}", full_text.title);
    } else {
        println!("PMC not available");
    }
    Ok(())
}
Source

pub async fn is_oa_subset(&self, pmcid: &str) -> Result<OaSubsetInfo>

Check if a PMC article is in the OA (Open Access) subset

The OA subset contains articles with programmatic access to full-text XML. Some publishers restrict programmatic access even though the article may be viewable on the PMC website.

§Arguments
  • pmcid - PMC ID (with or without “PMC” prefix)
§Returns

Returns Result<OaSubsetInfo> containing detailed information about OA availability

§Example
use pubmed_client::PmcClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = PmcClient::new();
    let oa_info = client.is_oa_subset("PMC7906746").await?;

    if oa_info.is_oa_subset {
        println!("Article is in OA subset");
        if let Some(link) = oa_info.download_link {
            println!("Download: {}", link);
        }
    } else {
        println!("Article is NOT in OA subset");
        if let Some(code) = oa_info.error_code {
            println!("Reason: {}", code);
        }
    }
    Ok(())
}
Source

pub async fn download_and_extract_tar<P: AsRef<Path>>( &self, pmcid: &str, output_dir: P, ) -> Result<Vec<String>>

Download and extract tar.gz file for a PMC article using the OA API

§Arguments
  • pmcid - PMC ID (with or without “PMC” prefix)
  • output_dir - Directory to extract the tar.gz contents to
§Returns

Returns a Result<Vec<String>> containing the list of extracted file paths

§Errors
  • ParseError::InvalidPmid - If the PMCID format is invalid
  • PubMedError::RequestError - If the HTTP request fails
  • ParseError::IoError - If file operations fail
  • ParseError::PmcNotAvailable - If the article is not available in OA
§Example
use pubmed_client::PmcClient;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = PmcClient::new();
    let output_dir = Path::new("./extracted_articles");
    let files = client.download_and_extract_tar("PMC7906746", output_dir).await?;

    for file in files {
        println!("Extracted: {}", file);
    }
    Ok(())
}
Source

pub async fn extract_figures_with_captions<P: AsRef<Path>>( &self, pmcid: &str, output_dir: P, ) -> Result<Vec<ExtractedFigure>>

Download, extract tar.gz file, and match figures with their captions from XML

§Arguments
  • pmcid - PMC ID (with or without “PMC” prefix)
  • output_dir - Directory to extract the tar.gz contents to
§Returns

Returns a Result<Vec<ExtractedFigure>> containing figures with both XML metadata and file paths

§Errors
  • ParseError::InvalidPmid - If the PMCID format is invalid
  • PubMedError::RequestError - If the HTTP request fails
  • ParseError::IoError - If file operations fail
  • ParseError::PmcNotAvailable - If the article is not available in OA
§Example
use pubmed_client::PmcClient;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = PmcClient::new();
    let output_dir = Path::new("./extracted_articles");
    let figures = client.extract_figures_with_captions("PMC7906746", output_dir).await?;

    for figure in figures {
        println!("Figure {}: {}", figure.figure.id, figure.figure.caption);
        println!("File: {}", figure.extracted_file_path);
    }
    Ok(())
}
Source

pub async fn clear_cache(&self)

Clear all cached PMC data

§Example
use pubmed_client::PmcClient;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = PmcClient::new();
    client.clear_cache().await;
    Ok(())
}
Source

pub fn cache_entry_count(&self) -> u64

Get cache statistics

Returns the number of items in cache, or 0 if caching is disabled

§Example
use pubmed_client::PmcClient;

let client = PmcClient::new();
let count = client.cache_entry_count();
println!("Cache entries: {}", count);
Source

pub async fn sync_cache(&self)

Synchronize cache operations to ensure all pending operations are flushed

This is useful for testing to ensure cache statistics are accurate

Trait Implementations§

Source§

impl Clone for PmcClient

Source§

fn clone(&self) -> PmcClient

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Default for PmcClient

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<T> ErasedDestructor for T
where T: 'static,