1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use crate::{AppendToUrlQuery, Url};
use std::num::NonZeroU32;

// This type forbids zero as value.
// Zero is invalid in Azure and would throw an error if specified.
// Azure has a soft cap on 5k. There is no harm to go over but maybe
// we should inform the user that they are specifing a value outside
// the allowed range. Right now we simply ignore it.
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd, Eq, Ord)]
pub struct MaxResults(NonZeroU32);

impl MaxResults {
    pub fn new(max_results: NonZeroU32) -> Self {
        Self(max_results)
    }
}

impl AppendToUrlQuery for MaxResults {
    fn append_to_url_query(&self, url: &mut Url) {
        url.query_pairs_mut()
            .append_pair("maxresults", &format!("{}", self.0));
    }
}

impl From<NonZeroU32> for MaxResults {
    fn from(max_results: NonZeroU32) -> Self {
        Self::new(max_results)
    }
}

impl TryFrom<u32> for MaxResults {
    type Error = String;

    fn try_from(max_results: u32) -> Result<Self, Self::Error> {
        match NonZeroU32::new(max_results) {
            Some(max_results) => Ok(max_results.into()),
            None => Err(format!(
                "number {max_results} is not a valid NonZeroU32 value"
            )),
        }
    }
}