//! Auto-generated PID 17134 types.
//! Einrichtung Konfiguration Zuordnung LF von NB
//! Do not edit manually.
#![allow(unused_imports, non_camel_case_types)]

use serde::{Deserialize, Serialize};
use crate::segment::OwnedSegment;
use crate::cursor::{SegmentCursor, SegmentNotFound, peek_is, consume, expect_segment};

/// SG1 — Referenz, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg1 {
    pub rff: Option<OwnedSegment>,
}

/// SG29 — Positionsnummer
/// Qualifiers: Z16
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg29Z16 {
    pub imd: Option<OwnedSegment>,
    pub lin: Option<OwnedSegment>,
    pub pia: Option<OwnedSegment>,
    pub qty: Option<OwnedSegment>,
    pub sg30_z37: Vec<Pid17134Sg30Z37>,
    pub sg34: Vec<Pid17134Sg34>,
}

/// SG29 — Positionsnummer
/// Qualifiers: Z19
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg29Z19 {
    pub lin: Option<OwnedSegment>,
    pub pia: Option<OwnedSegment>,
    pub sg30_z39_z41: Vec<Pid17134Sg30Z39Z41>,
    pub sg34: Vec<Pid17134Sg34>,
}

/// SG29 — Positionsnummer
/// Qualifiers: Z54
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg29Z54 {
    pub lin: Option<OwnedSegment>,
    pub pia: Option<OwnedSegment>,
    pub sg30_z39_z41: Vec<Pid17134Sg30Z39Z41>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: DEB
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg2Deb {
    pub nad: Option<OwnedSegment>,
    pub sg3: Vec<Pid17134Sg3>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: DP
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg2Dp {
    pub loc: Option<OwnedSegment>,
    pub nad: Option<OwnedSegment>,
    pub sg3_z20: Vec<Pid17134Sg3Z20>,
    pub sg3_z59: Vec<Pid17134Sg3Z59>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: MR
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg2Mr {
    pub nad: Option<OwnedSegment>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: MS
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg2Ms {
    pub nad: Option<OwnedSegment>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: SU
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg2Su {
    pub nad: Option<OwnedSegment>,
    pub sg3: Vec<Pid17134Sg3>,
}

/// SG2 — Beteiligter, Qualifier
/// Qualifiers: Z31
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg2Z31 {
    pub nad: Option<OwnedSegment>,
    pub sg3: Vec<Pid17134Sg3>,
}

/// SG3 — Referenz, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg3 {
    pub rff: Option<OwnedSegment>,
}

/// SG30 — Klassentyp, Code
/// Qualifiers: Z37
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg30Z37 {
    pub cci: Option<OwnedSegment>,
}

/// SG30 — Klassentyp, Code
/// Qualifiers: Z39, Z41
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg30Z39Z41 {
    pub cci: Option<OwnedSegment>,
}

/// SG34 — Referenz, Qualifier
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg34 {
    pub rff: Option<OwnedSegment>,
}

/// SG3 — Referenz, Qualifier
/// Qualifiers: Z20
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg3Z20 {
    pub rff: Option<OwnedSegment>,
}

/// SG3 — Referenz, Qualifier
/// Qualifiers: Z59
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134Sg3Z59 {
    pub rff: Option<OwnedSegment>,
}

/// PID 17134: Einrichtung Konfiguration Zuordnung LF von NB
/// Kommunikation: NB an MSB
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Pid17134 {
    pub unh: OwnedSegment,
    pub bgm: OwnedSegment,
    pub dtm: OwnedSegment,
    pub dtm_2: OwnedSegment,
    pub imd: OwnedSegment,
    pub imd_2: OwnedSegment,
    pub uns: OwnedSegment,
    pub unt: OwnedSegment,
    pub sg1: Vec<Pid17134Sg1>,
    pub sg2_ms: Vec<Pid17134Sg2Ms>,
    pub sg2_mr: Vec<Pid17134Sg2Mr>,
    pub sg2_dp: Vec<Pid17134Sg2Dp>,
    pub sg2_su: Vec<Pid17134Sg2Su>,
    pub sg2_z31: Vec<Pid17134Sg2Z31>,
    pub sg2_deb: Vec<Pid17134Sg2Deb>,
    pub sg29_z54: Vec<Pid17134Sg29Z54>,
    pub sg29_z16: Vec<Pid17134Sg29Z16>,
    pub sg29_z19: Vec<Pid17134Sg29Z19>,
}

impl Pid17134Sg1 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17134Sg29Z16 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let imd = if peek_is(segments, cursor, "IMD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let lin = if peek_is(segments, cursor, "LIN") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let pia = if peek_is(segments, cursor, "PIA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let qty = if peek_is(segments, cursor, "QTY") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if imd.is_none() && lin.is_none() && pia.is_none() && qty.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg30_z37 = Vec::new();
        while let Some(item) = Pid17134Sg30Z37::from_segments(segments, cursor) {
            sg30_z37.push(item);
        }
        let mut sg34 = Vec::new();
        while let Some(item) = Pid17134Sg34::from_segments(segments, cursor) {
            sg34.push(item);
        }
        Some(Self {
            imd,
            lin,
            pia,
            qty,
            sg30_z37,
            sg34,
        })
    }
}

impl Pid17134Sg29Z19 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let lin = if peek_is(segments, cursor, "LIN") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let pia = if peek_is(segments, cursor, "PIA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if lin.is_none() && pia.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg30_z39_z41 = Vec::new();
        while let Some(item) = Pid17134Sg30Z39Z41::from_segments(segments, cursor) {
            sg30_z39_z41.push(item);
        }
        let mut sg34 = Vec::new();
        while let Some(item) = Pid17134Sg34::from_segments(segments, cursor) {
            sg34.push(item);
        }
        Some(Self {
            lin,
            pia,
            sg30_z39_z41,
            sg34,
        })
    }
}

impl Pid17134Sg29Z54 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let lin = if peek_is(segments, cursor, "LIN") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let pia = if peek_is(segments, cursor, "PIA") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if lin.is_none() && pia.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg30_z39_z41 = Vec::new();
        while let Some(item) = Pid17134Sg30Z39Z41::from_segments(segments, cursor) {
            sg30_z39_z41.push(item);
        }
        Some(Self {
            lin,
            pia,
            sg30_z39_z41,
        })
    }
}

