pub struct PmcClient { /* private fields */ }Expand description
Client for interacting with PMC (PubMed Central) API
Implementations§
Source§impl PmcClient
impl PmcClient
Sourcepub fn new() -> Self
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();pub fn get_pmc_config(&self) -> &ClientConfig
pub fn get_tar_client_config(&self) -> &ClientConfig
Sourcepub fn with_config(config: ClientConfig) -> Self
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);Sourcepub fn with_client(client: Client) -> Self
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);Sourcepub fn with_base_url(self, base_url: String) -> Self
pub fn with_base_url(self, base_url: String) -> Self
Sourcepub async fn fetch_full_text(&self, pmcid: &str) -> Result<PmcArticle>
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 availablePubMedError::RequestError- If the HTTP request failsParseError::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(())
}Sourcepub async fn check_pmc_availability(&self, pmid: &str) -> Result<Option<String>>
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(())
}Sourcepub async fn is_oa_subset(&self, pmcid: &str) -> Result<OaSubsetInfo>
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(())
}Sourcepub async fn download_and_extract_tar<P: AsRef<Path>>(
&self,
pmcid: &str,
output_dir: P,
) -> Result<Vec<String>>
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 invalidPubMedError::RequestError- If the HTTP request failsParseError::IoError- If file operations failParseError::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(())
}Sourcepub async fn extract_figures_with_captions<P: AsRef<Path>>(
&self,
pmcid: &str,
output_dir: P,
) -> Result<Vec<ExtractedFigure>>
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 invalidPubMedError::RequestError- If the HTTP request failsParseError::IoError- If file operations failParseError::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(())
}Sourcepub async fn clear_cache(&self)
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(())
}Sourcepub fn cache_entry_count(&self) -> u64
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);Sourcepub async fn sync_cache(&self)
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§
Auto Trait Implementations§
impl Freeze for PmcClient
impl !RefUnwindSafe for PmcClient
impl Send for PmcClient
impl Sync for PmcClient
impl Unpin for PmcClient
impl !UnwindSafe for PmcClient
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().