pub fn new_from_history(h: &History, to_wf_task_num: Option<usize>) -> Result<Self> {
        let events = &h.events;
        if events.is_empty() {
            bail!("History is empty!");
        }

        let is_all_hist = to_wf_task_num.is_none();
        let to_wf_task_num = to_wf_task_num.unwrap_or(usize::MAX);
        let mut workflow_task_started_event_id = 0;
        let mut wf_task_count = 0;
        let mut history = events.iter().peekable();

        let wf_type = match &events.get(0).unwrap().attributes {
            Some(history_event::Attributes::WorkflowExecutionStartedEventAttributes(attrs)) => {
                attrs
                    .workflow_type
                    .as_ref()
                    .ok_or_else(|| {
                        anyhow!("No workflow type defined in execution started attributes")
                    })?
                    .name
                    .clone()
            }
            _ => bail!("First event in history was not workflow execution started!"),
        };

        let mut events = vec![];
        while let Some(event) = history.next() {
            events.push(event.clone());
            let next_event = history.peek();

            if event.event_type == EventType::WorkflowTaskStarted as i32 {
                let next_is_completed = next_event.map_or(false, |ne| {
                    ne.event_type == EventType::WorkflowTaskCompleted as i32
                });
                let next_is_failed_or_timeout = next_event.map_or(false, |ne| {
                    ne.event_type == EventType::WorkflowTaskFailed as i32
                        || ne.event_type == EventType::WorkflowTaskTimedOut as i32
                });

                if next_event.is_none() || next_is_completed {
                    let previous_started_event_id = workflow_task_started_event_id;
                    workflow_task_started_event_id = event.event_id;
                    if workflow_task_started_event_id == previous_started_event_id {
                        bail!(
                            "Latest wf started id {workflow_task_started_event_id} and previous \
                             one {previous_started_event_id} are equal!"
                        );
                    }
                    wf_task_count += 1;
                    if wf_task_count == to_wf_task_num || next_event.is_none() {
                        return Ok(Self {
                            previous_started_event_id,
                            workflow_task_started_event_id,
                            events,
                            wf_task_count,
                            wf_type,
                        });
                    }
                } else if next_event.is_some() && !next_is_failed_or_timeout {
                    bail!(
                        "Invalid history! Event {event:?} should be WFT \
                           completed, failed, or timed out"
                    );
                }
            }

            if next_event.is_none() {
                if event.is_final_wf_execution_event() || is_all_hist {
                    // Since this is the end of execution, we are pretending that the SDK is
                    // replaying *complete* history, which would mean the previously started ID is
                    // in fact the last task.
                    return Ok(Self {
                        previous_started_event_id: workflow_task_started_event_id,
                        workflow_task_started_event_id,
                        events,
                        wf_task_count,
                        wf_type,
                    });
                }
                // No more events
                if workflow_task_started_event_id != event.event_id {
                    bail!("History ends unexpectedly");
                }
            }
        }
        unreachable!()
    }
