Skip to main content

UrlBuilder

Struct UrlBuilder 

Source
pub struct UrlBuilder { /* private fields */ }
Expand description

Fluent URL builder.

Build a URL incrementally by chaining setter methods, then call build to produce the final String.

Path segments are automatically percent-encoded. Query parameters are form-encoded. No validation of scheme or host is performed — this is a string-composition helper, not a full URL parser.

Implementations§

Source§

impl UrlBuilder

Source

pub fn new() -> Self

Create an empty UrlBuilder.

Source

pub fn scheme(self, scheme: impl Into<String>) -> Self

Set the URL scheme (e.g. "https").

§Examples
use api_bones::url::UrlBuilder;

let url = UrlBuilder::new().scheme("https").host("example.com").build();
assert_eq!(url, "https://example.com");
Source

pub fn host(self, host: impl Into<String>) -> Self

Set the host (e.g. "api.example.com").

Source

pub fn port(self, port: u16) -> Self

Set an optional port number.

§Examples
use api_bones::url::UrlBuilder;

let url = UrlBuilder::new()
    .scheme("http")
    .host("localhost")
    .port(8080)
    .build();
assert_eq!(url, "http://localhost:8080");
Source

pub fn path(self, segment: impl Into<String>) -> Self

Append a path segment (will be percent-encoded).

Call multiple times to build up /a/b/c style paths.

§Examples
use api_bones::url::UrlBuilder;

let url = UrlBuilder::new()
    .scheme("https")
    .host("example.com")
    .path("v1")
    .path("users")
    .path("hello world")
    .build();
assert_eq!(url, "https://example.com/v1/users/hello%20world");
Source

pub fn query(self, key: impl Into<String>, value: impl ToString) -> Self

Append a query parameter (key and value are form-encoded).

§Examples
use api_bones::url::UrlBuilder;

let url = UrlBuilder::new()
    .scheme("https")
    .host("example.com")
    .query("q", "hello world")
    .build();
assert_eq!(url, "https://example.com?q=hello+world");
Source

pub fn fragment(self, fragment: impl Into<String>) -> Self

Set the URL fragment (the part after #).

§Examples
use api_bones::url::UrlBuilder;

let url = UrlBuilder::new()
    .scheme("https")
    .host("example.com")
    .fragment("section-1")
    .build();
assert_eq!(url, "https://example.com#section-1");
Source

pub fn build(&self) -> String

Produce the final URL string.

§Examples
use api_bones::url::UrlBuilder;

let url = UrlBuilder::new()
    .scheme("https")
    .host("api.example.com")
    .path("v1")
    .path("items")
    .query("page", 2u32)
    .fragment("top")
    .build();

assert_eq!(url, "https://api.example.com/v1/items?page=2#top");

Trait Implementations§

Source§

impl Clone for UrlBuilder

Source§

fn clone(&self) -> UrlBuilder

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for UrlBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for UrlBuilder

Source§

fn default() -> UrlBuilder

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for UrlBuilder

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for UrlBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for UrlBuilder

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ValidateIp for T
where T: ToString,

Source§

fn validate_ipv4(&self) -> bool

Validates whether the given string is an IP V4
Source§

fn validate_ipv6(&self) -> bool

Validates whether the given string is an IP V6
Source§

fn validate_ip(&self) -> bool

Validates whether the given string is an IP
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,