pinboard_rs/api/v1/notes/
note.rs

1// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
2// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
3// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
4// option. This file may not be copied, modified, or distributed
5// except according to those terms.
6
7use crate::api::endpoint_prelude::*;
8use crate::api::v1::Limit;
9use derive_builder::Builder;
10
11/// Create a Note endpoint for retrieving a single note.
12///
13/// <https://pinboard.in/api/#notes_get>
14///
15/// # Arguments
16/// * `id` - id of the note to retrieve
17///
18/// Example
19/// ```rust
20/// # fn main() {
21/// # use crate::pinboard_rs::api::v1::notes::Note;
22/// # use crate::pinboard_rs::api::Endpoint;
23/// let note_endpoint = Note::builder().id("abc123").build().unwrap();
24/// assert_eq!(note_endpoint.endpoint(), "v1/notes/abc123/");
25/// # }
26/// ```
27#[derive(Debug, Clone, Builder)]
28pub struct Note<'a> {
29    /// The note id
30    #[builder(setter(into))]
31    id: Cow<'a, str>,
32}
33
34impl<'a> Note<'a> {
35    /// Create a builder for the endpoint
36    pub fn builder() -> NoteBuilder<'a> {
37        NoteBuilder::default()
38    }
39}
40
41impl<'a> Endpoint for Note<'a> {
42    fn method(&self) -> Method {
43        Method::GET
44    }
45
46    fn endpoint(&self) -> Cow<'static, str> {
47        format!("v1/notes/{}/", self.id).into()
48    }
49}
50
51impl<'a> Limit for Note<'a> {}
52
53#[cfg(test)]
54mod tests {
55    use crate::api::v1::notes::Note;
56    use crate::api::v1::Limit;
57    use crate::api::{self, Query};
58    use crate::test::client::{ExpectedUrl, SingleTestClient};
59
60    #[test]
61    fn id_is_required() {
62        let err = Note::builder().build().unwrap_err();
63        assert_eq!(&err.to_string(), "`id` must be initialized")
64    }
65
66    #[test]
67    fn endpoint() {
68        let endpoint = ExpectedUrl::builder()
69            .endpoint("v1/notes/IDHERE/")
70            .build()
71            .unwrap();
72        let client = SingleTestClient::new_raw(endpoint, "");
73
74        let endpoint = Note::builder().id("IDHERE").build().unwrap();
75        api::ignore(endpoint).query(&client).unwrap();
76    }
77
78    #[test]
79    fn limit() {
80        assert_eq!(Note::secs_between_calls(), 3)
81    }
82}