snapr/
builder.rs

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use crate::{Error, Snapr, TileFetcher};

/// Builder structure for [`snapr`].
///
/// ## Example
///
/// ```rust
/// use image::DynamicImage;
/// use snapr::SnaprBuilder;
///
/// fn tile_fetcher(x: i32, y: i32, zoom: u8) -> Result<DynamicImage, snapr::Error> {
///     todo!()
/// }
///
/// let snapr = SnaprBuilder::new()
///     .with_tile_fetcher(tile_fetcher)
///     .build();
///
/// assert!(snapr.is_ok());
/// ```
#[derive(Debug, Default)]
pub struct SnaprBuilder {
    tile_fetcher: Option<TileFetcher>,
    tile_size: Option<u32>,
    height: Option<u32>,
    width: Option<u32>,
    zoom: Option<u8>,
}

impl SnaprBuilder {
    /// Constructs a new [`SnaprBuilder`] to be used in constructing a [`Snapr`].
    pub fn new() -> Self {
        Self::default()
    }

    /// Configures a [`TileFetcher`] to be used in the [`Snapr::tile_fetcher`] field.
    pub fn with_tile_fetcher(self, tile_fetcher: TileFetcher) -> Self {
        Self {
            tile_fetcher: Some(tile_fetcher),
            ..self
        }
    }

    /// Configures the `tile_size` to be used in the [`Snapr::tile_size`] field.
    pub fn with_tile_size(self, tile_size: u32) -> Self {
        Self {
            tile_size: Some(tile_size),
            ..self
        }
    }

    /// Configures the `height` to be used in the [`Snapr::height`] field.
    pub fn with_height(self, height: u32) -> Self {
        Self {
            height: Some(height),
            ..self
        }
    }

    /// Configures the `width` to be used in the [`Snapr::width`] field.
    pub fn with_width(self, width: u32) -> Self {
        Self {
            width: Some(width),
            ..self
        }
    }

    /// Configures the `zoom` to be used in the [`Snapr::zoom`] field.
    pub fn with_zoom(self, zoom: u8) -> Self {
        Self {
            zoom: Some(zoom),
            ..self
        }
    }

    /// Attempts to construct a new [`Snapr`] from the [`SnaprBuilder`].
    ///
    /// ## Example
    ///
    /// ```rust
    /// use image::DynamicImage;
    /// use snapr::SnaprBuilder;
    ///
    /// fn tile_fetcher(x: i32, y: i32, zoom: u8) -> Result<DynamicImage, snapr::Error> {
    ///     todo!()
    /// }
    ///
    /// let snapr = SnaprBuilder::new()
    ///     .with_tile_fetcher(tile_fetcher)
    ///     .build();
    ///
    /// assert!(snapr.is_ok());
    /// ```
    pub fn build(self) -> Result<Snapr, Error> {
        let Some(tile_fetcher) = self.tile_fetcher else {
            return Err(Error::Builder {
                reason: "field `tile_fetcher` needs to be set prior to a `snapr` being built"
                    .to_string(),
            });
        };

        let tile_size = self.tile_size.unwrap_or(256);
        let height = self.height.unwrap_or(600);
        let width = self.width.unwrap_or(800);
        let zoom = self.zoom;

        let snapr = Snapr {
            tile_fetcher,
            tile_size,
            height,
            width,
            zoom,
        };

        Ok(snapr)
    }
}