Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation with detailed explanations #5

Merged
merged 2 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ plugins = [
"path:crates/tasmota",
"path:crates/weather",
]
bin = [
"path:crates/litehouse",
"path:crates/litehouse-config",
"path:crates/plugin",
"path:crates/plugin-macro",
]
71 changes: 71 additions & 0 deletions crates/litehouse-config/src/capabilities.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
//! Capabilities for Litehouse plugins.
//!
//! This module defines the capabilities that can be granted to plugins, allowing them to interact with the system and external resources in a controlled manner.

use serde::{Deserialize, Serialize};
use std::fmt;
use std::str::FromStr;
use thiserror::Error;

/// Represents the different capabilities that can be granted to plugins.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Capability {
/// Allows the plugin to start an HTTP server on the specified port.
HttpServer(u16),
/// Allows the plugin to make HTTP requests to the specified URL.
HttpClient(String),
}

impl Serialize for Capability {
fn serialize<S>(&self, serializer: S) -> std::prelude::v1::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let string = self.to_string();
serializer.serialize_str(&string)
}
}

impl<'de> Deserialize<'de> for Capability {
fn deserialize<D>(deserializer: D) -> std::prelude::v1::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
s.parse().map_err(serde::de::Error::custom)
}
}

impl fmt::Display for Capability {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Capability::HttpServer(port) => write!(f, "http-server:{}", port),
Capability::HttpClient(url) => write!(f, "http-client:{}", url),
}
}
}

impl FromStr for Capability {
type Err = CapabilityParseError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parts: Vec<&str> = s.splitn(2, ':').collect();
match parts.as_slice() {
["http-server", port] => port
.parse()
.map(Capability::HttpServer)
.map_err(|_| CapabilityParseError::InvalidPort),
["http-client", url] => Ok(Capability::HttpClient(url.to_string())),
_ => Err(CapabilityParseError::InvalidFormat),
}
}
}

/// Errors that can occur when parsing a string into a `Capability`.
#[derive(Debug, Error)]
pub enum CapabilityParseError {
#[error("invalid capability format")]
InvalidFormat,
#[error("invalid port number")]
InvalidPort,
}
4 changes: 2 additions & 2 deletions crates/litehouse-config/src/hash_read.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use pin_project_lite::pin_project;
use std::task::Poll;
use tokio::io::AsyncRead;

use digest::Digest;
use pin_project_lite::pin_project;
use tokio::io::AsyncRead;

pin_project! {
pub struct HashRead<T, H> {
Expand Down
Loading
Loading