diff options
author | Vika <vika@fireburn.ru> | 2023-07-29 21:59:56 +0300 |
---|---|---|
committer | Vika <vika@fireburn.ru> | 2023-07-29 21:59:56 +0300 |
commit | 0617663b249f9ca488e5de652108b17d67fbaf45 (patch) | |
tree | 11564b6c8fa37bf9203a0a4cc1c4e9cc088cb1a5 /kittybox-rs/util/src/lib.rs | |
parent | 26c2b79f6a6380ae3224e9309b9f3352f5717bd7 (diff) | |
download | kittybox-0617663b249f9ca488e5de652108b17d67fbaf45.tar.zst |
Moved the entire Kittybox tree into the root
Diffstat (limited to 'kittybox-rs/util/src/lib.rs')
-rw-r--r-- | kittybox-rs/util/src/lib.rs | 123 |
1 files changed, 0 insertions, 123 deletions
diff --git a/kittybox-rs/util/src/lib.rs b/kittybox-rs/util/src/lib.rs deleted file mode 100644 index c49bdf5..0000000 --- a/kittybox-rs/util/src/lib.rs +++ /dev/null @@ -1,123 +0,0 @@ -#![warn(missing_docs)] -//! Small things that couldn't fit elsewhere in Kittybox, yet may be -//! useful on their own or in multiple Kittybox crates. -//! -//! Some things are gated behind features, namely: -//! - `fs` - enables use of filesystem-related utilities -use serde::{Deserialize, Serialize}; - -#[derive(Clone, Serialize, Deserialize)] -pub struct IndiewebEndpoints { - pub authorization_endpoint: String, - pub token_endpoint: String, - pub webmention: Option<String>, - pub microsub: Option<String>, -} - -/// Data structure representing a Micropub channel in the ?q=channels output. -#[derive(Serialize, Deserialize, PartialEq, Debug)] -#[cfg_attr(feature = "sqlx", derive(sqlx::FromRow))] -pub struct MicropubChannel { - /// The channel's UID. It is usually also a publically accessible permalink URL. - pub uid: String, - /// The channel's user-friendly name used to recognize it in lists. - pub name: String, -} - -#[derive(Debug, Default)] -/// Common types of webmentions. -pub enum MentionType { - /// Corresponds to a `u-in-reply-to` link. - Reply, - /// Corresponds to a `u-like-of` link. - Like, - /// Corresponds to a `u-repost-of` link. - Repost, - /// Corresponds to a `u-bookmark-of` link. - Bookmark, - /// A plain link without MF2 annotations. - #[default] - Mention -} - -/// Common errors from the IndieWeb protocols that can be reused between modules. -pub mod error; -pub use error::{ErrorType, MicropubError}; - -/// Common data-types useful in creating smart authentication systems. -pub mod auth { - #[derive(PartialEq, Eq, Hash, Clone, Copy)] - pub enum EnrolledCredential { - /// An indicator that a password is enrolled. Passwords can be - /// used to recover from a lost token. - Password, - /// An indicator that one or more WebAuthn credentials were - /// enrolled. - WebAuthn - } -} - -/// A collection of traits for implementing a robust job queue. -pub mod queue; - -#[cfg(feature = "fs")] -/// Commonly-used operations with the file system in Kittybox's -/// underlying storage mechanisms. -pub mod fs { - use std::io::{self, Result}; - use std::path::{Path, PathBuf}; - use rand::{Rng, distributions::Alphanumeric}; - use tokio::fs; - - /// Create a temporary file named `temp.[a-zA-Z0-9]{length}` in - /// the given location and immediately open it. Returns the - /// filename and the corresponding file handle. It is the caller's - /// responsibility to clean up the temporary file when it is no - /// longer needed. - /// - /// Uses [`OpenOptions::create_new`][fs::OpenOptions::create_new] - /// to detect filename collisions, in which case it will - /// automatically retry until the operation succeeds. - /// - /// # Errors - /// - /// Returns the underlying [`io::Error`] if the operation fails - /// due to reasons other than filename collision. - pub async fn mktemp<T, B>(dir: T, basename: B, length: usize) -> Result<(PathBuf, fs::File)> - where - T: AsRef<Path>, - B: Into<Option<&'static str>> - { - let dir = dir.as_ref(); - let basename = basename.into().unwrap_or(""); - fs::create_dir_all(dir).await?; - - loop { - let filename = dir.join(format!( - "{}{}{}", - basename, - if basename.is_empty() { "" } else { "." }, - { - let string = rand::thread_rng() - .sample_iter(&Alphanumeric) - .take(length) - .collect::<Vec<u8>>(); - String::from_utf8(string).unwrap() - } - )); - - match fs::OpenOptions::new() - .create_new(true) - .write(true) - .open(&filename) - .await - { - Ok(file) => return Ok((filename, file)), - Err(err) => match err.kind() { - io::ErrorKind::AlreadyExists => continue, - _ => return Err(err) - } - } - } - } -} |