Skip to content

Commit

Permalink
Merge pull request #5 from arlyon/update-docs
Browse files Browse the repository at this point in the history
Update documentation with detailed explanations
  • Loading branch information
arlyon authored May 9, 2024
2 parents f8fb25a + e5814db commit 8789bca
Show file tree
Hide file tree
Showing 8 changed files with 575 additions and 442 deletions.
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

0 comments on commit 8789bca

Please sign in to comment.