Skip to content

Commit

Permalink
Make a proper slice version for Tld
Browse files Browse the repository at this point in the history
  • Loading branch information
kim committed Jul 10, 2023
1 parent 74bfbab commit 0c14036
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 18 deletions.
3 changes: 2 additions & 1 deletion crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use spacetimedb::host::UpdateDatabaseSuccess;
use spacetimedb_lib::name;
use spacetimedb_lib::name::DomainName;
use spacetimedb_lib::name::DomainParsingError;
use spacetimedb_lib::name::Tld;
use spacetimedb_lib::sats::TypeInSpace;

use crate::auth::{
Expand Down Expand Up @@ -619,7 +620,7 @@ pub async fn register_tld<S: ControlStateDelegate>(
// so, unless you are the owner, this will fail, hence not using get_or_create
let auth = auth.get().ok_or((StatusCode::BAD_REQUEST, "Invalid credentials."))?;

let tld = tld.parse::<DomainName>().map_err(DomainParsingRejection)?.into_tld();
let tld: Tld = tld.parse::<DomainName>().map_err(DomainParsingRejection)?.into();
let result = ctx.register_tld(&auth.identity, tld).await.map_err(log_and_500)?;
Ok(axum::Json(result))
}
Expand Down
77 changes: 60 additions & 17 deletions crates/lib/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use core::fmt;
use std::str::FromStr;
use std::{borrow::Borrow, ops::Deref, str::FromStr};

use spacetimedb_sats::{
de::{self, Deserialize, Deserializer},
Expand Down Expand Up @@ -168,13 +168,11 @@ impl fmt::Display for Tld {
}
}

impl From<Tld> for DomainName {
fn from(tld: Tld) -> Self {
let domain_name = tld.0;
Self {
tld_offset: domain_name.len(),
domain_name,
}
impl From<DomainName> for Tld {
fn from(value: DomainName) -> Self {
let mut name = value.domain_name;
name.truncate(value.tld_offset);
Self(name)
}
}

Expand Down Expand Up @@ -210,6 +208,47 @@ impl<'de> serde::Deserialize<'de> for Tld {
}
}

/// A slice of a [`Tld`], akin to [`str`].
#[derive(Debug, PartialEq, Eq)]
pub struct TldRef(str);

impl TldRef {
// Private to enforce parsing
fn new(s: &str) -> &Self {
// SAFETY: `TldRef` is just a wrapper around `str`, therefore converting
// `&str` to `&TldRef` is safe.
unsafe { &*(s as *const str as *const TldRef) }
}
}

impl Deref for TldRef {
type Target = str;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl Borrow<TldRef> for Tld {
fn borrow(&self) -> &TldRef {
TldRef::new(&self.0)
}
}

impl ToOwned for TldRef {
type Owned = Tld;

fn to_owned(&self) -> Self::Owned {
Tld(self.0.to_owned())
}
}

impl fmt::Display for TldRef {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.0)
}
}

/// A [`DomainName`] is the name of a database.
///
/// A database name is usually in one of the two following forms:
Expand Down Expand Up @@ -253,18 +292,12 @@ impl DomainName {
&self.domain_name
}

pub fn tld(&self) -> &str {
&self.domain_name[..self.tld_offset]
}

/// Drop subdomain, if any, and return only the TLD
pub fn into_tld(mut self) -> Tld {
self.domain_name.truncate(self.tld_offset);
Tld(self.domain_name)
pub fn tld(&self) -> &TldRef {
TldRef::new(&self.domain_name[..self.tld_offset])
}

pub fn as_tld(&self) -> Tld {
Tld(self.tld().to_owned())
self.tld().to_owned()
}

pub fn sub_domain(&self) -> Option<&str> {
Expand Down Expand Up @@ -302,6 +335,16 @@ impl FromStr for DomainName {
}
}

impl From<Tld> for DomainName {
fn from(tld: Tld) -> Self {
let domain_name = tld.0;
Self {
tld_offset: domain_name.len(),
domain_name,
}
}
}

impl SpacetimeType for DomainName {
fn make_type<S: TypespaceBuilder>(_typespace: &mut S) -> AlgebraicType {
AlgebraicType::String
Expand Down

0 comments on commit 0c14036

Please sign in to comment.