Skip to content

Commit

Permalink
draw: try to infer transformer by file extension
Browse files Browse the repository at this point in the history
  • Loading branch information
Kilerd committed Apr 20, 2023
1 parent bbc02d2 commit f568625
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ zhang-server = {version="0.1", path="../server"}
text-transformer = {version="0.1", path="../transformers/text"}
text-exporter = {version="0.1", path="../exporters/text"}

beancount-transformer = {version="0.1", path="../transformers/beancount"}


tokio = { version = "1", features = ['full', "tracing"] }
tokio-util = { version = "0.6", features = ["io", "compat"] }
env_logger = "0.9.0"
Expand Down
40 changes: 31 additions & 9 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
use std::marker::PhantomData;
use std::path::PathBuf;
use std::sync::Arc;

use beancount_transformer::BeancountTransformer;
use clap::{Args, Parser};
use env_logger::Env;
use log::LevelFilter;
use text_exporter::TextExporter;
use text_transformer::TextTransformer;
use zhang_core::exporter::AppendableExporter;
use zhang_core::ledger::Ledger;
use zhang_core::transform::Transformer;
use zhang_server::ServeConfig;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -78,6 +81,11 @@ pub struct ServerOpts {
pub no_report: bool,
}

enum SupportedFormat {
Zhang,
Beancount,
}

impl Opts {
pub async fn run(self) {
match self {
Expand All @@ -92,23 +100,37 @@ impl Opts {
}
Opts::Export(_) => todo!(),
Opts::Serve(opts) => {
// todo(feat): detect transformer and exporter based on file extension
let format = match opts.endpoint.rsplit_once(".") {
Some((_, "bc")) => SupportedFormat::Beancount,

This comment has been minimized.

Copy link
@Harry-Chen

Harry-Chen Apr 23, 2023

Contributor

'.bean' is also commonly used

_ => SupportedFormat::Zhang,
};

let exporter: Arc<dyn AppendableExporter> = Arc::new(TextExporter {});
zhang_server::serve::<TextTransformer>(ServeConfig {
path: opts.path,
endpoint: opts.endpoint,
port: opts.port,
database: opts.database,
no_report: opts.no_report,
exporter,
})
zhang_server::serve(
infer_transformer(&format),
ServeConfig {
path: opts.path,
endpoint: opts.endpoint,
port: opts.port,
database: opts.database,
no_report: opts.no_report,
exporter,
},
)
.await
.expect("cannot serve")
}
}
}
}

fn infer_transformer(format: &SupportedFormat) -> Box<dyn Transformer + 'static> {
match format {
SupportedFormat::Zhang => Box::new(TextTransformer::default()),
SupportedFormat::Beancount => Box::new(BeancountTransformer::default()),
}
}

// impl ExportOpts {
// pub async fn run(self) {
// let result = match self {
Expand Down
3 changes: 2 additions & 1 deletion server/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use std::marker::PhantomData;
use std::net::{Ipv4Addr, SocketAddrV4};
use std::path::PathBuf;
use std::sync::Arc;
Expand Down Expand Up @@ -59,7 +60,7 @@ pub struct ServeConfig {
pub exporter: Arc<dyn AppendableExporter>,
}

pub async fn serve<T: Transformer + Default + 'static>(opts: ServeConfig) -> ZhangResult<()> {
pub async fn serve<T: Transformer + Default + 'static>(transformer: T, opts: ServeConfig) -> ZhangResult<()> {
info!(
"version: {}, build date: {}",
env!("CARGO_PKG_VERSION"),
Expand Down

0 comments on commit f568625

Please sign in to comment.