move files out from crate

This commit is contained in:
Namekuji 2023-06-02 08:48:12 -04:00
parent 403e95b673
commit 148dbbe56d
No known key found for this signature in database
GPG Key ID: B541BD6E646CABC7
102 changed files with 82 additions and 91 deletions

View File

@ -4,15 +4,40 @@ name = "native-utils"
version = "0.0.0" version = "0.0.0"
[workspace] [workspace]
members = ["crates/*"] members = ["migration/Cargo.toml"]
[features]
default = ["napi"]
noarray = []
napi = ["dep:napi", "dep:napi-derive"]
[lib] [lib]
crate-type = ["cdylib"] crate-type = ["cdylib", "lib"]
[dependencies] [dependencies]
async-trait = "0.1.68"
cfg-if = "1.0.0"
chrono = "0.4.24"
cuid2 = "0.1.0"
derive_more = "0.99.17"
jsonschema = "0.17.0"
once_cell = "1.17.1"
parse-display = "0.8.0"
rand = "0.8.5"
schemars = { version = "0.8.12", features = ["chrono"] }
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "postgres-array", "sqlx-sqlite", "runtime-tokio-rustls"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
thiserror = "1.0.40"
tokio = { version = "1.28.1", features = ["full"] }
utoipa = "3.3.0"
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.0", default-features = false, features = ["napi4"] } napi = { version = "2.12.0", default-features = false, features = ["napi4", "tokio_rt"], optional = true }
napi-derive = "2.12.0" napi-derive = { version = "2.12.0", optional = true }
[dev-dependencies]
pretty_assertions = "1.3.0"
[build-dependencies] [build-dependencies]
napi-build = "2.0.1" napi-build = "2.0.1"

View File

@ -1,12 +0,0 @@
[package]
name = "database"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
once_cell = "1.17.1"
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "runtime-tokio-rustls"] }
thiserror = "1.0.40"
tokio = { version = "1.28.1", features = ["macros"] }

View File

@ -1,35 +0,0 @@
[package]
name = "model"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[features]
default = ["napi"]
noarray = []
napi = ["dep:napi", "dep:napi-derive"]
[dependencies]
async-trait = "0.1.68"
cfg-if = "1.0.0"
chrono = "0.4.24"
database = { path = "../database" }
derive_more = "0.99.17"
jsonschema = "0.17.0"
once_cell = "1.17.1"
parse-display = "0.8.0"
schemars = { version = "0.8.12", features = ["chrono"] }
sea-orm = { version = "0.11.3", features = ["sqlx-postgres", "postgres-array", "sqlx-sqlite", "runtime-tokio-rustls"] }
serde = { version = "1.0.163", features = ["derive"] }
serde_json = "1.0.96"
thiserror = "1.0.40"
tokio = { version = "1.28.1", features = ["sync"] }
util = { path = "../util" }
utoipa = "3.3.0"
# Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
napi = { version = "2.12.0", default-features = false, features = ["napi4"], optional = true }
napi-derive = { version = "2.12.0", optional = true }
[dev-dependencies]
pretty_assertions = "1.3.0"

View File

@ -1,15 +0,0 @@
[package]
name = "util"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
cuid2 = "0.1.0"
once_cell = "1.17.1"
rand = "0.8.5"
thiserror = "1.0.40"
[dev-dependencies]
pretty_assertions = "1.3.0"

View File

