Skip to content

Commit

Permalink
Move lib::{name,recovery} to their own crate
Browse files Browse the repository at this point in the history
  • Loading branch information
coolreader18 committed Nov 16, 2023
1 parent 70332d7 commit d503311
Show file tree
Hide file tree
Showing 25 changed files with 83 additions and 91 deletions.
22 changes: 19 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ members = [
"crates/sdk/tests/test-client",
"crates/sdk/tests/connect_disconnect_client",
"tools/upgrade-version",
"crates/api-common",
]
default-members = ["crates/cli"]
# cargo feature graph resolver. v2 is default in edition2021 but workspace
Expand Down
18 changes: 18 additions & 0 deletions crates/api-common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[package]
name = "spacetimedb-api-common"
version = "0.7.3"
edition = "2021"

[dependencies]
spacetimedb-lib = { path = "../lib", version = "0.7.1" }
spacetimedb-sats = { path = "../sats", version = "0.7.1" }

chrono.workspace = true
hex.workspace = true
serde.workspace = true
thiserror.workspace = true

[dev-dependencies]
itertools.workspace = true
proptest.workspace = true
serde_json.workspace = true
4 changes: 4 additions & 0 deletions crates/api-common/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
//! Common types used for both implementing and consuming the SpacetimeDB HTTP API.
pub mod name;
pub mod recovery;
33 changes: 10 additions & 23 deletions crates/lib/src/name.rs → crates/api-common/src/name.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use spacetimedb_sats::{impl_deserialize, impl_serialize, impl_st};
use std::{borrow::Borrow, fmt, ops::Deref, str::FromStr};

use crate::Address;
use spacetimedb_lib::Address;

