1use crate::{AppendToUrlQuery, Url};
2use std::num::NonZeroU32;
34// This type forbids zero as value.
5// Zero is invalid in Azure and would throw an error if specified.
6// Azure has a soft cap on 5k. There is no harm to go over but maybe
7// we should inform the user that they are specifing a value outside
8// the allowed range. Right now we simply ignore it.
9#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
10pub struct MaxResults(NonZeroU32);
1112impl MaxResults {
13pub fn new(max_results: NonZeroU32) -> Self {
14Self(max_results)
15 }
16}
1718impl AppendToUrlQuery for MaxResults {
19fn append_to_url_query(&self, url: &mut Url) {
20 url.query_pairs_mut()
21 .append_pair("maxresults", &format!("{}", self.0));
22 }
23}
2425impl From<NonZeroU32> for MaxResults {
26fn from(max_results: NonZeroU32) -> Self {
27Self::new(max_results)
28 }
29}
3031impl TryFrom<u32> for MaxResults {
32type Error = String;
3334fn try_from(max_results: u32) -> Result<Self, Self::Error> {
35match NonZeroU32::new(max_results) {
36Some(max_results) => Ok(max_results.into()),
37None => Err(format!(
38"number {max_results} is not a valid NonZeroU32 value"
39)),
40 }
41 }
42}