impl Pid17134Sg2Deb {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg3 = Vec::new();
        while let Some(item) = Pid17134Sg3::from_segments(segments, cursor) {
            sg3.push(item);
        }
        Some(Self {
            nad,
            sg3,
        })
    }
}

impl Pid17134Sg2Dp {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let loc = if peek_is(segments, cursor, "LOC") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if loc.is_none() && nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg3_z20 = Vec::new();
        while let Some(item) = Pid17134Sg3Z20::from_segments(segments, cursor) {
            sg3_z20.push(item);
        }
        let mut sg3_z59 = Vec::new();
        while let Some(item) = Pid17134Sg3Z59::from_segments(segments, cursor) {
            sg3_z59.push(item);
        }
        Some(Self {
            loc,
            nad,
            sg3_z20,
            sg3_z59,
        })
    }
}

impl Pid17134Sg2Mr {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            nad,
        })
    }
}

impl Pid17134Sg2Ms {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            nad,
        })
    }
}

impl Pid17134Sg2Su {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg3 = Vec::new();
        while let Some(item) = Pid17134Sg3::from_segments(segments, cursor) {
            sg3.push(item);
        }
        Some(Self {
            nad,
            sg3,
        })
    }
}

impl Pid17134Sg2Z31 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let nad = if peek_is(segments, cursor, "NAD") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if nad.is_none() {
            cursor.restore(saved);
            return None;
        }
        let mut sg3 = Vec::new();
        while let Some(item) = Pid17134Sg3::from_segments(segments, cursor) {
            sg3.push(item);
        }
        Some(Self {
            nad,
            sg3,
        })
    }
}

