gpt4 book ai didi

Serialize an ISO 8601 time/duration "Thhmmss-hhmm/PTmm" ie ("T063000-0600/PT10M") to a rust struct(将ISO 8601时间/持续时间“THMMSS-HMM/PTMM”ie(“T063000-0600/PT10M”)序列化为铁锈结构)

转载 作者:bug小助手 更新时间:2023-10-28 21:14:28 25 4
gpt4 key购买 nike



I'm writing a fairly simple REST api, one of my objects will want to know a time of day and duration to be activated. I'm hoping I can serialize an ISO 8601 Time/Duration into a data members of a struct in rust using actix-web, serde and chrono, is this possible?

我正在编写一个相当简单的REST API,我的一个对象会想知道要激活的时间和持续时间。我希望我可以使用Actix-web、serde和chrono将ISO 8601时间/持续时间序列化为Ruust中结构的数据成员,这可能吗?


I've done some initial research into serde serilization and chrono, but I'm not finding much other than very basic datetime serialization. Not quite what I want to do.

我对serde序列化和时序做了一些初步研究,但除了非常基本的Date Time序列化之外,我没有发现其他什么东西。这不是我想做的事。


更多回答
优秀答案推荐

Anything is possible, of course, but in this case you will have to do some of the lifting yourself or using another crate, because chrono's Duration type doesn't provide formatting or parsing utilities.

当然,任何事情都是可能的,但在这种情况下,您必须自己完成一些操作或使用另一个板条箱,因为chrono的持续时间类型不提供格式化或解析实用程序。


You can implement the time half quite easily. Note that chrono doesn't have a "time with timezone" type, so I will use NaiveTime and assume UTC.

你可以很容易地将时间减半。注意,Timo没有“Time with Time Zone”类型,所以我将使用NAIVETime并假定UTC。


use chrono::{NaiveTime, Duration};
use serde::Serialize;

struct TimeWithDuration {
pub time: NaiveTime,
pub duration: Duration,
}

impl Serialize for TimeWithDuration {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_str(&format!(
"{}/{}",
self.time.format("T%H%M%SZ"),
format_duration(self.duration),
)
}
}

fn format_duration(duration: Duration) -> impl Display {
todo!()
}

format_duration is a placeholder function that should format the duration appropriately. You might consider looking into the iso8601-duration crate.

FORMAT_DURATION是一个占位符函数,它应该适当地格式化持续时间。您可能会考虑查看is8601持续时间板条箱。


更多回答

Me not knowing the difference between the annotation and implementing Serialize... If there are more fields do I need to also implement Serialize for each field ie struct looks like #[derive(Serialize, Deserialize, Debug)] struct MoistureSensor { pub id: Uuid, pub name: String, pub description: String, pub power_pin: i32, pub analog_pin: i32, pub start_time: NaiveTime, pub duration: Duration, }

我不知道注释和实现序列化之间的区别……如果有更多的字段,我还需要为每个字段实现序列化吗?例如,struct看起来像#[派生(Serialize,Disialize,Debug)]struct MoistureSensor{发布ID:uuid,发布名称:字符串,发布描述:字符串,发布电源:I32,发布模拟锁定:I32,发布开始时间:naiveTime,发布持续时间:持续时间,}

@JeremyJones You would combine start_time and duration into a single struct like I have in my answer, assuming that the value is provided in a single string value in the input. For example if you have JSON like { "time_duration": "T063000-0600/PT10M" } then you would have #[derive(Serialize)] struct MoistureSensor { pub time_duration: TimeWithDuration }.

@JeremyJones您将像我在答案中所做的那样将START_TIME和DURATION组合成一个结构,假设该值是在输入的单个字符串值中提供的。例如,如果您有JSON LIKE{“TIME_DUDURE”:“T063000-0600/PT10M”},那么您应该有#[派生(序列化)]结构MoistureSensor{pub TIME_DATION:TimeWithDuration}。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com