Compare commits
1 Commits
master
...
any-select
Author | SHA1 | Date |
---|---|---|
puffaboo | 4b8243ed58 |
29
src/main.rs
29
src/main.rs
|
@ -13,10 +13,12 @@ use crate::{
|
||||||
config::{FediverseConfig, Publisher},
|
config::{FediverseConfig, Publisher},
|
||||||
publish::MastodonPublisher,
|
publish::MastodonPublisher,
|
||||||
publish::MisskeyPublisher,
|
publish::MisskeyPublisher,
|
||||||
selection::{telegram::get_chat_ref, SelectorExt, TelegramSelector},
|
selection::{telegram::get_chat_ref, AnySelector, SelectorExt, TelegramSelector},
|
||||||
};
|
};
|
||||||
|
|
||||||
use futures::{SinkExt, StreamExt, TryStreamExt, channel::mpsc::channel, future::Either, sink::unfold};
|
use futures::{
|
||||||
|
channel::mpsc::channel, future::Either, sink::unfold, SinkExt, StreamExt, TryStreamExt,
|
||||||
|
};
|
||||||
use model::SampleModelExt;
|
use model::SampleModelExt;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
|
@ -66,20 +68,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
|
||||||
|
|
||||||
let cfg_clone = cfg.clone();
|
let cfg_clone = cfg.clone();
|
||||||
|
|
||||||
let mut model = TelegramSelector::new(
|
let mut model = AnySelector::new()
|
||||||
api,
|
|
||||||
chat,
|
|
||||||
Box::pin(unfold((), move |_, chat_ref| {
|
|
||||||
let mut cfg_clone = cfg_clone.clone();
|
|
||||||
async move {
|
|
||||||
if let ChatRef::Id(id) = &chat_ref {
|
|
||||||
cfg_clone.chat_ref = id.clone();
|
|
||||||
let _ = cfg_clone.save(CONFIG_PATH);
|
|
||||||
}
|
|
||||||
Ok::<_, Infallible>(())
|
|
||||||
}
|
|
||||||
})),
|
|
||||||
)
|
|
||||||
.filter(
|
.filter(
|
||||||
model::GPTSampleModel::new(
|
model::GPTSampleModel::new(
|
||||||
cfg.python_path.clone(),
|
cfg.python_path.clone(),
|
||||||
|
@ -145,9 +134,11 @@ async fn resolve_publisher(
|
||||||
config: &mut config::Config,
|
config: &mut config::Config,
|
||||||
) -> Result<Either<MisskeyPublisher, MastodonPublisher>, Box<dyn Error>> {
|
) -> Result<Either<MisskeyPublisher, MastodonPublisher>, Box<dyn Error>> {
|
||||||
let publisher = match &config.publisher {
|
let publisher = match &config.publisher {
|
||||||
config::Publisher::Misskey(cfg) => {
|
config::Publisher::Misskey(cfg) => Either::Left(MisskeyPublisher::new(
|
||||||
Either::Left(MisskeyPublisher::new(&cfg.base_url, cfg.token.clone(), cfg.visibility)?)
|
&cfg.base_url,
|
||||||
}
|
cfg.token.clone(),
|
||||||
|
cfg.visibility,
|
||||||
|
)?),
|
||||||
config::Publisher::Mastodon(cfg) => {
|
config::Publisher::Mastodon(cfg) => {
|
||||||
let app = AppBuilder {
|
let app = AppBuilder {
|
||||||
client_name: "izzilis",
|
client_name: "izzilis",
|
||||||
|
|
|
@ -0,0 +1,23 @@
|
||||||
|
use std::convert::Infallible;
|
||||||
|
|
||||||
|
use futures::future::BoxFuture;
|
||||||
|
|
||||||
|
use super::Selector;
|
||||||
|
|
||||||
|
#[derive(Debug, Copy, Clone)]
|
||||||
|
pub struct AnySelector;
|
||||||
|
|
||||||
|
impl Selector for AnySelector {
|
||||||
|
type Error = Box<Infallible>;
|
||||||
|
type Response = BoxFuture<'static, Result<bool, Self::Error>>;
|
||||||
|
|
||||||
|
fn review(&self, message: String) -> Self::Response {
|
||||||
|
Box::pin(async move { Ok(message.len() != 0) })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl AnySelector {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Self {}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,29 +0,0 @@
|
||||||
use std::error::Error;
|
|
||||||
|
|
||||||
use async_std::io::stdin;
|
|
||||||
use futures::future::BoxFuture;
|
|
||||||
|
|
||||||
use super::Selector;
|
|
||||||
|
|
||||||
#[derive(Debug, Copy, Clone)]
|
|
||||||
pub struct ConsoleSelector;
|
|
||||||
|
|
||||||
impl Selector for ConsoleSelector {
|
|
||||||
type Error = Box<dyn Error>;
|
|
||||||
type Response = BoxFuture<'static, Result<bool, Self::Error>>;
|
|
||||||
|
|
||||||
fn review(&self, message: String) -> Self::Response {
|
|
||||||
println!("{} (y/N) ", message);
|
|
||||||
let stdin = stdin();
|
|
||||||
Box::pin(async move {
|
|
||||||
let mut buffer = String::new();
|
|
||||||
stdin.read_line(&mut buffer).await?;
|
|
||||||
Ok(
|
|
||||||
match buffer.chars().next().unwrap_or('n').to_ascii_lowercase() {
|
|
||||||
'y' => true,
|
|
||||||
_ => false,
|
|
||||||
},
|
|
||||||
)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,9 +2,9 @@ use futures::{stream::BoxStream, Future, Stream, TryStreamExt};
|
||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
mod console;
|
mod any;
|
||||||
pub mod telegram;
|
pub mod telegram;
|
||||||
pub use console::ConsoleSelector;
|
pub use any::AnySelector;
|
||||||
pub use telegram::TelegramSelector;
|
pub use telegram::TelegramSelector;
|
||||||
|
|
||||||
pub trait Selector {
|
pub trait Selector {
|
||||||
|
|
Loading…
Reference in New Issue