#[cfg(test)]
mod tests;

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum InsertDomainResult {
Success {
domain: DomainName,
Expand Down Expand Up @@ -39,19 +38,14 @@ pub enum InsertDomainResult {
OtherError(String),
}

#[derive(Clone, Copy, Debug)]
#[cfg_attr(
feature = "serde",
derive(serde::Serialize, serde::Deserialize),
serde(rename_all = "lowercase")
)]
#[derive(Clone, Copy, Debug, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PublishOp {
Created,
Updated,
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum PublishResult {
Success {
/// `Some` if publish was given a domain name to operate on, `None`
Expand Down Expand Up @@ -88,8 +82,7 @@ pub enum PublishResult {
PermissionDenied { domain: DomainName },
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum DnsLookupResponse {
/// The lookup was successful and the domain and address are returned.
Success { domain: DomainName, address: Address },
Expand All @@ -98,8 +91,7 @@ pub enum DnsLookupResponse {
Failure { domain: DomainName },
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum RegisterTldResult {
Success {
domain: Tld,
Expand All @@ -115,8 +107,7 @@ pub enum RegisterTldResult {
// TODO(jdetter): Insufficient funds error here
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub enum SetDefaultDomainResult {
Success {
domain: DomainName,
Expand All @@ -139,8 +130,7 @@ pub enum SetDefaultDomainResult {
///
/// Note that the SpacetimeDB DNS registry may apply additional restrictions on
/// what TLDs can be registered.
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize)]
pub struct Tld(String);

impl Tld {
Expand Down Expand Up @@ -187,7 +177,6 @@ impl_deserialize!([] Tld, de => {
Ok(Self(s))
});

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Tld {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down Expand Up @@ -363,7 +352,6 @@ impl_deserialize!([] DomainName, de => {
parse_domain_name(s).map_err(spacetimedb_sats::de::Error::custom)
});

#[cfg(feature = "serde")]
mod serde_impls {
use super::*;

Expand Down Expand Up @@ -435,8 +423,7 @@ mod serde_impls {
}
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
pub struct ReverseDNSResponse {
pub names: Vec<DomainName>,
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use chrono::serde::ts_seconds;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

use crate::Identity;
use spacetimedb_lib::Identity;

#[derive(Deserialize, Serialize, Clone)]
pub struct RecoveryCode {
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ bench = false

[dependencies]
spacetimedb-core = { path = "../core", version = "0.7.3" }
spacetimedb-lib = { path = "../lib", version = "0.7.3", features = ["cli"] }
spacetimedb-lib = { path = "../lib", version = "0.7.3" }
spacetimedb-api-common = { path = "../api-common", version = "0.7.3" }
spacetimedb-standalone = { path = "../standalone", version = "0.7.3", optional = true }

anyhow.workspace = true
Expand Down
5 changes: 3 additions & 2 deletions crates/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use clap::Command;
use mimalloc::MiMalloc;
use spacetimedb_cli::*;
use spacetimedb_lib::util;

#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
Expand All @@ -12,7 +11,8 @@ async fn main() -> Result<(), anyhow::Error> {
// Save a default version to disk
config.save();

let (cmd, subcommand_args) = util::match_subcommand_or_exit(get_command());
let matches = get_command().get_matches();
let (cmd, subcommand_args) = matches.subcommand().unwrap();
exec_subcommand(config, &cmd, &subcommand_args).await?;

Ok(())
Expand All @@ -21,6 +21,7 @@ async fn main() -> Result<(), anyhow::Error> {
fn get_command() -> Command {
Command::new("spacetime")
.args_conflicts_with_subcommands(true)
.arg_required_else_help(true)
.subcommand_required(true)
.subcommands(get_subcommands())
.help_expected(true)
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/subcommands/dns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use clap::ArgMatches;
use clap::{Arg, Command};
use reqwest::Url;

use spacetimedb_lib::name::{DnsLookupResponse, InsertDomainResult, RegisterTldResult};
use spacetimedb_api_common::name::{DnsLookupResponse, InsertDomainResult, RegisterTldResult};

pub fn cli() -> Command {
Command::new("dns")
Expand Down
3 changes: 2 additions & 1 deletion crates/cli/src/subcommands/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use email_address::EmailAddress;
use reqwest::{StatusCode, Url};
use serde::Deserialize;
use spacetimedb::auth::identity::decode_token;
use spacetimedb_lib::{recovery::RecoveryCodeResponse, Identity};
use spacetimedb_api_common::recovery::RecoveryCodeResponse;
use spacetimedb_lib::Identity;
use tabled::{
settings::{object::Columns, Alignment, Modify, Style},
Table, Tabled,
Expand Down
4 changes: 2 additions & 2 deletions crates/cli/src/subcommands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use clap::Arg;
use clap::ArgAction::SetTrue;
use clap::ArgMatches;
use reqwest::{StatusCode, Url};
use spacetimedb_lib::name::PublishOp;
use spacetimedb_lib::name::{is_address, parse_domain_name, PublishResult};
use spacetimedb_api_common::name::PublishOp;
use spacetimedb_api_common::name::{is_address, parse_domain_name, PublishResult};
use std::fs;
use std::path::PathBuf;

Expand Down
2 changes: 1 addition & 1 deletion crates/cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::Context;
use base64::{engine::general_purpose::STANDARD as BASE_64_STD, Engine as _};
use reqwest::RequestBuilder;
use serde::Deserialize;
use spacetimedb_lib::name::{DnsLookupResponse, RegisterTldResult, ReverseDNSResponse};
use spacetimedb_api_common::name::{DnsLookupResponse, RegisterTldResult, ReverseDNSResponse};
use spacetimedb_lib::{Address, AlgebraicType, Identity};
use std::collections::HashMap;
use std::io::Write;
Expand Down
1 change: 1 addition & 0 deletions crates/client-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ spacetimedb-core = { path = "../core", version = "0.7.3" }
tokio = { version = "1.2", features = ["full"] }
lazy_static = "1.4.0"
spacetimedb-lib = { path = "../lib", version = "0.7.3" }
spacetimedb-api-common = { path = "../api-common", version = "0.7.3" }
log = "0.4.4"
serde = "1.0.136"
serde_json = { version = "1.0", features = ["raw_value"] }
Expand Down
4 changes: 2 additions & 2 deletions crates/client-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use spacetimedb::identity::Identity;
use spacetimedb::messages::control_db::{Database, DatabaseInstance, IdentityEmail, Node};
use spacetimedb::module_host_context::ModuleHostContext;
use spacetimedb::sendgrid_controller::SendGridController;
use spacetimedb_lib::name::{DomainName, InsertDomainResult, RegisterTldResult, Tld};
use spacetimedb_lib::recovery::RecoveryCode;
use spacetimedb_api_common::name::{DomainName, InsertDomainResult, RegisterTldResult, Tld};
use spacetimedb_api_common::recovery::RecoveryCode;

pub mod auth;
pub mod routes;
Expand Down
4 changes: 2 additions & 2 deletions crates/client-api/src/routes/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ use spacetimedb::identity::Identity;
use spacetimedb::json::client_api::StmtResultJson;
use spacetimedb::messages::control_db::{Database, DatabaseInstance, HostType};
use spacetimedb::sql::execute::execute;
use spacetimedb_api_common::name::{self, DnsLookupResponse, DomainName, DomainParsingError, PublishOp, PublishResult};
use spacetimedb_api_common::recovery::{RecoveryCode, RecoveryCodeResponse};
use spacetimedb_lib::address::AddressForUrl;
use spacetimedb_lib::identity::AuthCtx;
use spacetimedb_lib::name::{self, DnsLookupResponse, DomainName, DomainParsingError, PublishOp, PublishResult};
use spacetimedb_lib::recovery::{RecoveryCode, RecoveryCodeResponse};
use spacetimedb_lib::sats::WithTypespace;
use std::collections::HashMap;
use std::convert::From;
Expand Down
2 changes: 1 addition & 1 deletion crates/client-api/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use bytestring::ByteString;
use http::{HeaderName, HeaderValue, Request, StatusCode};

use spacetimedb::address::Address;
use spacetimedb_api_common::name::DomainName;
use spacetimedb_lib::address::AddressForUrl;
use spacetimedb_lib::name::DomainName;

use crate::routes::database::DomainParsingRejection;
use crate::{log_and_500, ControlStateReadAccess};
Expand Down
1 change: 1 addition & 0 deletions crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ harness = false

[dependencies]
spacetimedb-lib = { path = "../lib", version = "0.7.3" }
spacetimedb-api-common = { path = "../api-common", version = "0.7.3" }
spacetimedb-sats = { path = "../sats", version = "0.7.3" }
spacetimedb-vm = { path = "../vm", version = "0.7.3" }
spacetimedb-client-api-messages = { path = "../client-api-messages", version = "0.7.3" }
Expand Down
6 changes: 4 additions & 2 deletions crates/core/src/control_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ use crate::identity::Identity;
use crate::messages::control_db::{Database, DatabaseInstance, EnergyBalance, IdentityEmail, Node};
use crate::stdb_path;

use spacetimedb_lib::name::{DomainName, DomainParsingError, InsertDomainResult, RegisterTldResult, Tld, TldRef};
use spacetimedb_lib::recovery::RecoveryCode;
use spacetimedb_api_common::name::{
DomainName, DomainParsingError, InsertDomainResult, RegisterTldResult, Tld, TldRef,
};
use spacetimedb_api_common::recovery::RecoveryCode;
use spacetimedb_sats::bsatn;

#[cfg(test)]
Expand Down
6 changes: 1 addition & 5 deletions crates/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ required-features = ["serde"]

[features]
default = ["serde"]
serde = ["dep:serde", "spacetimedb-sats/serde", "dep:serde_with", "chrono/serde"]
cli = ["clap"]
serde = ["dep:serde", "spacetimedb-sats/serde"]
# Allows using `Arbitrary` impls defined in this crate.
proptest = ["dep:proptest", "dep:proptest-derive"]

Expand All @@ -27,14 +26,11 @@ spacetimedb-primitives = { path = "../primitives", version = "0.7.3" }

anyhow.workspace = true
bitflags.workspace = true
chrono = { workspace = true, optional = true }
clap = {workspace = true, optional = true }
derive_more.workspace = true
enum-as-inner.workspace = true
hex.workspace = true
itertools.workspace = true
serde = { workspace = true, optional = true }
serde_with = {workspace = true, optional = true }
thiserror.workspace = true
tracing.workspace = true

Expand Down
5 changes: 0 additions & 5 deletions crates/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use spacetimedb_sats::{impl_serialize, WithTypespace};
pub mod address;
pub mod filter;
pub mod identity;
pub mod name;
pub mod operator;
pub mod primary_key;
pub mod type_def {
Expand All @@ -19,10 +18,6 @@ pub mod type_value {
}

pub mod error;
#[cfg(feature = "serde")]
pub mod recovery;
#[cfg(feature = "cli")]
pub mod util;
pub mod version;

pub use address::Address;
Expand Down
Loading

0 comments on commit d503311

Please sign in to comment.