collection wrapper for serde

4-update-check
plazmoid 4 years ago
parent 9ccf69e692
commit 2864511f7f
  1. 4
      bin/u_agent/src/main.rs
  2. 8
      bin/u_server/src/handlers.rs
  3. 4
      bin/u_server/src/main.rs
  4. 6
      lib/u_lib/src/client/network.rs
  5. 26
      lib/u_lib/src/contracts/datatypes.rs
  6. 8
      lib/u_lib/src/contracts/messaging.rs
  7. 2
      lib/u_lib/src/contracts/mod.rs

@ -26,7 +26,7 @@ async fn main() {
retry_until_ok!(instance.init(&cli_info).await);
loop {
let jobs = retry_until_ok!(instance.get_jobs().await);
if jobs.len() > 0 {
if jobs.0.len() > 0 {
println!("{:?}", jobs);
}
sleep(Duration::from_secs(2));
@ -35,7 +35,7 @@ async fn main() {
#[macro_export]
macro_rules! retry_until_ok {
( $body:stmt ) => {
( $body:expr ) => {
loop {
match $body {
Ok(r) => break r,

@ -12,7 +12,7 @@ pub async fn add_client(
let mut clients = db.lock().await;
if clients.contains_key(&new_cli.id) {
Ok(warp::reply::json(
&RawMsg("Already exist".to_string()).into_message()
&RawMsg("Already exist".to_string()).as_message()
))
} else {
clients.insert(
@ -20,7 +20,7 @@ pub async fn add_client(
UClient::new(new_cli.into_owned())
);
Ok(warp::reply::json(
&RawMsg("Added".to_string()).into_message()
&RawMsg("Added".to_string()).as_message()
))
}
}
@ -42,7 +42,7 @@ pub async fn get_jobs(
pub async fn set_jobs(
uid: Option<Uuid>,
msg: Message<'_, JobStorageWrapper>,
msg: Message<'_, CollectionWrapper<JobStorage>>,
db: Storage) -> Result<impl Reply, Rejection>
{
let mut clients = db.lock().await;
@ -64,6 +64,6 @@ pub async fn listing(db: Storage) -> Result<impl Reply, Rejection> {
result.push(cli.client_info.clone());
}
Ok(warp::reply::json(
&Message::new_owned(result)
&CollectionWrapper(result).as_message()
))
}

@ -46,14 +46,14 @@ async fn main() {
let set_jobs = warp::post()
.and(warp::path(Paths::set_jobs))
.and(warp::path::param::<Uuid>().map(Some))
.and(get_content::<JobStorageWrapper>())
.and(get_content::<CollectionWrapper<JobStorage>>())
.and(db.clone())
.and_then(handlers::set_jobs);
let update_own_jobs = warp::post()
.and(warp::path(Paths::set_jobs))
.and(warp::path::param::<Uuid>().map(Some))
.and(get_content::<JobStorageWrapper>())
.and(get_content::<CollectionWrapper<JobStorage>>())
.and(db.clone())
.and_then(handlers::set_jobs);

@ -104,7 +104,7 @@ impl ClientHandler {
}
build_handler!(POST init(ClientInfo) -> RawMsg);
build_handler!(GET ls() -> Vec<ClientInfo>);
build_handler!(GET ls() -> CollectionWrapper<Vec<ClientInfo>>);
build_handler!(POST del() -> ());
build_handler!(GET get_jobs() -> JobStorageWrapper);
build_handler!(POST set_jobs(JobStorageWrapper) -> ());
build_handler!(GET get_jobs() -> CollectionWrapper<JobStorage>);
build_handler!(POST set_jobs(CollectionWrapper<JobStorage>) -> ());

@ -13,9 +13,31 @@ use {
pub type CliStorage = HashMap<Uuid, UClient>;
pub type JobStorage = HashMap<Uuid, JobMeta>;
// because can't impl From<HashMap<...>> for Cow
// because can't impl From<CollectionWrapper<...>> for Cow
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct JobStorageWrapper(pub JobStorage);
pub struct CollectionWrapper<T>(pub T);
impl<T> From<T> for CollectionWrapper<T> {
fn from(t: T) -> Self {
CollectionWrapper(t)
}
}
impl<T: Clone> ToMsg for CollectionWrapper<T> {}
impl<'cow, T: Clone> From<CollectionWrapper<T>> for Cow<'cow, CollectionWrapper<T>> {
#[inline]
fn from(obj: CollectionWrapper<T>) -> Cow<'cow, CollectionWrapper<T>> {
Cow::Owned(obj)
}
}
impl<'cow, T: Clone> From<&'cow CollectionWrapper<T>> for Cow<'cow, CollectionWrapper<T>> {
#[inline]
fn from(obj: &'cow CollectionWrapper<T>) -> Cow<'cow, CollectionWrapper<T>> {
Cow::Borrowed(obj)
}
}
#[derive(Clone)]

@ -14,10 +14,6 @@ where Self: Clone {
where Cow<'m, Self>: From<&'m Self> {
Message::new(self)
}
fn into_message(self) -> Message<'static, Self> {
Message::new_owned(self)
}
}
#[derive(Serialize, Deserialize, Debug)]
@ -37,14 +33,14 @@ where I: Clone {
}
}
// crutch
/* crutch
// because Vec is stored as &[] (wtf?)
pub fn new_owned(item: I) -> Self {
Self {
id: *UID,
item: Cow::Owned(item)
}
}
}*/
pub fn into_item(self) -> Cow<'cow, I> {
self.item

@ -34,4 +34,4 @@ macro_rules! to_message {
}
}
to_message!(ClientInfo, RawMsg, JobStorageWrapper);
to_message!(ClientInfo, RawMsg);
Loading…
Cancel
Save