diff options
author | Kornel <kornel@geekhood.net> | 2020-02-23 13:59:45 +0000 |
---|---|---|
committer | Kornel <kornel@geekhood.net> | 2020-02-23 13:59:45 +0000 |
commit | c97d1afaf0badcf93a8de57eaed54fb42c85c962 (patch) | |
tree | 3ee7a86ec2a872857d6caad7024fbaeeaa1fd65e /docs_rs_client | |
parent | b0944ba4c1c15bbf49cb638e90a7849b88dc2f75 (diff) |
Moved
Diffstat (limited to 'docs_rs_client')
-rw-r--r-- | docs_rs_client/.gitignore | 1 | ||||
-rw-r--r-- | docs_rs_client/Cargo.toml | 16 | ||||
-rw-r--r-- | docs_rs_client/README.md | 3 | ||||
-rw-r--r-- | docs_rs_client/src/lib_docs_rs_client.rs | 45 |
4 files changed, 65 insertions, 0 deletions
diff --git a/docs_rs_client/.gitignore b/docs_rs_client/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/docs_rs_client/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/docs_rs_client/Cargo.toml b/docs_rs_client/Cargo.toml new file mode 100644 index 0000000..2fdcad5 --- /dev/null +++ b/docs_rs_client/Cargo.toml @@ -0,0 +1,16 @@ +[package] +edition = "2018" +name = "docs_rs_client" +version = "0.4.0" +authors = ["Kornel <kornel@geekhood.net>"] + +[lib] +name = "docs_rs_client" +path = "src/lib_docs_rs_client.rs" + +[dependencies] +serde = "1.0.104" +serde_json = "1.0.44" +serde_derive = "1.0.104" +simple_cache = { path = "../simple_cache", version = "0.7.0" } +tokio = { version = "0.2", features = ["macros"] } diff --git a/docs_rs_client/README.md b/docs_rs_client/README.md new file mode 100644 index 0000000..bf91c6c --- /dev/null +++ b/docs_rs_client/README.md @@ -0,0 +1,3 @@ +# Does it have docs.rs page? + +This is a tiny library that fetches and catches responses from docs.rs API to check if docs.rs has documentation pages for any given crate. This is used to display docs.rs links on crates.rs only where approprite. diff --git a/docs_rs_client/src/lib_docs_rs_client.rs b/docs_rs_client/src/lib_docs_rs_client.rs new file mode 100644 index 0000000..9e91caf --- /dev/null +++ b/docs_rs_client/src/lib_docs_rs_client.rs @@ -0,0 +1,45 @@ +use serde_derive::*; +pub use simple_cache::Error; +use simple_cache::TempCache; +use std::path::Path; + +pub struct DocsRsClient { + cache: TempCache<Option<Vec<BuildStatus>>>, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct BuildStatus { + build_status: bool, //true, + build_time: String, //"2018-03-26T08:57:46+02:00", + rustc_version: String, +} + +impl DocsRsClient { + pub fn new(cache_path: impl AsRef<Path>) -> Result<Self, Error> { + Ok(Self { + cache: TempCache::new(cache_path.as_ref())?, + }) + } + + pub async fn builds(&self, crate_name: &str, version: &str) -> Result<bool, Error> { + let res = self.build_status(crate_name, version).await?; + Ok(res.and_then(|s| s.get(0).map(|st| st.build_status)).unwrap_or(false)) + } + + pub async fn build_status(&self, crate_name: &str, version: &str) -> Result<Option<Vec<BuildStatus>>, Error> { + let key = format!("{}-{}", crate_name, version); + if let Some(cached) = self.cache.get(&key)? { + return Ok(cached); + } + let url = format!("https://docs.rs/crate/{}/{}/builds.json", crate_name, version); + Ok(self.cache.get_json(&key, url, |t| t).await?.and_then(|f| f)) + } +} + +#[tokio::test] +async fn test_docsrsclient() { + let client = DocsRsClient::new("../data/docsrs.db").expect("new"); + + assert!(client.builds("libc", "0.2.40").await.expect("libc")); + client.build_status("libc", "0.2.40").await.expect("libc"); +} |