Skip to content

Commit

Permalink
More common types (#283)
Browse files Browse the repository at this point in the history
* Fragment/UnixTimestamp -> core::common

* clippy

* Timestamp::now -> Timestamp::now_utc
  • Loading branch information
l1h3r authored Jun 11, 2021
1 parent 5812aa5 commit 69c2775
Show file tree
Hide file tree
Showing 20 changed files with 56 additions and 75 deletions.
2 changes: 1 addition & 1 deletion bindings/wasm/src/credential/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl VerifiableCredential {
base.insert("type".into(), types.serde_into().map_err(err)?);

if !base.contains_key("issuanceDate") {
base.insert("issuanceDate".into(), Timestamp::now().to_string().into());
base.insert("issuanceDate".into(), Timestamp::now_utc().to_string().into());
}

base.serde_into().map_err(err).map(Self)
Expand Down
8 changes: 4 additions & 4 deletions examples/low-level-api/diff_chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn main() -> Result<()> {
new.as_document_mut().authentication_mut().append(authentication.into());
}

new.set_updated(Timestamp::now());
new.set_updated(Timestamp::now_utc());
new.set_previous_message_id(*chain.integration_message_id());

chain.current().sign_data(&mut new, keys[0].secret())?;
Expand All @@ -90,7 +90,7 @@ async fn main() -> Result<()> {
let mut this: IotaDocument = chain.current().clone();
this.properties_mut().insert("foo".into(), 123.into());
this.properties_mut().insert("bar".into(), 456.into());
this.set_updated(Timestamp::now());
this.set_updated(Timestamp::now_utc());
this
};

Expand Down Expand Up @@ -129,7 +129,7 @@ async fn main() -> Result<()> {
new.as_document_mut().authentication_mut().append(authentication.into());
}

new.set_updated(Timestamp::now());
new.set_updated(Timestamp::now_utc());
new.set_previous_message_id(*chain.integration_message_id());

new.sign(keypair.secret())?;
Expand All @@ -149,7 +149,7 @@ async fn main() -> Result<()> {
let mut this: IotaDocument = chain.current().clone();
this.properties_mut().insert("baz".into(), 789.into());
this.properties_mut().remove("bar");
this.set_updated(Timestamp::now());
this.set_updated(Timestamp::now_utc());
this
};

Expand Down
2 changes: 1 addition & 1 deletion identity-account/src/account/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use core::fmt::Result as FmtResult;
use futures::executor;
use futures::TryStreamExt;
use hashbrown::HashMap;
use identity_core::common::Fragment;
use identity_core::crypto::KeyType;
use identity_core::crypto::SetSignature;
use identity_did::verification::MethodType;
Expand Down Expand Up @@ -40,7 +41,6 @@ use crate::identity::IdentityState;
use crate::identity::IdentityTag;
use crate::identity::TinyMethod;
use crate::storage::Storage;
use crate::types::Fragment;
use crate::types::Generation;
use crate::types::KeyLocation;
use crate::utils::Shared;
Expand Down
8 changes: 4 additions & 4 deletions identity-account/src/events/command.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity_core::common::Fragment;
use identity_core::common::Object;
use identity_core::common::Url;
use identity_core::crypto::PublicKey;
Expand All @@ -18,7 +19,6 @@ use crate::identity::IdentityState;
use crate::identity::TinyMethod;
use crate::identity::TinyService;
use crate::storage::Storage;
use crate::types::Fragment;
use crate::types::Generation;
use crate::types::KeyLocation;

Expand Down Expand Up @@ -148,7 +148,7 @@ impl Command {

// The fragment must not be an authentication location
ensure!(
!fragment.is_authentication(),
!KeyLocation::is_authentication_fragment(&fragment),
CommandError::InvalidMethodFragment("reserved")
);

Expand All @@ -165,7 +165,7 @@ impl Command {

// The fragment must not be an authentication location
ensure!(
!fragment.is_authentication(),
!KeyLocation::is_authentication_fragment(&fragment),
CommandError::InvalidMethodFragment("reserved")
);

Expand All @@ -182,7 +182,7 @@ impl Command {

// The fragment must not be an authentication location
ensure!(
!fragment.is_authentication(),
!KeyLocation::is_authentication_fragment(&fragment),
CommandError::InvalidMethodFragment("reserved")
);

Expand Down
2 changes: 1 addition & 1 deletion identity-account/src/events/error.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity_core::common::Fragment;
use identity_did::verification::MethodType;

use crate::types::Fragment;
use crate::types::KeyLocation;

/// Errors than may occur while processing a [Command][crate::events::Command].
Expand Down
6 changes: 3 additions & 3 deletions identity-account/src/events/event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

use identity_core::common::Fragment;
use identity_core::common::UnixTimestamp;
use identity_did::verification::MethodScope;
use identity_iota::did::IotaDID;
use identity_iota::tangle::MessageId;
Expand All @@ -10,8 +12,6 @@ use crate::identity::IdentityState;
use crate::identity::TinyMethod;
use crate::identity::TinyMethodRef;
use crate::identity::TinyService;
use crate::types::Fragment;
use crate::types::UnixTimestamp;

/// Event data tagged with a timestamp.
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize)]
Expand All @@ -25,7 +25,7 @@ impl Event {
pub fn new(data: EventData) -> Self {
Self {
data,
time: UnixTimestamp::now(),
time: UnixTimestamp::now_utc(),
}
}

Expand Down
4 changes: 2 additions & 2 deletions identity-account/src/identity/identity_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

use core::convert::TryInto;
use hashbrown::HashMap;
use identity_core::common::Fragment;
use identity_core::common::Object;
use identity_core::common::UnixTimestamp;
use identity_core::common::Url;
use identity_core::crypto::JcsEd25519;
use identity_core::crypto::SetSignature;
Expand Down Expand Up @@ -31,10 +33,8 @@ use crate::error::Error;
use crate::error::Result;
use crate::identity::IdentityId;
use crate::storage::Storage;
use crate::types::Fragment;
use crate::types::Generation;
use crate::types::KeyLocation;
use crate::types::UnixTimestamp;

type Properties = VerifiableProperties<BaseProperties>;
type BaseDocument = CoreDocument<Properties, Object, Object>;
Expand Down
9 changes: 7 additions & 2 deletions identity-account/src/types/key_location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result;
use identity_core::common::Fragment;
use identity_did::verification::MethodType;

use crate::types::Fragment;
use crate::types::Generation;

/// The storage location of a verification method key.
Expand Down Expand Up @@ -56,7 +56,12 @@ impl KeyLocation {

/// Returns true if the key location points to an authentication method.
pub fn is_authentication(&self) -> bool {
self.fragment.is_authentication()
Self::is_authentication_fragment(&self.fragment)
}

/// Returns true if the fragment points to an authentication method.
pub fn is_authentication_fragment(fragment: &Fragment) -> bool {
fragment.name().starts_with(Self::AUTH)
}
}

Expand Down
4 changes: 0 additions & 4 deletions identity-account/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

mod fragment;
mod generation;
mod key_location;
mod signature;
mod unix_timestamp;

pub use self::fragment::*;
pub use self::generation::*;
pub use self::key_location::*;
pub use self::signature::*;
pub use self::unix_timestamp::*;
2 changes: 1 addition & 1 deletion identity-account/tests/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use identity_account::identity::IdentitySnapshot;
use identity_account::identity::TinyMethod;
use identity_account::storage::MemStore;
use identity_account::types::Generation;
use identity_account::types::UnixTimestamp;
use identity_core::common::UnixTimestamp;
use identity_did::verification::MethodType;

async fn new_account() -> Result<Account> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result;

use crate::types::KeyLocation;

/// Represents a DID URL fragment.
#[derive(Clone, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
#[serde(from = "String", into = "String")]
Expand Down Expand Up @@ -36,11 +34,6 @@ impl Fragment {
assert!(!self.0.is_empty());
&self.0[1..]
}

/// Returns true if the fragment points to an authentication method.
pub fn is_authentication(&self) -> bool {
self.name().starts_with(KeyLocation::AUTH)
}
}

impl Debug for Fragment {
Expand Down
4 changes: 4 additions & 0 deletions identity-core/src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
mod bitset;
mod context;
mod fragment;
mod object;
mod one_or_many;
mod timestamp;
mod unix_timestamp;
mod url;

pub use self::bitset::BitSet;
pub use self::context::Context;
pub use self::fragment::Fragment;
pub use self::object::Object;
pub use self::object::Value;
pub use self::one_or_many::OneOrMany;
pub use self::timestamp::Timestamp;
pub use self::unix_timestamp::UnixTimestamp;
pub use self::url::Url;
6 changes: 3 additions & 3 deletions identity-core/src/common/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl Timestamp {
}

/// Creates a new `Timestamp` with the current date and time.
pub fn now() -> Self {
pub fn now_utc() -> Self {
Self(Self::truncate(Utc::now()))
}

Expand Down Expand Up @@ -61,7 +61,7 @@ impl Timestamp {

impl Default for Timestamp {
fn default() -> Self {
Self::now()
Self::now_utc()
}
}

Expand Down Expand Up @@ -155,7 +155,7 @@ mod tests {

#[test]
fn test_json_roundtrip() {
let time1: Timestamp = Timestamp::now();
let time1: Timestamp = Timestamp::now_utc();
let json: Vec<u8> = time1.to_json_vec().unwrap();
let time2: Timestamp = Timestamp::from_json_slice(&json).unwrap();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use core::fmt::Debug;
use core::fmt::Display;
use core::fmt::Formatter;
use core::fmt::Result;
use identity_core::common::Timestamp;

use crate::common::Timestamp;

/// A simple representation of a unix timestamp.
#[derive(Clone, Copy, Hash, PartialEq, Eq, PartialOrd, Ord, Deserialize, Serialize)]
Expand All @@ -14,11 +15,12 @@ use identity_core::common::Timestamp;
pub struct UnixTimestamp(i64);

impl UnixTimestamp {
/// Returns the default timestamp value.
pub const EPOCH: Self = Self(0);

/// Returns the current time as a unix timestamp.
pub fn now() -> Self {
Timestamp::now().into()
pub fn now_utc() -> Self {
Timestamp::now_utc().into()
}

/// Returns true if this time is the unix epoch.
Expand Down Expand Up @@ -64,7 +66,7 @@ mod tests {

#[test]
fn test_roundtrip() {
let time: UnixTimestamp = UnixTimestamp::now();
let time: UnixTimestamp = UnixTimestamp::now_utc();
let core: Timestamp = time.into();

assert_eq!(time, UnixTimestamp::from(core));
Expand Down
25 changes: 6 additions & 19 deletions identity-diff/derive/src/impls/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use quote::quote;
use syn::punctuated::Punctuated;
use syn::token::Comma;
use syn::GenericParam;
use syn::Type;
use syn::WhereClause;

/// derive a Diff type Enum from an incoming `InputModel`.
Expand Down Expand Up @@ -337,10 +336,7 @@ fn parse_evariants(evariants: &[EVariant], diff: &Ident) -> (Vec<TokenStream>, V
}
// Tuple variants.
(SVariant::Tuple, vname, vfields) => {
// setup data.
let field_typs: Vec<&Type> = vfields.iter().map(|f| f.typ()).collect();

let field_max = field_typs.len();
let field_max = vfields.len();
let field_names: Vec<Ident> = (0..field_max).map(|ident| format_ident!("field_{}", ident)).collect();

let buf: Ident = format_ident!("buf");
Expand Down Expand Up @@ -481,12 +477,7 @@ fn parse_merge(
}
// tuple variants.
SVariant::Tuple => {
// get field names based on position.
let ftyps: Vec<&Type> = vfields.iter().map(|f| f.typ()).collect();

let fmax = ftyps.len();

let (left_names, right_names) = populate_field_names(vfields, fmax, struct_type);
let (left_names, right_names) = populate_field_names(vfields, vfields.len(), struct_type);

// setup merge logic.
let merge_fvalues: Vec<TokenStream> = vfields
Expand Down Expand Up @@ -647,11 +638,7 @@ fn parse_diff(
}
// tuple variants.
SVariant::Tuple => {
let ftyps: Vec<&Type> = vfields.iter().map(|f| f.typ()).collect();

let fmax = ftyps.len();

let (left_names, right_names) = populate_field_names(vfields, fmax, struct_type);
let (left_names, right_names) = populate_field_names(vfields, vfields.len(), struct_type);

// setup diff logic.
let diff_fvalues: Vec<TokenStream> = vfields
Expand Down Expand Up @@ -808,9 +795,9 @@ fn parse_from_into(
}
// tuple variants.
SVariant::Tuple => {
let ftyps: Vec<&Type> = vfields.iter().map(|f| f.typ()).collect();
let fmax = ftyps.len();
let fnames: Vec<Ident> = (0..fmax).map(|ident| format_ident!("field_{}", ident)).collect();
let fnames: Vec<Ident> = (0..vfields.len())
.map(|ident| format_ident!("field_{}", ident))
.collect();

// from logic.
let from_fassign: Vec<TokenStream> = var
Expand Down
Loading

0 comments on commit 69c2775

Please sign in to comment.