Skip to content
Merged
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
2 changes: 1 addition & 1 deletion build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use vergen::{Config, vergen};
use vergen::{vergen, Config};

fn main() {
vergen(Config::default()).unwrap();
Expand Down
22 changes: 17 additions & 5 deletions gitarena-common/src/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ pub async fn create_postgres_pool(module: &'static str, max_conns: Option<u32>)
.after_connect(move |connection| {
Box::pin(async move {
// If setting the app name fails it's not a big deal if the connection is still fine so let's ignore the error
let _ = connection.execute(ONCE.get_or_init(|| format!("set application_name = '{}';", module)).as_str()).await;
let _ = connection
.execute(
ONCE.get_or_init(|| format!("set application_name = '{}';", module))
.as_str(),
)
.await;
Ok(())
})
})
Expand All @@ -57,17 +62,24 @@ async fn read_database_config() -> Result<ConnectOptions> {
let password = fs::read_to_string(file).await?;
options = options.password(password.as_str());
}
Err(VarError::NotUnicode(_)) => bail!("`DATABASE_PASSWORD_FILE` environment variable is not valid unicode"),
Err(VarError::NotPresent) => { /* No password auth required, or it was already set in the connection string; safe to ignore */ }
Err(VarError::NotUnicode(_)) => {
bail!("`DATABASE_PASSWORD_FILE` environment variable is not valid unicode")
}
Err(VarError::NotPresent) => { /* No password auth required, or it was already set in the connection string; safe to ignore */
}
}

Ok(options)
}

fn get_max_connections() -> Result<u32> {
Ok(match env::var("MAX_POOL_CONNECTIONS") {
Ok(env_str) => env_str.parse::<u32>().context("Unable to parse MAX_POOL_CONNECTIONS environment variable into a u32")?,
Ok(env_str) => env_str
.parse::<u32>()
.context("Unable to parse MAX_POOL_CONNECTIONS environment variable into a u32")?,
Err(VarError::NotPresent) => num_cpus::get() as u32,
Err(VarError::NotUnicode(_)) => bail!("MAX_POOL_CONNECTIONS environment variable is not a valid unicode string")
Err(VarError::NotUnicode(_)) => {
bail!("MAX_POOL_CONNECTIONS environment variable is not a valid unicode string")
}
})
}
8 changes: 4 additions & 4 deletions gitarena-common/src/database/models.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt::{Display, Formatter};
use std::fmt;
use std::fmt::{Display, Formatter};

use anyhow::{bail, Error};
use serde::{Deserialize, Serialize};
Expand All @@ -12,7 +12,7 @@ pub enum KeyType {
EcdsaSha2Nistp256,
EcdsaSha2Nistp384,
EcdsaSha2Nistp521,
SshEd25519
SshEd25519,
}

impl Display for KeyType {
Expand All @@ -24,7 +24,7 @@ impl Display for KeyType {
EcdsaSha2Nistp256 => "ecdsa-sha2-nistp256",
EcdsaSha2Nistp384 => "ecdsa-sha2-nistp384",
EcdsaSha2Nistp521 => "ecdsa-sha2-nistp521",
SshEd25519 => "ssh-ed25519"
SshEd25519 => "ssh-ed25519",
})
}
}
Expand All @@ -41,7 +41,7 @@ impl TryFrom<&str> for KeyType {
"ecdsa-sha2-nistp384" => EcdsaSha2Nistp384,
"ecdsa-sha2-nistp521" => EcdsaSha2Nistp521,
"ssh-ed25519" => SshEd25519,
_ => bail!("Unknown key type: {}", value)
_ => bail!("Unknown key type: {}", value),
})
}
}
23 changes: 17 additions & 6 deletions gitarena-common/src/ipc.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,43 @@
use std::fmt::{Debug, Display, Formatter};
use std::io::{Read, Write};
use std::{fmt, fs, mem};
use std::fmt::{Debug, Display, Formatter};

use anyhow::{Context, Result};
use bincode::config::{AllowTrailing, Bounded, LittleEndian, VarintEncoding, WithOtherEndian, WithOtherIntEncoding, WithOtherLimit, WithOtherTrailing};
use bincode::config::{
AllowTrailing, Bounded, LittleEndian, VarintEncoding, WithOtherEndian, WithOtherIntEncoding,
WithOtherLimit, WithOtherTrailing,
};
use bincode::{DefaultOptions, Options as _};
use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};

// World's longest type, thank you
pub type BincodeType = WithOtherTrailing<WithOtherIntEncoding<WithOtherEndian<WithOtherLimit<DefaultOptions, Bounded>, LittleEndian>, VarintEncoding>, AllowTrailing>;
pub type BincodeType = WithOtherTrailing<
WithOtherIntEncoding<
WithOtherEndian<WithOtherLimit<DefaultOptions, Bounded>, LittleEndian>,
VarintEncoding,
>,
AllowTrailing,
>;

