diff options
author | 2021-05-23 10:31:38 -0700 | |
---|---|---|
committer | 2021-05-23 10:31:38 -0700 | |
commit | 9d5b84b88ddc2b7de1bc0f03d9026eb52e8976ac (patch) | |
tree | 735b1031148d4cddfa1a0e01f52b02e23e94f9cb /examples/todo/main.rs | |
parent | 4cffa16633f83b6604f7ce3de47d40a29e6a9b54 (diff) | |
download | notion-9d5b84b88ddc2b7de1bc0f03d9026eb52e8976ac.tar.gz notion-9d5b84b88ddc2b7de1bc0f03d9026eb52e8976ac.tar.zst notion-9d5b84b88ddc2b7de1bc0f03d9026eb52e8976ac.zip |
Add initial todo example (#13)
Diffstat (limited to 'examples/todo/main.rs')
-rw-r--r-- | examples/todo/main.rs | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/examples/todo/main.rs b/examples/todo/main.rs new file mode 100644 index 0000000..2283549 --- /dev/null +++ b/examples/todo/main.rs @@ -0,0 +1,76 @@ +mod commands; + +use anyhow::{Context, Result}; +use clap::Clap; +use notion::models::DatabaseId; +use notion::NotionApi; +use serde::{Deserialize, Serialize}; + +// https://docs.rs/clap/3.0.0-beta.2/clap/ +#[derive(Clap)] +#[clap(version = "1.0", author = "Kevin K. <kbknapp@gmail.com>")] +struct Opts { + #[clap(subcommand)] + command: SubCommand, +} + +#[derive(Clap)] +enum SubCommand { + /// Configure what database this notion-todo example uses + Config, + /// List all todos + List, + /// Add a todo item to the notion database + Add, + /// Complete a todo item + Check, +} + +#[derive(Deserialize, Serialize)] +struct TodoConfig { + api_token: Option<String>, + task_database_id: Option<DatabaseId>, +} + +#[tokio::main] +async fn main() -> Result<()> { + let opts: Opts = Opts::parse(); + + // https://docs.rs/config/0.11.0/config/ + let config = config::Config::default() + .with_merged(config::File::with_name("todo_config")) + .unwrap_or_default() + .with_merged(config::Environment::with_prefix("NOTION"))?; + + let config: TodoConfig = config.try_into().context("Failed to read config")?; + + let notion_api = NotionApi::new( + std::env::var("NOTION_API_TOKEN") + .or(config + .api_token + .ok_or(anyhow::anyhow!("No api token from config"))) + .context( + "No Notion API token found in either the environment variable \ + `NOTION_API_TOKEN` or the config file!", + )?, + )?; + + match opts.command { + SubCommand::Config => commands::configure::configure(notion_api).await, + SubCommand::List => list_tasks(notion_api), + SubCommand::Add => add_task(notion_api), + SubCommand::Check => complete_task(notion_api), + } +} + +fn list_tasks(_notion_api: NotionApi) -> Result<()> { + Ok(()) +} + +fn add_task(_notion_api: NotionApi) -> Result<()> { + Ok(()) +} + +fn complete_task(_notion_api: NotionApi) -> Result<()> { + Ok(()) +} |