Skip to content

Commit

Permalink
no_std fixes, btreemap compiler bug workaround
Browse files Browse the repository at this point in the history
  • Loading branch information
rudib committed Nov 9, 2021
1 parent 57a88cd commit 1a0312a
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 42 deletions.
2 changes: 1 addition & 1 deletion meh_http_server_rest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
std = ["meh_http_common/std", "meh_http_server/std", "slog/std", "serde/std", "serde_json/std"]
3 changes: 2 additions & 1 deletion meh_http_server_rest/src/error_handler.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
7 changes: 4 additions & 3 deletions meh_http_server_rest/src/extras.rs
Original file line number Diff line number Diff line change
@@ -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<TypeId, Box<dyn Any + Send + Sync>>,
extras: BTreeMap<TypeId, Box<dyn Any + Send + Sync>>,
}
impl Extras {
pub fn get<'a, T: 'static>(&'a self) -> Option<&'a T>
Expand Down
4 changes: 2 additions & 2 deletions meh_http_server_rest/src/helpers.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
3 changes: 2 additions & 1 deletion meh_http_server_rest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T = ()> = Result<T, RestError>;

use alloc::borrow::Cow;

#[derive(Debug)]
pub enum RestError {
TcpError(TcpError),
Expand Down
3 changes: 2 additions & 1 deletion meh_http_server_rest/src/middleware.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
6 changes: 3 additions & 3 deletions meh_http_server_rest/src/middleware_chain.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions meh_http_server_rest/src/middleware_fn.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down
16 changes: 9 additions & 7 deletions meh_http_server_rest/src/openapi.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand All @@ -8,7 +10,7 @@ pub struct OpenApi {
pub openapi_version: Cow<'static, str>,
pub info: Info,
pub servers: Vec<Server>,
pub paths: HashMap<Cow<'static, str>, Path>
pub paths: BTreeMap<String, Path>
}

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -27,7 +29,7 @@ pub struct Server {
#[derive(Serialize, Deserialize, Clone)]
pub struct Path {
#[serde(flatten)]
pub methods: HashMap<Cow<'static, str>, PathMethod>
pub methods: BTreeMap<Cow<'static, str>, PathMethod>
}

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -39,13 +41,13 @@ pub struct PathMethod {
#[serde(rename="requestBody")]
#[serde(skip_serializing_if = "Option::is_none")]
pub request_body: Option<RequestBody>,
pub responses: HashMap<Cow<'static, str>, Response>
pub responses: BTreeMap<Cow<'static, str>, Response>
}

#[derive(Serialize, Deserialize, Clone)]
pub struct Response {
pub description: Cow<'static, str>,
pub content: HashMap<Cow<'static, str>, ResponseContent>
pub content: BTreeMap<Cow<'static, str>, ResponseContent>
}

#[derive(Serialize, Deserialize, Clone)]
Expand All @@ -56,7 +58,7 @@ pub struct ResponseContent {
#[derive(Serialize, Deserialize, Clone)]
pub struct RequestBody {
pub required: bool,
pub content: HashMap<Cow<'static, str>, RequestContent>
pub content: BTreeMap<Cow<'static, str>, RequestContent>
}

#[derive(Serialize, Deserialize, Clone)]
Expand Down
45 changes: 26 additions & 19 deletions meh_http_server_rest/src/quick_rest/mod.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -21,12 +27,12 @@ use async_trait::async_trait;

struct OpenApiContext {
is_openapi_request: bool,
apis: HashMap<Cow<'static, str>, OpenApiContextApi>,
apis: BTreeMap<String, OpenApiContextApi>,
}

struct OpenApiContextApi {
path: Cow<'static, str>,
paths: HashMap<Cow<'static, str>, Path>,
paths: BTreeMap<String, Path>,
combined_getters: Vec<OpenApiGetter>,
}

Expand Down Expand Up @@ -56,7 +62,7 @@ where
return Ok(ctx.into());
};

let mut paths = HashMap::new();
let mut paths = BTreeMap::<String, Path>::new();
for (api_id, api) in &openapi.apis {
let api_url = format!("{}", api_id);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -403,13 +410,13 @@ where
if let Some(openapi_ctx) = ctx.extras.get_mut::<OpenApiContext>() {
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);
});
}
Expand Down Expand Up @@ -479,19 +486,19 @@ 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 {
schema: schema.clone(),
},
);

let mut responses = HashMap::new();
let mut responses = BTreeMap::new();
responses.insert(
"200".into(),
Response {
Expand All @@ -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(),
},
);

Expand All @@ -543,16 +550,16 @@ where
);
}

let p: Cow<str> = 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 });
});
};
Expand Down
4 changes: 2 additions & 2 deletions meh_http_server_rest/src/response_builder.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down

0 comments on commit 1a0312a

Please sign in to comment.