@ -1,9 +1,8 @@
pub mod error; pub mod error;
use error::Error;
use sea_orm::{Database, DbConn}; use sea_orm::{Database, DbConn};
use crate::error::Error;
static DB_CONN: once_cell::sync::OnceCell<DbConn> = once_cell::sync::OnceCell::new(); static DB_CONN: once_cell::sync::OnceCell<DbConn> = once_cell::sync::OnceCell::new();
pub async fn init_database(connection_uri: impl Into<String>) -> Result<(), Error> { pub async fn init_database(connection_uri: impl Into<String>) -> Result<(), Error> {
@ -18,8 +17,7 @@ pub fn get_database() -> Result<&'static DbConn, Error> {
#[cfg(test)] #[cfg(test)]
mod unit_test { mod unit_test {
use super::get_database; use super::{error::Error, get_database};
use crate::error::Error;
#[test] #[test]
fn error_uninitialized() { fn error_uninitialized() {

View File

@ -1 +1,6 @@
pub mod database;
pub mod model;
pub mod util;
#[cfg(feature = "napi")]
pub mod mastodon_api; pub mod mastodon_api;

View File

@ -3,9 +3,16 @@ pub enum Error {
#[error("Failed to parse string")] #[error("Failed to parse string")]
ParseError(#[from] parse_display::ParseError), ParseError(#[from] parse_display::ParseError),
#[error("Failed to get database connection")] #[error("Failed to get database connection")]
DbConnError(#[from] database::error::Error), DbConnError(#[from] crate::database::error::Error),
#[error("Database operation error: {0}")] #[error("Database operation error: {0}")]
DbOperationError(#[from] sea_orm::DbErr), DbOperationError(#[from] sea_orm::DbErr),
#[error("Requested entity not found")] #[error("Requested entity not found")]
NotFound, NotFound,
} }
#[cfg(feature = "napi")]
impl Into<napi::Error> for Error {
fn into(self) -> napi::Error {
napi::Error::from_reason(self.to_string())
}
}

View File

@ -3,7 +3,7 @@ pub mod antenna;
use async_trait::async_trait; use async_trait::async_trait;
use schemars::JsonSchema; use schemars::JsonSchema;
use crate::error::Error; use super::error::Error;
#[async_trait] #[async_trait]
pub trait Repository<T: JsonSchema> { pub trait Repository<T: JsonSchema> {
@ -14,7 +14,10 @@ pub trait Repository<T: JsonSchema> {
mod macros { mod macros {
macro_rules! impl_pack_by_id { macro_rules! impl_pack_by_id {
($a:ty, $b:ident) => { ($a:ty, $b:ident) => {
match <$a>::find_by_id($b).one(database::get_database()?).await? { match <$a>::find_by_id($b)
.one(crate::database::get_database()?)
.await?
{
None => Err(Error::NotFound), None => Err(Error::NotFound),
Some(m) => m.pack().await, Some(m) => m.pack().await,
} }

View File

@ -2,9 +2,10 @@ use async_trait::async_trait;
use cfg_if::cfg_if; use cfg_if::cfg_if;
use sea_orm::{ColumnTrait, EntityTrait, QueryFilter}; use sea_orm::{ColumnTrait, EntityTrait, QueryFilter};
use crate::entity::{antenna, antenna_note, user_group_joining}; use crate::database;
use crate::error::Error; use crate::model::entity::{antenna, antenna_note, user_group_joining};
use crate::schema::Antenna; use crate::model::error::Error;
use crate::model::schema::Antenna;
use super::macros::impl_pack_by_id; use super::macros::impl_pack_by_id;
use super::Repository; use super::Repository;

View File

@ -5,7 +5,8 @@ use schemars::JsonSchema;
use utoipa::ToSchema; use utoipa::ToSchema;
use super::Schema; use super::Schema;
use crate::entity::sea_orm_active_enums::AntennaSrcEnum; use crate::model;
use crate::model::entity::sea_orm_active_enums::AntennaSrcEnum;
#[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)] #[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -47,10 +48,10 @@ pub enum AntennaSrc {
} }
impl TryFrom<AntennaSrcEnum> for super::AntennaSrc { impl TryFrom<AntennaSrcEnum> for super::AntennaSrc {
type Error = crate::error::Error; type Error = model::error::Error;
fn try_from(value: AntennaSrcEnum) -> Result<Self, Self::Error> { fn try_from(value: AntennaSrcEnum) -> Result<Self, Self::Error> {
value.to_string().parse().map_err(crate::error::Error::from) value.to_string().parse().map_err(model::error::Error::from)
} }
} }
@ -64,7 +65,7 @@ mod unit_test {
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serde_json::json; use serde_json::json;
use crate::{entity::sea_orm_active_enums::AntennaSrcEnum, schema::AntennaSrc}; use crate::model::{entity::sea_orm_active_enums::AntennaSrcEnum, schema::AntennaSrc};
use super::VALIDATOR; use super::VALIDATOR;

View File

@ -1,9 +1,12 @@
use napi::bindgen_prelude::{FromNapiValue, ToNapiValue}; use napi::bindgen_prelude::*;
use napi_derive::napi; use napi_derive::napi;
use parse_display::FromStr; use parse_display::FromStr;
use schemars::JsonSchema; use schemars::JsonSchema;
use utoipa::ToSchema; use utoipa::ToSchema;
use crate::model::entity::antenna::Model;
use crate::model::repository::Repository;
#[napi] #[napi]
#[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)] #[derive(Clone, Debug, PartialEq, Eq, JsonSchema, ToSchema)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
@ -44,3 +47,11 @@ pub enum AntennaSrc {
Group, Group,
Instances, Instances,
} }
#[napi]
impl Antenna {
#[napi]
pub async fn pack_by_id(id: String) -> napi::Result<Antenna> {
Model::pack_by_id(id).await.map_err(Into::into)
}
}

View File

@ -25,7 +25,7 @@ mod unit_test {
use pretty_assertions::{assert_eq, assert_ne}; use pretty_assertions::{assert_eq, assert_ne};
use std::thread; use std::thread;
use crate::id; use crate::util::id;
#[test] #[test]
fn can_generate_unique_ids() { fn can_generate_unique_ids() {

View File

@ -1,20 +1,19 @@
#![cfg(not(feature = "napi"))] #![cfg(not(feature = "napi"))]
extern crate model; mod model;
mod repository;
use chrono::Utc; use chrono::Utc;
use model::entity; use native_utils::database;
use model::entity::sea_orm_active_enums::AntennaSrcEnum; use native_utils::model::entity;
use native_utils::model::entity::sea_orm_active_enums::AntennaSrcEnum;
use native_utils::util::{
id::{create_id, init_id},
random::gen_string,
};
use sea_orm::{ use sea_orm::{
sea_query::TableCreateStatement, ActiveModelTrait, ConnectionTrait, DbBackend, DbConn, DbErr, sea_query::TableCreateStatement, ActiveModelTrait, ConnectionTrait, DbBackend, DbConn, DbErr,
EntityTrait, IntoActiveModel, TransactionTrait, EntityTrait, IntoActiveModel, TransactionTrait,
}; };
use util::{
id::{create_id, init_id},
random::gen_string,
};
/// Insert predefined entries in the database. /// Insert predefined entries in the database.
async fn prepare() { async fn prepare() {

View File

@ -0,0 +1 @@
mod repository;

Some files were not shown because too many files have changed in this diff Show More