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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/*!
* Provides a builder experience for creating `IRI` instances. The [`IriBuilder`](builder/struct.IriBuilder.html)
* type provides a simple API to create new `IRI` instances in a fluent style.
*
* # Example
*
* ```rust
* use rdftk_iri::{builder::IriBuilder, IRI, error::Result as IriResult, Scheme};
* use std::convert::TryInto;
*
* fn make_example_iri() -> IriResult<IRI> {
*     let mut builder = IriBuilder::default();
*     builder
*         .scheme(&Scheme::https())
*         .user_name("john.doe")
*         .host_str("www.example.com")?
*         .port(123.into())
*         .path_str("/forum/questions/")?
*         .query_str("tag=networking&order=newest")?
*         .fragment_str("top")?
*         .try_into()
* }
* ```
*
*/

#![allow(clippy::module_name_repetitions)]

use crate::error::{Error as IriError, Result as IriResult};
use crate::{Authority, Fragment, Host, Path, Port, Query, Scheme, UserInfo, IRI};
use std::convert::TryFrom;
use std::str::FromStr;

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

///
/// The builder type, this provides simple API access to create new `IRI` instances in a
/// fluent style.
///
#[derive(Debug)]
pub struct IriBuilder {
    scheme: Option<Scheme>,
    host: Option<Host>,
    user_name: Option<String>,
    password: Option<String>,
    port: Option<Port>,
    path: Option<Path>,
    query: Option<Query>,
    fragment: Option<Fragment>,
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl Default for IriBuilder {
    fn default() -> Self {
        Self {
            scheme: None,
            host: None,
            user_name: None,
            password: None,
            port: None,
            path: None,
            query: None,
            fragment: None,
        }
    }
}

impl TryFrom<&mut IriBuilder> for IRI {
    type Error = IriError;

    fn try_from(builder: &mut IriBuilder) -> Result<Self, Self::Error> {
        let mut iri = match &builder.path {
            None => IRI::default(),
            Some(path) => IRI::new(path),
        };

        if let Some(scheme) = &builder.scheme {
            iri.set_scheme(Some(scheme.clone()));
        }

        if let Some(host) = &builder.host {
            let mut authority = Authority::new(host.clone());

            if let Some(port) = &builder.port {
                authority.set_port(port.clone());
            }

            if let Some(user_name) = &builder.user_name {
                let mut user_info = UserInfo::new(user_name)?;
                if let Some(password) = &builder.password {
                    user_info.set_password(password)?;
                }
                authority.set_user_info(user_info);
            }
            iri.set_authority(Some(authority));
        }

        if let Some(query) = &builder.query {
            iri.set_query(Some(query.clone()));
        }

        if let Some(fragment) = &builder.fragment {
            iri.set_fragment(Some(fragment.clone()));
        }

        Ok(iri)
    }
}

impl IriBuilder {
    /// Use the provided scheme for this IRI.
    pub fn scheme(&mut self, scheme: &Scheme) -> &mut Self {
        self.scheme = Some(scheme.clone());
        self
    }

    /// Use the provided scheme, parsed from a string, for this IRI.
    pub fn scheme_str(&mut self, scheme: &str) -> IriResult<&mut Self> {
        Ok(self.scheme(&Scheme::from_str(scheme)?))
    }

    /// Use the provided host name, parsed from a string, for this IRI's authority.
    pub fn host(&mut self, host: &Host) -> &mut Self {
        self.host = Some(host.clone());
        self
    }

    /// Use the provided host name, parsed from a string, for this IRI's authority.
    pub fn host_str(&mut self, host: &str) -> IriResult<&mut Self> {
        Ok(self.host(&Host::from_str(host)?))
    }

    /// Use the provided port number for this IRI's authority.
    pub fn port(&mut self, port: Port) -> &mut Self {
        self.port = Some(port);
        self
    }

    /// Use the provided user name and password for this IRI's authority.
    pub fn user(&mut self, user_name: &str, password: &str) -> &mut Self {
        self.user_name = Some(user_name.to_string());
        self.password = Some(password.to_string());
        self
    }

    /// Use the provided user name for this IRI's authority.
    pub fn user_name(&mut self, user_name: &str) -> &mut Self {
        self.user_name = Some(user_name.to_string());
        self
    }

    /// Use the provided password for this IRI's authority.
    pub fn password(&mut self, password: &str) -> &mut Self {
        self.password = Some(password.to_string());
        self
    }

    /// Use the provided path for this IRI.
    pub fn path_root(&mut self) -> &mut Self {
        self.path = Some(Path::root());
        self
    }

    /// Use the provided path for this IRI.
    pub fn path(&mut self, path: &Path) -> &mut Self {
        self.path = Some(path.clone());
        self
    }

    /// Use the provided path, parsed from a string, for this IRI.
    pub fn path_str(&mut self, path: &str) -> IriResult<&mut Self> {
        self.path = Some(Path::from_str(path)?);
        Ok(self)
    }

    /// Append a segment to the path for this IRI.
    pub fn append_path_segment(&mut self, segment: &str) -> IriResult<&mut Self> {
        match &mut self.path {
            None => self.path = Some(Path::from_str(segment)?),
            Some(path) => path.push(segment)?,
        }
        Ok(self)
    }

    /// Use the provided query for this IRI.
    pub fn query(&mut self, query: &Query) -> &mut Self {
        self.query = Some(query.clone());
        self
    }

    /// Use the provided query, parsed from a string, for this IRI.
    pub fn query_str(&mut self, query: &str) -> IriResult<&mut Self> {
        self.query = Some(Query::from_str(query)?);
        Ok(self)
    }

    /// Use the provided fragment for this IRI.
    pub fn fragment(&mut self, fragment: &Fragment) -> &mut Self {
        self.fragment = Some(fragment.clone());
        self
    }

    /// Use the provided fragment, parsed from a string, for this IRI.
    pub fn fragment_str(&mut self, fragment: &str) -> IriResult<&mut Self> {
        self.fragment = Some(Fragment::from_str(fragment)?);
        Ok(self)
    }
}