Skip to content

Implement length rule for urls #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions garde/src/rules/url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,36 @@ impl<T: Url> Url for Option<T> {
}
}
}

impl super::length::HasSimpleLength for url::Url {
fn length(&self) -> usize {
self.as_str().len()
}
}

impl super::length::HasChars for url::Url {
fn num_chars(&self) -> usize {
self.as_str().chars().count()
}
}

#[cfg(feature = "unicode")]
impl super::length::HasGraphemes for url::Url {
fn num_graphemes(&self) -> usize {
use unicode_segmentation::UnicodeSegmentation;

self.as_str().graphemes(true).count()
}
}

impl super::length::HasBytes for url::Url {
fn num_bytes(&self) -> usize {
self.as_str().len()
}
}

impl super::length::HasUtf16CodeUnits for url::Url {
fn num_code_units(&self) -> usize {
self.as_str().encode_utf16().count()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
source: garde/tests/./rules/url.rs
expression: snapshot
---
Struct {
field: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
v: Struct {
default: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
simple: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
bytes: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
chars: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
graphemes: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
utf_16_code_units: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
},
}
field: length is lower than 30
v.bytes: length is lower than 30
v.chars: length is lower than 30
v.default: length is lower than 30
v.graphemes: length is lower than 30
v.simple: length is lower than 30
v.utf_16_code_units: length is lower than 30

Tuple(
Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
)
[0]: length is lower than 30
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
---
source: garde/tests/./rules/url.rs
expression: snapshot
---
Struct {
default: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
simple: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
bytes: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
chars: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
graphemes: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
utf_16_code_units: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/",
query: None,
fragment: None,
},
}
bytes: length is lower than 30
chars: length is lower than 30
default: length is lower than 30
graphemes: length is lower than 30
simple: length is lower than 30
utf_16_code_units: length is lower than 30
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
---
source: garde/tests/./rules/url.rs
expression: snapshot
---
Struct {
default: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/watch",
query: Some(
"v=dQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQ",
),
fragment: None,
},
simple: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/watch",
query: Some(
"v=dQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQ",
),
fragment: None,
},
bytes: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/watch",
query: Some(
"v=dQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQ",
),
fragment: None,
},
chars: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/watch",
query: Some(
"v=dQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQ",
),
fragment: None,
},
graphemes: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/watch",
query: Some(
"v=dQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQ",
),
fragment: None,
},
utf_16_code_units: Url {
scheme: "https",
cannot_be_a_base: false,
username: "",
password: None,
host: Some(
Domain(
"www.youtube.com",
),
),
port: None,
path: "/watch",
query: Some(
"v=dQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQdQw4w9WgXcQ",
),
fragment: None,
},
}
bytes: length is greater than 100
chars: length is greater than 100
default: length is greater than 100
graphemes: length is greater than 100
simple: length is greater than 100
utf_16_code_units: length is greater than 100
Loading