diff --git a/meh_http_server_rest/Cargo.toml b/meh_http_server_rest/Cargo.toml index d16f5b7..5474946 100644 --- a/meh_http_server_rest/Cargo.toml +++ b/meh_http_server_rest/Cargo.toml @@ -16,4 +16,4 @@ slog = { version = "2.6.0", default-features = false } [features] default = ["std"] -std = ["meh_http_common/std", "meh_http_server/std", "slog/std"] \ No newline at end of file +std = ["meh_http_common/std", "meh_http_server/std", "slog/std", "serde/std", "serde_json/std"] \ No newline at end of file diff --git a/meh_http_server_rest/src/error_handler.rs b/meh_http_server_rest/src/error_handler.rs index 07f20ab..4a7a03d 100644 --- a/meh_http_server_rest/src/error_handler.rs +++ b/meh_http_server_rest/src/error_handler.rs @@ -1,4 +1,5 @@ -use std::marker::PhantomData; +use core::marker::PhantomData; +use alloc::boxed::Box; use crate::{HandlerResult, RestErrorContext, middleware::{HttpMiddleware, HttpMiddlewareContext, HttpMiddlewareRunner}, response_builder::HttpResponseBuilder}; use async_trait::async_trait; use meh_http_common::{resp::HttpStatusCodes}; diff --git a/meh_http_server_rest/src/extras.rs b/meh_http_server_rest/src/extras.rs index 82fbd38..45ef902 100644 --- a/meh_http_server_rest/src/extras.rs +++ b/meh_http_server_rest/src/extras.rs @@ -1,9 +1,10 @@ -use std::{any::{Any, TypeId}, collections::HashMap}; - +use core::any::{Any, TypeId}; +use alloc::collections::BTreeMap; +use alloc::boxed::Box; #[derive(Default)] pub struct Extras { - extras: HashMap>, + extras: BTreeMap>, } impl Extras { pub fn get<'a, T: 'static>(&'a self) -> Option<&'a T> diff --git a/meh_http_server_rest/src/helpers.rs b/meh_http_server_rest/src/helpers.rs index e0f3629..551addf 100644 --- a/meh_http_server_rest/src/helpers.rs +++ b/meh_http_server_rest/src/helpers.rs @@ -1,5 +1,5 @@ -use std::marker::PhantomData; - +use core::marker::PhantomData; +use alloc::boxed::Box; use meh_http_common::{req::HttpServerHeader, resp::HttpStatusCodes}; use slog::{warn}; use async_trait::async_trait; diff --git a/meh_http_server_rest/src/lib.rs b/meh_http_server_rest/src/lib.rs index 26f370e..b2418c3 100644 --- a/meh_http_server_rest/src/lib.rs +++ b/meh_http_server_rest/src/lib.rs @@ -13,13 +13,14 @@ pub mod response_builder; pub mod error_handler; pub mod quick_rest; -use std::borrow::Cow; use meh_http_common::stack::{TcpError}; use middleware::HttpMiddlewareContext; use response_builder::{HttpReponseComplete, HttpResponseBuilder}; pub type RestResult = Result; +use alloc::borrow::Cow; + #[derive(Debug)] pub enum RestError { TcpError(TcpError), diff --git a/meh_http_server_rest/src/middleware.rs b/meh_http_server_rest/src/middleware.rs index 5a7a8f1..5423c6d 100644 --- a/meh_http_server_rest/src/middleware.rs +++ b/meh_http_server_rest/src/middleware.rs @@ -1,4 +1,5 @@ -use std::{marker::PhantomData}; +use core::marker::PhantomData; +use alloc::boxed::Box; use meh_http_common::{stack::TcpSocket}; use async_trait::async_trait; use meh_http_server::HttpContext; diff --git a/meh_http_server_rest/src/middleware_chain.rs b/meh_http_server_rest/src/middleware_chain.rs index 903c680..1920491 100644 --- a/meh_http_server_rest/src/middleware_chain.rs +++ b/meh_http_server_rest/src/middleware_chain.rs @@ -1,6 +1,6 @@ -use std::marker::PhantomData; -use std::ops::Add; - +use core::marker::PhantomData; +use core::ops::Add; +use alloc::boxed::Box; use crate::HandlerResult; use crate::middleware::HttpMiddlewareContext; use crate::middleware::HttpMiddlewareRunner; diff --git a/meh_http_server_rest/src/middleware_fn.rs b/meh_http_server_rest/src/middleware_fn.rs index 8d861d6..d08b69a 100644 --- a/meh_http_server_rest/src/middleware_fn.rs +++ b/meh_http_server_rest/src/middleware_fn.rs @@ -1,5 +1,5 @@ -use std::pin::Pin; - +use core::pin::Pin; +use alloc::boxed::Box; use async_trait::async_trait; use futures::Future; use crate::{HandlerResult, HandlerResultOk, middleware::{HttpMiddleware, HttpMiddlewareContext, HttpMiddlewareRunner}, response_builder::HttpResponseBuilder}; diff --git a/meh_http_server_rest/src/openapi.rs b/meh_http_server_rest/src/openapi.rs index f057f97..77c97cf 100644 --- a/meh_http_server_rest/src/openapi.rs +++ b/meh_http_server_rest/src/openapi.rs @@ -1,5 +1,7 @@ -use std::{borrow::Cow, collections::HashMap}; - +use alloc::borrow::Cow; +use alloc::collections::BTreeMap; +use alloc::vec::Vec; +use alloc::string::String; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Clone)] @@ -8,7 +10,7 @@ pub struct OpenApi { pub openapi_version: Cow<'static, str>, pub info: Info, pub servers: Vec, - pub paths: HashMap, Path> + pub paths: BTreeMap } #[derive(Serialize, Deserialize, Clone)] @@ -27,7 +29,7 @@ pub struct Server { #[derive(Serialize, Deserialize, Clone)] pub struct Path { #[serde(flatten)] - pub methods: HashMap, PathMethod> + pub methods: BTreeMap, PathMethod> } #[derive(Serialize, Deserialize, Clone)] @@ -39,13 +41,13 @@ pub struct PathMethod { #[serde(rename="requestBody")] #[serde(skip_serializing_if = "Option::is_none")] pub request_body: Option, - pub responses: HashMap, Response> + pub responses: BTreeMap, Response> } #[derive(Serialize, Deserialize, Clone)] pub struct Response { pub description: Cow<'static, str>, - pub content: HashMap, ResponseContent> + pub content: BTreeMap, ResponseContent> } #[derive(Serialize, Deserialize, Clone)] @@ -56,7 +58,7 @@ pub struct ResponseContent { #[derive(Serialize, Deserialize, Clone)] pub struct RequestBody { pub required: bool, - pub content: HashMap, RequestContent> + pub content: BTreeMap, RequestContent> } #[derive(Serialize, Deserialize, Clone)] diff --git a/meh_http_server_rest/src/quick_rest/mod.rs b/meh_http_server_rest/src/quick_rest/mod.rs index fc003d6..56d435d 100644 --- a/meh_http_server_rest/src/quick_rest/mod.rs +++ b/meh_http_server_rest/src/quick_rest/mod.rs @@ -1,5 +1,11 @@ -use std::{borrow::Cow, collections::HashMap, marker::PhantomData}; - +use alloc::borrow::Cow; +use alloc::collections::BTreeMap; +use alloc::string::String; +use alloc::boxed::Box; +use alloc::vec::Vec; +use alloc::string::ToString; + +use core::marker::PhantomData; use meh_http_common::{req::HttpServerHeader, resp::HttpStatusCodes}; use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_json::{json, Map, Value}; @@ -21,12 +27,12 @@ use async_trait::async_trait; struct OpenApiContext { is_openapi_request: bool, - apis: HashMap, OpenApiContextApi>, + apis: BTreeMap, } struct OpenApiContextApi { path: Cow<'static, str>, - paths: HashMap, Path>, + paths: BTreeMap, combined_getters: Vec, } @@ -56,7 +62,7 @@ where return Ok(ctx.into()); }; - let mut paths = HashMap::new(); + let mut paths = BTreeMap::::new(); for (api_id, api) in &openapi.apis { let api_url = format!("{}", api_id); @@ -108,7 +114,7 @@ where }, ); - paths.extend(api.paths.clone()); + paths.extend(api.paths.iter().map(|(k, v)| (k.clone().to_string(), v.clone()))); } // handle the GET request @@ -198,9 +204,10 @@ where == Some(true) && ctx.request.method.as_ref().map(|s| s.as_str()) == Some("GET"); + let open_api = OpenApiContext { is_openapi_request, - apis: HashMap::new(), + apis: BTreeMap::new() }; ctx.extras.insert(open_api); @@ -403,13 +410,13 @@ where if let Some(openapi_ctx) = ctx.extras.get_mut::() { openapi_ctx .apis - .entry(v.api.clone()) + .entry(v.api.to_string()) .or_insert_with(|| OpenApiContextApi { path: v.api.clone(), - paths: HashMap::new(), + paths: BTreeMap::new(), combined_getters: vec![], }); - openapi_ctx.apis.entry(v.api.clone()).and_modify(|e| { + openapi_ctx.apis.entry(v.api.to_string()).and_modify(|e| { e.combined_getters.push(g); }); } @@ -479,11 +486,11 @@ where } } ); - let mut methods = HashMap::new(); + let mut methods = BTreeMap::new(); // get { - let mut response_contents = HashMap::new(); + let mut response_contents = BTreeMap::new(); response_contents.insert( "application/json".into(), ResponseContent { @@ -491,7 +498,7 @@ where }, ); - let mut responses = HashMap::new(); + let mut responses = BTreeMap::new(); responses.insert( "200".into(), Response { @@ -513,12 +520,12 @@ where // set { - let mut responses = HashMap::new(); + let mut responses = BTreeMap::new(); responses.insert( "204".into(), Response { description: "Successfully set the new value".into(), - content: HashMap::new(), + content: BTreeMap::new(), }, ); @@ -543,16 +550,16 @@ where ); } - let p: Cow = format!("{}/{}", api, id).into(); - openapi.apis.entry(api.clone()).or_insert_with({ + let p = format!("{}/{}", api, id); + openapi.apis.entry(api.clone().to_string()).or_insert_with({ let api = api.clone(); move || OpenApiContextApi { path: api.clone().into(), - paths: HashMap::new(), + paths: BTreeMap::new(), combined_getters: vec![], } }); - openapi.apis.entry(api.clone()).and_modify(|e| { + openapi.apis.entry(api.clone().to_string()).and_modify(|e| { e.paths.insert(p, Path { methods }); }); }; diff --git a/meh_http_server_rest/src/response_builder.rs b/meh_http_server_rest/src/response_builder.rs index d4ca9cf..208c2bc 100644 --- a/meh_http_server_rest/src/response_builder.rs +++ b/meh_http_server_rest/src/response_builder.rs @@ -1,5 +1,5 @@ -use std::ops::Deref; - +use core::ops::Deref; +use alloc::vec::Vec; use meh_http_common::{req::HttpServerHeader, resp::{HttpResponseWriter, HttpStatusCodes}, stack::TcpSocket}; use meh_http_server::HttpContext;