Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
Empty file added python/py.typed
Empty file.
61 changes: 61 additions & 0 deletions python/pyfaup.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
class FaupCompat:

url: bytes

def __init__(self, url: str | None=None) -> None:
...

def decode(self, url: str) -> None:
...

def get_credential(self) -> str | None:
...

def get_domain(self) -> str | None:
...

def get_subdomain(self) -> str | None:
...

def get_fragment(self) -> str | None:
...

def get_host(self) -> str | None:
...

def get_resource_path(self) -> str | None:
...

def get_tld(self) -> str | None:
...

def get_query_string(self) -> str | None:
...

def get_scheme(self) -> str | None:
...

def get_domain_without_tld(self) -> str | None:
...

def get_port(self) -> int | None:
...


class Url:

orig: str
scheme: str
username: str | None
password: str | None
host: str
subdomain: str | None
domain: str | None
suffix: str | None
port: int | None
path: str | None
query: str | None
fragment: str | None

def __init__(self, url: str | None = None) -> None:
...
57 changes: 57 additions & 0 deletions python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ impl From<Error> for PyErr {
/// >>> print(url.port) # 8080
#[pyclass]
pub struct Url {
#[pyo3(get)]
pub orig: String,
#[pyo3(get)]
pub scheme: String,
#[pyo3(get)]
Expand Down Expand Up @@ -91,6 +93,7 @@ impl From<faup_rs::Url<'_>> for Url {
};

Self {
orig: value.to_string(),
scheme: value.scheme().into(),
username,
password,
Expand Down Expand Up @@ -140,6 +143,7 @@ impl Url {
.map(|u| u.into())
.map_err(|e| PyValueError::new_err(e.to_string()))
}

}

/// A compatibility class that mimics the FAUP (Fast URL Parser) Python API.
Expand Down Expand Up @@ -233,6 +237,59 @@ impl FaupCompat {

Ok(m)
}

fn get_credential(&self) -> Option<String> {
let url = self.url.as_ref();
url.and_then(|u| u.credentials())
}

fn get_domain(&self) -> Option<&str> {
self.url.as_ref()?.domain.as_deref()
}

fn get_subdomain(&self) -> Option<&str> {
self.url.as_ref()?.subdomain.as_deref()
}

fn get_fragment(&self) -> Option<&str> {
self.url.as_ref()?.fragment.as_deref()
}

fn get_host(&self) -> Option<&str> {
self.url.as_ref().map(|u| u.host.as_str())
}

fn get_resource_path(&self) -> Option<&str> {
self.url.as_ref()?.path.as_deref()
}

fn get_tld(&self) -> Option<&str> {
self.url.as_ref()?.suffix.as_deref()
}

fn get_query_string(&self) -> Option<&str> {
self.url.as_ref()?.query.as_deref()
}

fn get_scheme(&self) -> Option<&str> {
self.url.as_ref().map(|u| u.scheme.as_str())
}

fn get_port(&self) -> Option<u16> {
self.url.as_ref()?.port
}

fn get_domain_without_tld(&self) -> Option<&str> {
let domain = self.url.as_ref()?.domain.as_deref();
let tld = self.url.as_ref()?.suffix.as_deref();
if domain.is_some() && tld.is_some() {
Some(&domain?[0..domain?.chars().count() - tld?.chars().count() - 1])
}
else {
None
}
}

}

/// A Python module implemented in Rust for URL parsing.
Expand Down
Loading