From cf5562823dfbcba6a62e44ae7d0c43d79fb1f254 Mon Sep 17 00:00:00 2001 From: Ben Weber Date: Mon, 28 Oct 2024 23:13:03 +0100 Subject: [PATCH 1/2] Remove examples dir --- examples/todo/README.md | 19 -------- examples/todo/commands.rs | 1 - examples/todo/commands/configure.rs | 71 --------------------------- examples/todo/main.rs | 76 ----------------------------- 4 files changed, 167 deletions(-) delete mode 100644 examples/todo/README.md delete mode 100644 examples/todo/commands.rs delete mode 100644 examples/todo/commands/configure.rs delete mode 100644 examples/todo/main.rs diff --git a/examples/todo/README.md b/examples/todo/README.md deleted file mode 100644 index b22d08b..0000000 --- a/examples/todo/README.md +++ /dev/null @@ -1,19 +0,0 @@ -# Notion database todo example - -This example is builds a todo list using a notion database. - -## Setup your notion api token - -Create an `internal` integration here: https://www.notion.so/my-integrations - -```bash - export NOTION_API_TOKEN='secret_token_here' -``` -> Notice the space before the export command. -> This will prevent your terminal from storing this token in your shell history... - -## Selecting the database to use - -```bash -cargo run --example todo -- config -``` diff --git a/examples/todo/commands.rs b/examples/todo/commands.rs deleted file mode 100644 index a78a393..0000000 --- a/examples/todo/commands.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod configure; diff --git a/examples/todo/commands/configure.rs b/examples/todo/commands/configure.rs deleted file mode 100644 index ac4cd23..0000000 --- a/examples/todo/commands/configure.rs +++ /dev/null @@ -1,71 +0,0 @@ -use crate::TodoConfig; -use anyhow::Result; -use rusticnotion::ids::{AsIdentifier, DatabaseId}; -use rusticnotion::models::search::NotionSearch; -use rusticnotion::models::Database; -use rusticnotion::NotionApi; -use skim::{Skim, SkimItem, SkimItemReceiver, SkimItemSender, SkimOptions}; -use std::borrow::Cow; -use std::ops::Deref; -use std::sync::Arc; - -fn skim_select_database(databases: Vec) -> Result { - let options = SkimOptions::default(); - - let (sender, receiver): (SkimItemSender, SkimItemReceiver) = crossbeam_channel::bounded(500); - - struct SkimDB { - db: Database, - } - - impl SkimItem for SkimDB { - fn text(&self) -> Cow { - Cow::Owned(self.db.title_plain_text()) - } - } - - for db in databases { - sender.send(Arc::new(SkimDB { db }))?; - } - - // `run_with` would read and show items from the stream - let selected_items = Skim::run_with(&options, Some(receiver)) - .filter(|out| !out.is_abort) - .map(|out| out.selected_items) - .unwrap_or_default(); - - let db = selected_items - .first() - .expect("No database selected, aborting...") - .clone(); - let db: &SkimDB = db - .deref() - .as_any() - .downcast_ref() - .expect("Couldn't cast back to SkimDB"); - - let database_id = db.db.as_id(); - - Ok(database_id.clone()) -} - -pub async fn configure(notion_api: NotionApi) -> Result<()> { - let databases: Vec = notion_api - .search(NotionSearch::filter_by_databases()) - .await? - .only_databases() - .results; - - let database_id = skim_select_database(databases)?; - - println!("Selected database's id: {}", database_id); - - let bytes = toml::to_vec(&TodoConfig { - api_token: None, - task_database_id: Some(database_id), - })?; - - std::fs::write("../todo_config.toml", bytes)?; - - Ok(()) -} diff --git a/examples/todo/main.rs b/examples/todo/main.rs deleted file mode 100644 index 8dab53e..0000000 --- a/examples/todo/main.rs +++ /dev/null @@ -1,76 +0,0 @@ -mod commands; - -use anyhow::{Context, Result}; -use clap::Parser; -use rusticnotion::ids::DatabaseId; -use rusticnotion::NotionApi; -use serde::{Deserialize, Serialize}; - -// From -#[derive(Parser, Debug)] -#[clap(version = "1.0", author = "Jake Swenson")] -struct Opts { - #[clap(subcommand)] - command: SubCommand, -} - -#[derive(Parser, Debug)] -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, - task_database_id: Option, -} - -#[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(()) -} From ddee02be04eebe370f69ea3e2a7345358cbccfba Mon Sep 17 00:00:00 2001 From: Ben Weber Date: Mon, 28 Oct 2024 23:13:45 +0100 Subject: [PATCH 2/2] Make tokio dev dependency --- Cargo.toml | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1f0e54a..490209c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,15 +23,13 @@ features = ["serde", "clock"] version = "0.11" features = ["json"] -[dependencies.tokio] -version = "1" -features = ["full"] [dependencies.serde] version = "1.0" features = ["derive"] [dev-dependencies] +tokio = { version = "1", features = ["full"] } cargo-husky = "1" wiremock = "0.5.2" anyhow = "1.0.40"