You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
230 lines
5.0 KiB
230 lines
5.0 KiB
#![cfg_attr(nightly, feature(label_break_value))]
|
|
#![cfg_attr(nightly, feature(const_fn))]
|
|
#![cfg_attr(nightly, feature(never_type))]
|
|
|
|
|
|
#![allow(dead_code)]
|
|
#![allow(unused_macros)]
|
|
|
|
#[macro_use] extern crate tracing;
|
|
#[macro_use] extern crate pin_project;
|
|
use std::{
|
|
convert::{TryFrom, TryInto},
|
|
};
|
|
use color_eyre::{
|
|
eyre::{
|
|
self,
|
|
WrapErr,
|
|
},
|
|
Help, SectionExt,
|
|
};
|
|
use lazy_static::lazy_static;
|
|
use serde::{Serialize, Deserialize};
|
|
use tracing_futures::Instrument;
|
|
|
|
|
|
macro_rules! cfg_debug {
|
|
(if {$($if:tt)*} else {$($else:tt)*}) => {
|
|
{
|
|
cfg_if::cfg_if!{
|
|
if #[cfg(debug_assertions)] {
|
|
$($if)*
|
|
} else {
|
|
$($else)*
|
|
}
|
|
}
|
|
}
|
|
};
|
|
}
|
|
macro_rules! nightly {
|
|
(else $($else:tt)*) => {
|
|
cfg_if::cfg_if! {
|
|
if #[cfg(not(nightly))] {
|
|
$($else)*
|
|
}
|
|
}
|
|
};
|
|
(if {$($if:tt)*} else {$($else:tt)*}) => {
|
|
|
|
cfg_if::cfg_if!{
|
|
if #[cfg(nightly)] {
|
|
$($if)*
|
|
} else {
|
|
$($else)*
|
|
}
|
|
}
|
|
};
|
|
($($tt:tt)*) => {
|
|
cfg_if::cfg_if! {
|
|
if #[cfg(nightly)] {
|
|
$($tt)*
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
macro_rules! static_assert {
|
|
($expr:expr) => {
|
|
const _: [(); 1] = [(); ((!!$expr) as bool) as usize];
|
|
};
|
|
($expr:expr, $lit:literal) => {
|
|
static_assert!{$expr}
|
|
}
|
|
}
|
|
|
|
pub const CURRENT_VERSION: version::Version = version::Version::new(0,0,0,version::Tag::Prerelease);
|
|
|
|
mod bytes;
|
|
mod ext;
|
|
use ext::*;
|
|
mod version;
|
|
mod config;
|
|
mod resolve;
|
|
mod args;
|
|
|
|
mod format;
|
|
mod work;
|
|
|
|
pub mod timestamp
|
|
{
|
|
use chrono::prelude::*;
|
|
/// Get the current timestamp
|
|
pub fn now() -> i64
|
|
{
|
|
#[cfg(feature="local-time")] return Local::now().timestamp();
|
|
#[cfg(not(feature="local-time"))] Utc::now().timestamp()
|
|
}
|
|
}
|
|
|
|
pub mod serialise
|
|
{
|
|
use super::*;
|
|
|
|
/// Serialise this object into base64 text
|
|
///
|
|
/// # TODO
|
|
/// Line wrapping
|
|
#[instrument(err, skip(value), fields(value_type=?std::any::type_name::<T>()))]
|
|
pub fn into_text<T: Serialize>(value: &T) -> Result<String, Error>
|
|
{
|
|
let bytes = serde_cbor::to_vec(value).map_err(Error::Serialise)?;
|
|
Ok(base64::encode(bytes))
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub enum Error
|
|
{
|
|
Base64(base64::DecodeError),
|
|
Serialise(serde_cbor::Error),
|
|
Deserialise(serde_cbor::Error),
|
|
}
|
|
|
|
impl std::error::Error for Error
|
|
{
|
|
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
|
|
match &self {
|
|
Self::Base64(b) => Some(b),
|
|
Self::Serialise(b) => Some(b),
|
|
Self::Deserialise(b) => Some(b),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl std::fmt::Display for Error
|
|
{
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result
|
|
{
|
|
match self {
|
|
Self::Base64(_) => write!(f, "base64 decode failed"),
|
|
Self::Serialise(_) => write!(f, "serialisation failed"),
|
|
Self::Deserialise(_) => write!(f, "deserialisation failed"),
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Try to deserialise this object from text.
|
|
#[instrument(err, skip(string), fields(string = ?string.as_ref()))]
|
|
pub fn from_text<T: serde::de::DeserializeOwned>(string: impl AsRef<[u8]>) -> Result<T, Error>
|
|
{
|
|
let bytes = base64::decode(string).map_err(Error::Base64)?;
|
|
serde_cbor::from_reader(&bytes[..]).map_err(Error::Deserialise)
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests
|
|
{
|
|
use crate::*;
|
|
#[test]
|
|
fn test_ser() -> Result<(), eyre::Report>
|
|
{
|
|
let object = "hello world";
|
|
let enc = super::into_text(&String::from(object)).wrap_err(eyre::eyre!("failed to encode"))?;
|
|
println!("Enc: {:?}", enc);
|
|
let dec: String = super::from_text(enc).wrap_err(eyre::eyre!("failed to decode"))?;
|
|
assert_eq!(object,dec);
|
|
Ok(())
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
/*/// Dispatch params operations that can be handled at top level (i.e. `Help`)
|
|
async fn dispatch_args() -> Result<args::Operation, eyre::Report>
|
|
{
|
|
match args::parse_args().await
|
|
.with_section(move || std::env::args().skip(1).join("\n").header("Input args were:"))? {
|
|
|
|
}
|
|
}*/
|
|
|
|
|
|
fn install_tracing() {
|
|
use tracing_error::ErrorLayer;
|
|
use tracing_subscriber::prelude::*;
|
|
use tracing_subscriber::{fmt, EnvFilter};
|
|
|
|
let fmt_layer = fmt::layer().with_target(false);
|
|
let filter_layer = EnvFilter::try_from_default_env()
|
|
.or_else(|_| EnvFilter::try_new("info"))
|
|
.unwrap();
|
|
|
|
tracing_subscriber::registry()
|
|
.with(filter_layer)
|
|
.with(fmt_layer)
|
|
.with(ErrorLayer::default())
|
|
.init();
|
|
}
|
|
|
|
#[instrument]
|
|
async fn work(op: config::Operation) -> Result<(), eyre::Report>
|
|
{
|
|
debug!("Got op");
|
|
match op {
|
|
config::Operation::Help => args::usage(),
|
|
config::Operation::GenerateKey(config::op::GenerateKey::Aes(aes)) => {
|
|
|
|
},
|
|
config::Operation::GenerateKey(config::op::GenerateKey::Rsa(rsa)) => {
|
|
|
|
},
|
|
_ => todo!(),
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
#[instrument]
|
|
#[tokio::main]
|
|
async fn main() -> Result<(), eyre::Report> {
|
|
install_tracing();
|
|
color_eyre::install()?;
|
|
|
|
trace!("Parsing args");
|
|
let args = args::parse_args().await?;
|
|
|
|
work(args).await
|
|
.with_suggestion(|| "Run with `RUST_LOG=\"debug\"` environment variable for more detailed logging")
|
|
.with_suggestion(|| "Run with `RUST_LOG=\"trace\"` environment variable for extremely detailed logging")?;
|
|
|
|
Ok(())
|
|
}
|