-
Notifications
You must be signed in to change notification settings - Fork 3
implementation of coserv data model #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
06ba3bf
coserv: implementation of coserv data model
DhanusML e4d5011
coserv profile: fix: make untagged
DhanusML 7861874
use cmw-rs from crates.io
DhanusML 4c31366
rename: Query -> CoservQuery
DhanusML 06aefbb
use latest corim-rs
DhanusML 5914e28
Implementation of coserv data model
DhanusML File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| use chrono::{format::SecondsFormat, DateTime, FixedOffset, Local}; | ||
| use corim_rs::generate_tagged; | ||
| use derive_more::From; | ||
| use std::ops::Add; | ||
|
|
||
| use serde::{ | ||
| de::{Deserialize, Deserializer, Error}, | ||
| ser::{Serialize, SerializeMap, Serializer}, | ||
| }; | ||
|
|
||
| #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, serde::Deserialize, serde::Serialize)] | ||
| pub struct Tstr(String); // Time string | ||
|
|
||
| generate_tagged!(( | ||
| 0, | ||
| TaggedTstr, | ||
| Tstr, | ||
| "text string", | ||
| "representation of date/time string using CBOR tag 0" | ||
| )); | ||
|
|
||
| /// Represents timestamps used in coserv query and results | ||
| #[derive(Debug, From, Default, PartialEq, Eq, PartialOrd, Ord, Clone)] | ||
| pub struct TimeStamp(pub DateTime<FixedOffset>); | ||
|
|
||
| /// Re-export to perform arithmetic on TimeStamp. | ||
| pub use chrono::TimeDelta; | ||
|
|
||
| impl TimeStamp { | ||
| pub fn now() -> Self { | ||
| Local::now().fixed_offset().into() | ||
| } | ||
|
|
||
| pub fn add(&self, delta: TimeDelta) -> Self { | ||
| self.0.add(delta).into() | ||
| } | ||
| } | ||
|
|
||
| impl Serialize for TimeStamp { | ||
| fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| S: Serializer, | ||
| { | ||
| // Section 3.3 of [RFC4287] | ||
| let tagged_time = TaggedTstr::from(Tstr(self.0.to_rfc3339_opts(SecondsFormat::Secs, true))); | ||
| tagged_time.serialize(serializer) | ||
| } | ||
| } | ||
|
|
||
| impl<'de> Deserialize<'de> for TimeStamp { | ||
| fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
| where | ||
| D: Deserializer<'de>, | ||
| { | ||
| let time_tstr = TaggedTstr::deserialize(deserializer)?.0 .0 .0; | ||
| Ok(TimeStamp( | ||
| DateTime::parse_from_rfc3339(&time_tstr).map_err(D::Error::custom)?, | ||
| )) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| #[test] | ||
| fn test_time_string() { | ||
| let tests: Vec<(TimeStamp, Vec<u8>)> = vec![ | ||
| ( | ||
| TimeStamp(DateTime::parse_from_rfc3339("2020-09-04T18:34:39+05:30").unwrap()), | ||
| vec![ | ||
| 0xc0, 0x78, 0x19, // tag(0), text(25) | ||
| 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x39, 0x2d, 0x30, 0x34, 0x54, 0x31, 0x38, | ||
| 0x3a, 0x33, 0x34, 0x3a, 0x33, 0x39, 0x2b, 0x30, 0x35, 0x3a, 0x33, 0x30, | ||
| ], | ||
| ), | ||
| ( | ||
| TimeStamp(DateTime::parse_from_rfc3339("2020-09-04T13:04:39+00:00").unwrap()), | ||
| vec![ | ||
| 0xc0, 0x74, // tag(0), text(20) | ||
| 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x39, 0x2d, 0x30, 0x34, 0x54, 0x31, 0x33, | ||
| 0x3a, 0x30, 0x34, 0x3a, 0x33, 0x39, 0x5a, | ||
| ], | ||
| ), | ||
| ( | ||
| TimeStamp(DateTime::parse_from_rfc3339("2020-09-04T13:04:39Z").unwrap()), | ||
| vec![ | ||
| 0xc0, 0x74, // tag(0), text(20) | ||
| 0x32, 0x30, 0x32, 0x30, 0x2d, 0x30, 0x39, 0x2d, 0x30, 0x34, 0x54, 0x31, 0x33, | ||
| 0x3a, 0x30, 0x34, 0x3a, 0x33, 0x39, 0x5a, | ||
| ], | ||
| ), | ||
| ]; | ||
|
|
||
| for (i, (time, expected_cbor)) in tests.iter().enumerate() { | ||
| let mut actual_cbor: Vec<u8> = vec![]; | ||
| ciborium::into_writer(&time, &mut actual_cbor).unwrap(); | ||
| assert_eq!(*expected_cbor, actual_cbor, "ser at index {i}: {time:?}"); | ||
|
|
||
| let time_de: TimeStamp = ciborium::from_reader(actual_cbor.as_slice()).unwrap(); | ||
| assert_eq!(*time, time_de, "de at index {i}: {time:?} != {time_de:?}"); | ||
| } | ||
|
|
||
| let err: Result<TimeStamp, _> = | ||
| ciborium::from_reader([0xc0_u8, 0x63, 0x66, 0x6f, 0x6f].as_slice()); | ||
| assert!(err.is_err()); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_time_add() { | ||
| let base_time = DateTime::parse_from_rfc3339("2020-09-04T00:00:00Z").unwrap(); | ||
| let expected_time = DateTime::parse_from_rfc3339("2020-09-04T01:00:00Z").unwrap(); | ||
| let result = base_time.add(TimeDelta::hours(1)); | ||
| assert_eq!(result, expected_time); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use the version published at https://crates.io/crates/cmw (0.1.0) rather than the git URL.