/// [Type-length-value](https://en.wikipedia.org/wiki/Type%E2%80%93length%E2%80%93value) packet to be used for GitArena IPC
#[derive(Deserialize, Serialize)]
pub struct IpcPacket<T: ?Sized> {
id: u64,
length: u64,
data: T
data: T,
}

impl<T: Serialize + Sized + PacketId> IpcPacket<T> {
pub fn new(data: T) -> Self {
let size = Self::bincode().serialized_size(&data).unwrap_or(mem::size_of::<T>() as u64);
let size = Self::bincode()
.serialized_size(&data)
.unwrap_or(mem::size_of::<T>() as u64);

IpcPacket {
id: data.id(),
length: size,
data
data,
}
}
}
Expand Down
34 changes: 23 additions & 11 deletions gitarena-common/src/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ use tracing_subscriber::fmt::Layer;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, layer, Registry};
use tracing_subscriber::{layer, EnvFilter, Registry};
use tracing_unwrap::ResultExt;

// Keep in sync with `gitarena::init_logger`
pub fn init_logger(module: &str, directives: &'static [&str]) -> Result<Vec<WorkerGuard>> {
let mut guards = Vec::new();

let env_filter = EnvFilter::try_from_default_env().unwrap_or_else(|err| default_env(err, directives));
let env_filter =
EnvFilter::try_from_default_env().unwrap_or_else(|err| default_env(err, directives));

let stdout_layer = stdout().map(|(layer, guard)| {
guards.push(guard);
Expand Down Expand Up @@ -49,21 +50,22 @@ pub fn init_logger(module: &str, directives: &'static [&str]) -> Result<Vec<Work
Ok(guards)
}

pub fn stdout<S: Subscriber + for<'a> LookupSpan<'a>>() -> Option<(impl layer::Layer<S>, WorkerGuard)> {
pub fn stdout<S: Subscriber + for<'a> LookupSpan<'a>>(
) -> Option<(impl layer::Layer<S>, WorkerGuard)> {
if env::var_os("NO_STDOUT_LOG").is_some() {
return None;
}

let (writer, guard) = tracing_appender::non_blocking(io::stdout());

let layer = Layer::new()
.with_thread_ids(true)
.with_writer(writer);
let layer = Layer::new().with_thread_ids(true).with_writer(writer);

Some((layer, guard))
}

pub fn log_file<S: Subscriber + for<'a> LookupSpan<'a>>(module: &str) -> Result<Option<(impl layer::Layer<S>, WorkerGuard)>> {
pub fn log_file<S: Subscriber + for<'a> LookupSpan<'a>>(
module: &str,
) -> Result<Option<(impl layer::Layer<S>, WorkerGuard)>> {
if cfg!(debug_assertions) || env::var_os("DEBUG_FILE_LOG").is_none() {
return Ok(None);
}
Expand All @@ -85,7 +87,9 @@ pub fn log_file<S: Subscriber + for<'a> LookupSpan<'a>>(module: &str) -> Result<
Ok(Some((layer, guard)))
}

pub fn tokio_console<S: Subscriber + for<'a> LookupSpan<'a>>(filter: EnvFilter) -> (EnvFilter, Option<impl layer::Layer<S>>) {
pub fn tokio_console<S: Subscriber + for<'a> LookupSpan<'a>>(
filter: EnvFilter,
) -> (EnvFilter, Option<impl layer::Layer<S>>) {
if !cfg!(tokio_unstable) {
return (filter, None);
}
Expand All @@ -100,12 +104,20 @@ pub fn tokio_console<S: Subscriber + for<'a> LookupSpan<'a>>(filter: EnvFilter)
}

pub fn default_env(err: FromEnvError, directives: &[&str]) -> EnvFilter {
let not_found = err.source()
.map(|o| o.downcast_ref::<VarError>().map_or_else(|| false, |err| matches!(err, VarError::NotPresent)))
let not_found = err
.source()
.map(|o| {
o.downcast_ref::<VarError>()
.map_or_else(|| false, |err| matches!(err, VarError::NotPresent))
})
.unwrap_or(false);

if !not_found {
eprintln!("Warning: Unable to parse `{}` environment variable, using default values: {}", EnvFilter::DEFAULT_ENV, err);
eprintln!(
"Warning: Unable to parse `{}` environment variable, using default values: {}",
EnvFilter::DEFAULT_ENV,
err
);
}

let level = if cfg!(debug_assertions) {
Expand Down
4 changes: 2 additions & 2 deletions gitarena-common/src/packets/git.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::{Deserialize, Serialize};
use gitarena_macros::IpcPacket;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Debug, Default, IpcPacket)]
#[ipc(packet = "Git", id = 1)] // = 1001
pub struct GitImport {
pub url: String,
pub username: Option<String>,
pub password: Option<String>
pub password: Option<String>,
}
4 changes: 2 additions & 2 deletions gitarena-common/src/packets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pub mod git; // 1xxx

#[repr(u64)]
pub enum PacketCategory {
Git = 1000
Git = 1000,
}

// TODO: Find a way to automatically generate this
Expand All @@ -13,5 +13,5 @@ pub enum PacketCategory {
#[repr(u64)]
#[derive(FromPrimitive, ToPrimitive)]
pub enum PacketId {
GitImport = 1001
GitImport = 1001,
}
25 changes: 15 additions & 10 deletions gitarena-macros/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::fmt::{Debug, Formatter};

use proc_macro2::TokenStream;
use proc_macro::TokenStream as ProcMacroTS;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::punctuated::Punctuated;
use syn::{Ident, LitStr, parse_macro_input, Token, Type};
use syn::{parse_macro_input, Ident, LitStr, Token, Type};

pub(crate) fn from_config(input: ProcMacroTS) -> ProcMacroTS {
let settings = parse_macro_input!(input as SettingsList);
Expand Down Expand Up @@ -42,7 +42,7 @@ pub(crate) fn from_optional_config(input: ProcMacroTS) -> ProcMacroTS {

#[derive(Debug)]
struct SettingsList {
settings: Vec<Setting>
settings: Vec<Setting>,
}

impl SettingsList {
Expand All @@ -52,7 +52,12 @@ impl SettingsList {

fn as_optional(&self) -> OptionalSettingsList {
OptionalSettingsList {
settings: self.settings.iter().cloned().map(|s| s.as_optional()).collect()
settings: self
.settings
.iter()
.cloned()
.map(|s| s.as_optional())
.collect(),
}
}
}
Expand All @@ -62,7 +67,7 @@ impl Parse for SettingsList {
let punctuated = Punctuated::<Setting, Token![,]>::parse_terminated(input)?;

Ok(SettingsList {
settings: punctuated.iter().cloned().collect::<Vec<Setting>>()
settings: punctuated.iter().cloned().collect::<Vec<Setting>>(),
})
}
}
Expand All @@ -83,7 +88,7 @@ impl ToTokens for SettingsList {
struct Setting {
identifier: Ident,
key: String,
ty: Type
ty: Type,
}

impl Debug for Setting {
Expand All @@ -98,7 +103,7 @@ impl Debug for Setting {
impl Setting {
fn as_optional(&self) -> OptionalSetting {
OptionalSetting {
original: self.clone()
original: self.clone(),
}
}
}
Expand All @@ -114,7 +119,7 @@ impl Parse for Setting {
Ok(Setting {
identifier: ident,
key: key_str,
ty
ty,
})
}
}
Expand All @@ -137,7 +142,7 @@ impl ToTokens for Setting {

#[derive(Debug)]
struct OptionalSettingsList {
settings: Vec<OptionalSetting>
settings: Vec<OptionalSetting>,
}

impl ToTokens for OptionalSettingsList {
Expand All @@ -154,7 +159,7 @@ impl ToTokens for OptionalSettingsList {

#[derive(Debug)]
struct OptionalSetting {
original: Setting
original: Setting,
}

impl ToTokens for OptionalSetting {
Expand Down
17 changes: 11 additions & 6 deletions gitarena-macros/src/ipc_packet.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
use proc_macro2::{Ident, Span};
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span};
use proc_macro_error::{emit_call_site_error, emit_error};
use quote::quote;
use syn::spanned::Spanned;
use syn::{DeriveInput, Lit, Meta, NestedMeta, parse_macro_input};
use syn::{parse_macro_input, DeriveInput, Lit, Meta, NestedMeta};

pub(crate) fn ipc_packet(input: TokenStream) -> TokenStream {
let mut input = parse_macro_input!(input as DeriveInput);
let mut input = parse_macro_input!(input as DeriveInput);
let identifier = input.ident;

let mut category = None;
let mut packet_id = None;

input.attrs.retain(|attribute| {
if let Ok(Meta::List(list)) = attribute.parse_meta() {
let ipc = list.path.segments.first().map(|segment| segment.ident == "ipc").unwrap_or_default();
let ipc = list
.path
.segments
.first()
.map(|segment| segment.ident == "ipc")
.unwrap_or_default();

if ipc {
for args in list.nested {
Expand Down Expand Up @@ -57,7 +62,7 @@ pub(crate) fn ipc_packet(input: TokenStream) -> TokenStream {
_ => emit_error! {
segment.span(),
"unknown identifier, expected `packet` or `id`"
}
},
}
}
}
Expand All @@ -78,7 +83,7 @@ pub(crate) fn ipc_packet(input: TokenStream) -> TokenStream {

match chars.next() {
Some(c) => c.to_uppercase().collect::<String>() + chars.as_str(),
None => String::new()
None => String::new(),
}
};

Expand Down
Loading