-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `crate::lsps::message`-module was becoming too big. I've decided to separate all files related to LSPS0. The code should better reflect how the LSP-specification is structured. Follow-up commits for LSPS1 and LSPS2 will follow
- Loading branch information
1 parent
eaa1737
commit 62f0aa6
Showing
5 changed files
with
166 additions
and
143 deletions.
There are no files selected for viewing
This file contains 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,147 @@ | ||
use serde::{Serialize, Deserialize}; | ||
use serde::de::Error as SeError; | ||
use serde::ser::Error as DeError; | ||
|
||
use time::format_description::FormatItem; | ||
use time::macros::format_description; | ||
use time::{OffsetDateTime, PrimitiveDateTime}; | ||
|
||
|
||
// Implements all the common schema's defined in LSPS0 common schema's | ||
|
||
// Initially I used serde_as for the parsing and serialization of this type. | ||
// However, the spec is more strict. | ||
// It requires a yyyy-mm-ddThh:mm:ss.uuuZ format | ||
// | ||
// The serde_as provides us options such as rfc_3339. | ||
// Note, that this also allows formats that are not compliant to the LSP-spec such as dropping | ||
// the fractional seconds or use non UTC timezones. | ||
// | ||
// For LSPS2 the `valid_until`-field must be copied verbatim. As a client this can only be | ||
// achieved if the LSPS2 sends a fully compliant timestamp. | ||
// | ||
// I have decided to fail early if another timestamp is received | ||
#[derive(Debug)] | ||
pub struct IsoDatetime { | ||
pub datetime: PrimitiveDateTime, | ||
} | ||
|
||
const DATETIME_FORMAT: &[FormatItem] = | ||
format_description!("[year]-[month]-[day]T[hour]:[minute]:[second].[subsecond digits:3]Z"); | ||
|
||
impl IsoDatetime { | ||
pub fn from_offset_date_time(datetime: OffsetDateTime) -> Self { | ||
let offset = time::UtcOffset::from_whole_seconds(0).unwrap(); | ||
let datetime_utc = datetime.to_offset(offset); | ||
let primitive = PrimitiveDateTime::new(datetime_utc.date(), datetime.time()); | ||
return Self { | ||
datetime: primitive, | ||
}; | ||
} | ||
|
||
pub fn from_primitive_date_time(datetime: PrimitiveDateTime) -> Self { | ||
return Self { datetime }; | ||
} | ||
|
||
pub fn datetime(&self) -> OffsetDateTime { | ||
return self.datetime.assume_utc(); | ||
} | ||
} | ||
|
||
impl Serialize for IsoDatetime { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
let datetime_str = self | ||
.datetime | ||
.format(&DATETIME_FORMAT) | ||
.map_err(|err| S::Error::custom(format!("Failed to format datetime {:?}", err)))?; | ||
|
||
serializer.serialize_str(&datetime_str) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for IsoDatetime { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let str_repr = <String as serde::de::Deserialize>::deserialize(deserializer)?; | ||
time::PrimitiveDateTime::parse(&str_repr, DATETIME_FORMAT) | ||
.map_err(|err| D::Error::custom(format!("Failed to parse Datetime. {:?}", err))) | ||
.map(|dt| Self::from_primitive_date_time(dt)) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct SatAmount(u64); | ||
#[derive(Debug)] | ||
pub struct MsatAmount(u64); | ||
|
||
impl SatAmount { | ||
pub fn sat_value(&self) -> u64 { | ||
return self.0; | ||
} | ||
|
||
pub fn new(value : u64) -> Self { | ||
return SatAmount(value) | ||
} | ||
} | ||
|
||
impl MsatAmount { | ||
pub fn msat_value(&self) -> u64 { | ||
return self.0; | ||
} | ||
|
||
|
||
pub fn new(value : u64) -> Self { | ||
return MsatAmount(value) | ||
} | ||
} | ||
|
||
impl Serialize for SatAmount { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
let amount_str = self.0.to_string(); | ||
serializer.serialize_str(&amount_str) | ||
} | ||
} | ||
|
||
impl Serialize for MsatAmount { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: serde::Serializer, | ||
{ | ||
let amount_str = self.0.to_string(); | ||
serializer.serialize_str(&amount_str) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for SatAmount { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let str_repr = <String as serde::de::Deserialize>::deserialize(deserializer)?; | ||
let u64_repr: Result<u64, _> = str_repr | ||
.parse() | ||
.map_err(|_| D::Error::custom(String::from("Failed to parse sat_amount"))); | ||
return Ok(Self(u64_repr.unwrap())); | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for MsatAmount { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: serde::Deserializer<'de>, | ||
{ | ||
let str_repr = <String as serde::de::Deserialize>::deserialize(deserializer)?; | ||
let u64_repr: Result<u64, _> = str_repr | ||
.parse() | ||
.map_err(|_| D::Error::custom(String::from("Failed to parse sat_amount"))); | ||
return Ok(Self(u64_repr.unwrap())); | ||
} | ||
} |
This file contains 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,2 @@ | ||
pub mod common_schemas; | ||
pub mod schema; |
This file contains 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,6 @@ | ||
use serde::{Serialize, Deserialize}; | ||
|
||
#[derive(Debug, Serialize, Deserialize)] | ||
pub struct ProtocolList { | ||
pub protocols: Vec<u32>, | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -4,3 +4,4 @@ pub mod json_rpc; | |
pub mod json_rpc_erased; | ||
pub mod message; | ||
pub mod transport; | ||
pub mod lsps0; |