1use std::{collections::HashSet, num::ParseIntError, str::FromStr};
5
6use futures::future;
7use linera_base::{
8 crypto::CryptoError,
9 data_types::{TimeDelta, Timestamp},
10 identifiers::{ApplicationId, ChainId, GenericApplicationId},
11 time::Duration,
12};
13use linera_core::{data_types::RoundTimeout, node::NotificationStream, worker::Reason};
14use tokio_stream::StreamExt as _;
15
16pub fn parse_json<T: serde::de::DeserializeOwned>(s: &str) -> anyhow::Result<T> {
18 Ok(serde_json::from_str(s.trim())?)
19}
20
21pub fn parse_millis(s: &str) -> Result<Duration, ParseIntError> {
23 Ok(Duration::from_millis(s.parse()?))
24}
25
26pub fn parse_millis_nonzero(s: &str) -> Result<Duration, anyhow::Error> {
31 let millis = s.parse::<u64>()?;
32 anyhow::ensure!(millis > 0, "must be greater than zero");
33 Ok(Duration::from_millis(millis))
34}
35
36pub fn non_zero_duration(d: Duration) -> Option<Duration> {
38 if d.is_zero() {
39 None
40 } else {
41 Some(d)
42 }
43}
44
45pub fn parse_secs(s: &str) -> Result<Duration, ParseIntError> {
47 Ok(Duration::from_secs(s.parse()?))
48}
49
50pub fn parse_millis_delta(s: &str) -> Result<TimeDelta, ParseIntError> {
52 Ok(TimeDelta::from_millis(s.parse()?))
53}
54
55pub fn parse_json_optional_millis_delta(s: &str) -> anyhow::Result<Option<TimeDelta>> {
57 Ok(parse_json::<Option<u64>>(s)?.map(TimeDelta::from_millis))
58}
59
60pub fn parse_chain_set(s: &str) -> Result<HashSet<ChainId>, CryptoError> {
62 match s.trim() {
63 "" => Ok(HashSet::new()),
64 s => s.split(",").map(ChainId::from_str).collect(),
65 }
66}
67
68pub fn parse_app_set(s: &str) -> anyhow::Result<HashSet<GenericApplicationId>> {
70 s.trim()
71 .split(",")
72 .map(|app_str| {
73 GenericApplicationId::from_str(app_str)
74 .or_else(|_| Ok(ApplicationId::from_str(app_str)?.into()))
75 })
76 .collect()
77}
78
79pub async fn wait_for_next_round(stream: &mut NotificationStream, timeout: RoundTimeout) {
81 let mut stream = stream.filter(|notification| match ¬ification.reason {
82 Reason::NewBlock { height, .. } | Reason::NewEvents { height, .. } => {
83 *height >= timeout.next_block_height
84 }
85 Reason::NewRound { round, .. } => *round > timeout.current_round,
86 Reason::NewIncomingBundle { .. } | Reason::BlockExecuted { .. } => false,
87 });
88 future::select(
89 Box::pin(stream.next()),
90 Box::pin(linera_base::time::timer::sleep(
91 timeout.timestamp.duration_since(Timestamp::now()),
92 )),
93 )
94 .await;
95}
96
97macro_rules! impl_from_infallible {
98 ($target:path) => {
99 impl From<::std::convert::Infallible> for $target {
100 fn from(infallible: ::std::convert::Infallible) -> Self {
101 match infallible {}
102 }
103 }
104 };
105}
106
107pub(crate) use impl_from_infallible;