Remove normalized representation of SimpleJson

This commit is contained in:
Charlie Marsh 2023-10-04 19:50:17 -04:00
parent 610fd9994f
commit b08e8c78b5
5 changed files with 16 additions and 114 deletions

View file

@ -20,7 +20,7 @@ pub(crate) async fn install(src: &Path) -> Result<ExitStatus> {
let packument = client.simple(&requirement.name).await?; let packument = client.simple(&requirement.name).await?;
#[allow(clippy::print_stdout)] #[allow(clippy::print_stdout)]
{ {
println!("{:#?}", packument); println!("{packument:#?}");
println!("{requirement:#?}"); println!("{requirement:#?}");
} }
} }

View file

@ -58,7 +58,7 @@ pub struct SimpleJson {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct File { pub(crate) struct File {
core_metadata: Metadata, core_metadata: Metadata,
data_dist_info_metadata: Metadata, data_dist_info_metadata: Metadata,
filename: String, filename: String,
@ -72,122 +72,27 @@ pub struct File {
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum Metadata { pub(crate) enum Metadata {
Bool(bool), Bool(bool),
Hashes(Hashes), Hashes(Hashes),
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)] #[serde(untagged)]
pub enum Yanked { pub(crate) enum Yanked {
Bool(bool), Bool(bool),
Reason(String), Reason(String),
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
pub struct Hashes { pub(crate) struct Hashes {
sha256: String, sha256: String,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct Meta { pub(crate) struct Meta {
#[serde(rename = "_last-serial")] #[serde(rename = "_last-serial")]
last_serial: i64, last_serial: i64,
api_version: String, api_version: String,
} }
/// The metadata for a single package, including the pubishing versions and their artifacts.
///
/// In npm, this is referred to as a "packument", which is a portmanteau of "package" and
/// "document".
#[derive(Debug, Serialize, Deserialize)]
pub struct PackageDocument {
pub meta: Meta,
pub artifacts: Vec<ArtifactInfo>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Meta {
pub version: String,
}
impl Default for Meta {
fn default() -> Self {
Self {
// According to the spec, clients SHOULD introspect each response for the repository
// version; if it doesn't exist, clients MUST assume that it is version 1.0.
version: "1.0".into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ArtifactInfo {
pub name: String,
pub url: Url,
pub hash: Option<String>,
pub requires_python: Option<String>,
pub dist_info_metadata: DistInfoMetadata,
pub yanked: Yanked,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "Option<RawDistInfoMetadata>")]
pub struct DistInfoMetadata {
pub available: bool,
pub hash: Option<String>,
}
impl From<Option<RawDistInfoMetadata>> for DistInfoMetadata {
fn from(maybe_raw: Option<RawDistInfoMetadata>) -> Self {
match maybe_raw {
None => Default::default(),
Some(raw) => match raw {
RawDistInfoMetadata::NoHashes(available) => Self {
available,
hash: None,
},
RawDistInfoMetadata::WithHashes(_) => Self {
available: true,
hash: None,
},
},
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
enum RawDistInfoMetadata {
NoHashes(bool),
WithHashes(HashMap<String, String>),
}
#[derive(Debug, Clone, Deserialize)]
enum RawYanked {
NoReason(bool),
WithReason(String),
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
#[serde(from = "RawYanked")]
pub struct Yanked {
pub yanked: bool,
pub reason: Option<String>,
}
impl From<RawYanked> for Yanked {
fn from(raw: RawYanked) -> Self {
match raw {
RawYanked::NoReason(yanked) => Self {
yanked,
reason: None,
},
RawYanked::WithReason(reason) => Self {
yanked: true,
reason: Some(reason),
},
}
}
}

View file

@ -33,9 +33,6 @@ pub enum PypiClientError {
impl PypiClientError { impl PypiClientError {
pub fn from_json_err(err: serde_json::Error, url: String) -> Self { pub fn from_json_err(err: serde_json::Error, url: String) -> Self {
Self::BadJson { Self::BadJson { source: err, url }
source: err,
url: url.clone(),
}
} }
} }

View file

@ -11,10 +11,6 @@ use url::Url;
mod api; mod api;
mod error; mod error;
fn main() {
println!("Hello, world!");
}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct PypiClientBuilder { pub struct PypiClientBuilder {
registry: Url, registry: Url,
@ -33,16 +29,19 @@ impl Default for PypiClientBuilder {
} }
impl PypiClientBuilder { impl PypiClientBuilder {
#[must_use]
pub fn registry(mut self, registry: Url) -> Self { pub fn registry(mut self, registry: Url) -> Self {
self.registry = registry; self.registry = registry;
self self
} }
#[must_use]
pub fn retries(mut self, retries: u32) -> Self { pub fn retries(mut self, retries: u32) -> Self {
self.retries = retries; self.retries = retries;
self self
} }
#[must_use]
pub fn cache(mut self, cache: impl AsRef<Path>) -> Self { pub fn cache(mut self, cache: impl AsRef<Path>) -> Self {
self.cache = Some(PathBuf::from(cache.as_ref())); self.cache = Some(PathBuf::from(cache.as_ref()));
self self
@ -50,7 +49,7 @@ impl PypiClientBuilder {
pub fn build(self) -> PypiClient { pub fn build(self) -> PypiClient {
let client_raw = { let client_raw = {
let mut client_core = ClientBuilder::new() let client_core = ClientBuilder::new()
.user_agent("puffin") .user_agent("puffin")
.pool_max_idle_per_host(20) .pool_max_idle_per_host(20)
.timeout(std::time::Duration::from_secs(60 * 5)); .timeout(std::time::Duration::from_secs(60 * 5));
@ -62,7 +61,7 @@ impl PypiClientBuilder {
let retry_strategy = RetryTransientMiddleware::new_with_policy(retry_policy); let retry_strategy = RetryTransientMiddleware::new_with_policy(retry_policy);
let mut client_builder = let mut client_builder =
reqwest_middleware::ClientBuilder::new(client_raw.clone()).with(retry_strategy); reqwest_middleware::ClientBuilder::new(client_raw).with(retry_strategy);
if let Some(path) = self.cache { if let Some(path) = self.cache {
client_builder = client_builder.with(Cache(HttpCache { client_builder = client_builder.with(Cache(HttpCache {
@ -84,5 +83,3 @@ pub struct PypiClient {
pub(crate) registry: Arc<Url>, pub(crate) registry: Arc<Url>,
pub(crate) client: ClientWithMiddleware, pub(crate) client: ClientWithMiddleware,
} }
impl PypiClient {}

View file

@ -1 +1,4 @@
flask==2.0 flask
black
jinja2
pyyaml