chore: add orgize-sync package

This commit is contained in:
PoiScript 2019-09-15 12:09:59 +08:00
parent bed20e6112
commit 1fb4d920e5
5 changed files with 181 additions and 0 deletions

50
orgize-sync/src/conf.rs Normal file
View file

@ -0,0 +1,50 @@
use app_dirs::{app_root, AppDataType, AppInfo};
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use crate::error::Result;
#[derive(Serialize, Deserialize)]
pub struct Conf {
pub files: Vec<File>,
#[cfg(feature = "google_calendar")]
pub google_calendar: Option<GoogleCalendarGlobalConf>,
}
#[derive(Serialize, Deserialize)]
#[cfg(feature = "google_calendar")]
pub struct GoogleCalendarGlobalConf {
pub client_id: String,
pub client_secret: String,
pub token_dir: String,
pub token_filename: String,
pub property: String,
}
#[derive(Serialize, Deserialize)]
#[cfg(feature = "google_calendar")]
pub struct GoogleCalendarConf {
pub calendar: String,
pub append_new: bool,
pub append_headline: String,
}
#[derive(Serialize, Deserialize)]
pub struct File {
pub path: String,
pub name: String,
#[cfg(feature = "google_calendar")]
pub google_calendar: Option<GoogleCalendarConf>,
}
pub fn default_conf_path() -> Result<PathBuf> {
let mut path = app_root(
AppDataType::UserConfig,
&AppInfo {
name: "orgize-sync",
author: "PoiScript",
},
)?;
path.push("conf.toml");
Ok(path)
}

46
orgize-sync/src/error.rs Normal file
View file

@ -0,0 +1,46 @@
use app_dirs::AppDirsError;
use isahc::http::Error as HttpError;
use isahc::Error as IsahcError;
use std::convert::From;
use std::io::Error as IOError;
use url::ParseError;
#[derive(Debug)]
pub enum Error {
AppDirs(AppDirsError),
IO(IOError),
Http(IsahcError),
Url(ParseError),
}
impl From<AppDirsError> for Error {
fn from(err: AppDirsError) -> Self {
Error::AppDirs(err)
}
}
impl From<IOError> for Error {
fn from(err: IOError) -> Self {
Error::IO(err)
}
}
impl From<IsahcError> for Error {
fn from(err: IsahcError) -> Self {
Error::Http(err)
}
}
impl From<HttpError> for Error {
fn from(err: HttpError) -> Self {
Error::Http(err.into())
}
}
impl From<ParseError> for Error {
fn from(err: ParseError) -> Self {
Error::Url(err)
}
}
pub type Result<T> = std::result::Result<T, Error>;

58
orgize-sync/src/main.rs Normal file
View file

@ -0,0 +1,58 @@
mod conf;
mod error;
use std::path::PathBuf;
use structopt::StructOpt;
use crate::conf::default_conf_path;
use crate::error::Result;
#[derive(StructOpt, Debug)]
#[structopt(name = "orgize-sync")]
struct Opt {
#[structopt(subcommand)]
subcommand: Cmd,
}
#[derive(StructOpt, Debug)]
enum Cmd {
#[structopt(name = "init")]
Init,
#[structopt(name = "sync")]
Sync {
#[structopt(long = "skip-google-calendar")]
skip_google_calendar: bool,
#[structopt(long = "skip-toggl")]
skip_toggl: bool,
#[structopt(short = "c", long = "conf", parse(from_os_str))]
conf_path: Option<PathBuf>,
},
#[structopt(name = "conf")]
Conf,
}
fn main() -> Result<()> {
let opt = Opt::from_args();
match opt.subcommand {
Cmd::Sync {
conf_path,
skip_google_calendar,
skip_toggl,
} => {
let conf_path = conf_path
.map(Result::Ok)
.unwrap_or_else(default_conf_path)?;
println!("{:#?}", conf_path);
if cfg!(feature = "google_calendar") && !skip_google_calendar {}
if cfg!(feature = "toggl") && !skip_toggl {}
}
Cmd::Init => (),
Cmd::Conf => (),
}
Ok(())
}