impl Pid17134Sg3 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17134Sg30Z37 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let cci = if peek_is(segments, cursor, "CCI") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if cci.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            cci,
        })
    }
}

impl Pid17134Sg30Z39Z41 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let cci = if peek_is(segments, cursor, "CCI") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if cci.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            cci,
        })
    }
}

impl Pid17134Sg34 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17134Sg3Z20 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17134Sg3Z59 {
    /// Try to assemble this group from segments at the cursor position.
    pub fn from_segments(
        segments: &[OwnedSegment],
        cursor: &mut SegmentCursor,
    ) -> Option<Self> {
        let saved = cursor.save();
        let rff = if peek_is(segments, cursor, "RFF") {
            Some(consume(segments, cursor)?.clone())
        } else {
            None
        };
        if rff.is_none() {
            cursor.restore(saved);
            return None;
        }
        Some(Self {
            rff,
        })
    }
}

impl Pid17134 {
    /// Assemble this PID from a pre-tokenized segment list.
    pub fn from_segments(
        segments: &[OwnedSegment],
    ) -> Result<Self, SegmentNotFound> {
        let mut cursor = SegmentCursor::new(segments.len());

        let unh = expect_segment(segments, &mut cursor, "UNH")?.clone();
        let bgm = expect_segment(segments, &mut cursor, "BGM")?.clone();
        let dtm = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let dtm_2 = expect_segment(segments, &mut cursor, "DTM")?.clone();
        let imd = expect_segment(segments, &mut cursor, "IMD")?.clone();
        let imd_2 = expect_segment(segments, &mut cursor, "IMD")?.clone();
        let uns = expect_segment(segments, &mut cursor, "UNS")?.clone();
        let unt = expect_segment(segments, &mut cursor, "UNT")?.clone();
        let mut sg1 = Vec::new();
        while let Some(item) = Pid17134Sg1::from_segments(segments, &mut cursor) {
            sg1.push(item);
        }
        let mut sg2_ms = Vec::new();
        while let Some(item) = Pid17134Sg2Ms::from_segments(segments, &mut cursor) {
            sg2_ms.push(item);
        }
        let mut sg2_mr = Vec::new();
        while let Some(item) = Pid17134Sg2Mr::from_segments(segments, &mut cursor) {
            sg2_mr.push(item);
        }
        let mut sg2_dp = Vec::new();
        while let Some(item) = Pid17134Sg2Dp::from_segments(segments, &mut cursor) {
            sg2_dp.push(item);
        }
        let mut sg2_su = Vec::new();
        while let Some(item) = Pid17134Sg2Su::from_segments(segments, &mut cursor) {
            sg2_su.push(item);
        }
        let mut sg2_z31 = Vec::new();
        while let Some(item) = Pid17134Sg2Z31::from_segments(segments, &mut cursor) {
            sg2_z31.push(item);
        }
        let mut sg2_deb = Vec::new();
        while let Some(item) = Pid17134Sg2Deb::from_segments(segments, &mut cursor) {
            sg2_deb.push(item);
        }
        let mut sg29_z54 = Vec::new();
        while let Some(item) = Pid17134Sg29Z54::from_segments(segments, &mut cursor) {
            sg29_z54.push(item);
        }
        let mut sg29_z16 = Vec::new();
        while let Some(item) = Pid17134Sg29Z16::from_segments(segments, &mut cursor) {
            sg29_z16.push(item);
        }
        let mut sg29_z19 = Vec::new();
        while let Some(item) = Pid17134Sg29Z19::from_segments(segments, &mut cursor) {
            sg29_z19.push(item);
        }

        Ok(Pid17134 {
            unh,
            bgm,
            dtm,
            dtm_2,
            imd,
            imd_2,
            uns,
            unt,
            sg1,
            sg2_ms,
            sg2_mr,
            sg2_dp,
            sg2_su,
            sg2_z31,
            sg2_deb,
            sg29_z54,
            sg29_z16,
            sg29_z19,
        })
    }
}