/// Constructs a new instance, retaining only enough events to reach the provided workflow
/// task number. If not provided, all events are retained.
Constructs a new instance, retaining only enough events to reach the provided workflow
task number. If not provided, all events are retained.
Constructs a new instance retaining only enough events to reach the provided workflow task number . If not provided all events are retained .
pub fn new_from_history(h: &History, to_wf_task_num: Option<usize>) -> Result<Self> {
        let events = &h.events;
        if events.is_empty() {
            bail!("History is empty!");
        }
        let is_all_hist = to_wf_task_num.is_none();
        let to_wf_task_num = to_wf_task_num.unwrap_or(usize::MAX);
        let mut workflow_task_started_event_id = 0;
        let mut wf_task_count = 0;
        let mut history = events.iter().peekable();
        let wf_type = match &events.get(0).unwrap().attributes {
            Some(history_event::Attributes::WorkflowExecutionStartedEventAttributes(attrs)) => {
                attrs
                    .workflow_type
                    .as_ref()
                    .ok_or_else(|| {
                        anyhow!("No workflow type defined in execution started attributes")
                    })?
                    .name
                    .clone()
            }
            _ => bail!("First event in history was not workflow execution started!"),
        };
        let mut events = vec![];
        while let Some(event) = history.next() {
            events.push(event.clone());
            let next_event = history.peek();
            if event.event_type == EventType::WorkflowTaskStarted as i32 {
                let next_is_completed = next_event.map_or(false, |ne| {
                    ne.event_type == EventType::WorkflowTaskCompleted as i32
                });
                let next_is_failed_or_timeout = next_event.map_or(false, |ne| {
                    ne.event_type == EventType::WorkflowTaskFailed as i32
                        || ne.event_type == EventType::WorkflowTaskTimedOut as i32
                });
                if next_event.is_none() || next_is_completed {
                    let previous_started_event_id = workflow_task_started_event_id;
                    workflow_task_started_event_id = event.event_id;
                    if workflow_task_started_event_id == previous_started_event_id {
                        bail!(
                            "Latest wf started id {workflow_task_started_event_id} and previous \
                             one {previous_started_event_id} are equal!"
                        );
                    }
                    wf_task_count += 1;
                    if wf_task_count == to_wf_task_num || next_event.is_none() {
                        return Ok(Self {
                            previous_started_event_id,
                            workflow_task_started_event_id,
                            events,
                            wf_task_count,
                            wf_type,
                        });
                    }
                } else if next_event.is_some() && !next_is_failed_or_timeout {
                    bail!(
                        "Invalid history! Event {event:?} should be WFT \
                           completed, failed, or timed out"
                    );
                }
            }
            if next_event.is_none() {
                if event.is_final_wf_execution_event() || is_all_hist {
                    // Since this is the end of execution, we are pretending that the SDK is
                    // replaying *complete* history, which would mean the previously started ID is
                    // in fact the last task.
                    return Ok(Self {
                        previous_started_event_id: workflow_task_started_event_id,
                        workflow_task_started_event_id,
                        events,
                        wf_task_count,
                        wf_type,
                    });
                }
                // No more events
                if workflow_task_started_event_id != event.event_id {
                    bail!("History ends unexpectedly");
                }
            }
        }
        unreachable!()
    }
pub fn new_from_history ( h : & History , to_wf_task_num : Option < usize > ) -> Result < Self > { let events = & h . events ; if events . is_empty ( ) { bail ! ( " " ) ; } let is_all_hist = to_wf_task_num . is_none ( ) ; let to_wf_task_num = to_wf_task_num . unwrap_or ( usize :: MAX ) ; let mut workflow_task_started_event_id = 0 ; let mut wf_task_count = 0 ; let mut history = events . iter ( ) . peekable ( ) ; let wf_type = match & events . get ( 0 ) . unwrap ( ) . attributes { Some ( history_event :: Attributes :: WorkflowExecutionStartedEventAttributes ( attrs ) ) => { attrs . workflow_type . as_ref ( ) . ok_or_else ( | | { anyhow ! ( " " ) } ) ? . name . clone ( ) } _ => bail ! ( " " ) , } ; let mut events = vec ! [ ] ; while let Some ( event ) = history . next ( ) { events . push ( event . clone ( ) ) ; let next_event = history . peek ( ) ; if event . event_type == EventType :: WorkflowTaskStarted as i32 { let next_is_completed = next_event . map_or ( false , | ne | { ne . event_type == EventType :: WorkflowTaskCompleted as i32 } ) ; let next_is_failed_or_timeout = next_event . map_or ( false , | ne | { ne . event_type == EventType :: WorkflowTaskFailed as i32 || ne . event_type == EventType :: WorkflowTaskTimedOut as i32 } ) ; if next_event . is_none ( ) || next_is_completed { let previous_started_event_id = workflow_task_started_event_id ; workflow_task_started_event_id = event . event_id ; if workflow_task_started_event_id == previous_started_event_id { bail ! ( " \
 " ) ; } wf_task_count += 1 ; if wf_task_count == to_wf_task_num || next_event . is_none ( ) { return Ok ( Self { previous_started_event_id , workflow_task_started_event_id , events , wf_task_count , wf_type , } ) ; } } else if next_event . is_some ( ) && ! next_is_failed_or_timeout { bail ! ( " \
 " ) ; } } if next_event . is_none ( ) { if event . is_final_wf_execution_event ( ) || is_all_hist { // Since this is the end of execution, we are pretending that the SDK is // replaying *complete* history, which would mean the previously started ID is // in fact the last task. return Ok ( Self { previous_started_event_id : workflow_task_started_event_id , workflow_task_started_event_id , events , wf_task_count , wf_type , } ) ; } // No more events if workflow_task_started_event_id != event . event_id { bail ! ( " " ) ; } } } unreachable ! ( ) }
Constructs a new instance, retaining only enough events to reach the provided workflow
task number.
Constructs a new instance retaining only enough events to reach the provided workflow task number .
h
&History
to_wf_task_num
Option<usize>
h
&History
to_wf_task_num
Option<usize>