// @generated by protoc-gen-roto — do not edit
#[allow(unused_imports)]
use roto_runtime::{ProtoAccessor, ProtoBuilder, Result, RotoError, read_varint, RepeatedFieldIterator, RotoMessage};
use core::str;
#[cfg(feature = "alloc")]
use bytes::{Bytes, BytesMut, Buf, BufMut};

pub struct Tool<'a> {
    accessor: roto_runtime::ProtoAccessor<'a>,
    name_offset: Option<usize>,
    version_offset: Option<usize>,
    payload_offset: Option<usize>,
    is_active_offset: Option<usize>,
    exploit_count_offset: Option<usize>,
}

impl<'a> Tool<'a> {
    pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
        let accessor = roto_runtime::ProtoAccessor::new(data)?;
        let mut name_offset = None;
        let mut version_offset = None;
        let mut payload_offset = None;
        let mut is_active_offset = None;
        let mut exploit_count_offset = None;
        for item in accessor.fields() {
            let (offset, tag, _) = item?;
            if tag.field_number == 1 { name_offset = Some(offset); }
            if tag.field_number == 2 { version_offset = Some(offset); }
            if tag.field_number == 3 { payload_offset = Some(offset); }
            if tag.field_number == 4 { is_active_offset = Some(offset); }
            if tag.field_number == 5 { exploit_count_offset = Some(offset); }
        }

        Ok(Self {
            accessor,
name_offset,
version_offset,
payload_offset,
is_active_offset,
exploit_count_offset,
        })
    }

    pub fn name(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.name_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn name_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.name().or(Ok(""))
    }

    pub fn has_name(&self) -> bool { self.name_offset.is_some() }

    pub fn version(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.version_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn version_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.version().or(Ok(""))
    }

    pub fn has_version(&self) -> bool { self.version_offset.is_some() }

    pub fn payload(&self) -> roto_runtime::Result<&'a [u8]> {
        let offset = self.payload_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        Ok(bytes)
    }

    pub fn payload_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
        self.payload().or(Ok(&[]))
    }

    pub fn has_payload(&self) -> bool { self.payload_offset.is_some() }

    pub fn is_active(&self) -> roto_runtime::Result<bool> {
        let offset = self.is_active_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn is_active_or_default(&self) -> roto_runtime::Result<bool> {
        self.is_active().or(Ok(false))
    }

    pub fn has_is_active(&self) -> bool { self.is_active_offset.is_some() }

    pub fn exploit_count(&self) -> roto_runtime::Result<i32> {
        let offset = self.exploit_count_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn exploit_count_or_default(&self) -> roto_runtime::Result<i32> {
        self.exploit_count().or(Ok(0))
    }

    pub fn has_exploit_count(&self) -> bool { self.exploit_count_offset.is_some() }

    pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
        self.accessor.raw_fields()
    }

}

pub struct ToolBuilder<'b> {
    builder: roto_runtime::ProtoBuilder<'b>,
    name_written: bool,
    version_written: bool,
    payload_written: bool,
    is_active_written: bool,
    exploit_count_written: bool,
}

impl<'b> ToolBuilder<'b> {
    pub fn builder(buf: &mut [u8]) -> ToolBuilder<'_> {
        ToolBuilder {
            builder: roto_runtime::ProtoBuilder::new(buf),
            name_written: false,
            version_written: false,
            payload_written: false,
            is_active_written: false,
            exploit_count_written: false,
        }
    }

    pub fn name(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(1, value)?;
        self.name_written = true;
        Ok(self)
    }

    pub fn version(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(2, value)?;
        self.version_written = true;
        Ok(self)
    }

    pub fn payload(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(3, value)?;
        self.payload_written = true;
        Ok(self)
    }

    pub fn is_active(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(4, value)?;
        self.is_active_written = true;
        Ok(self)
    }

    pub fn exploit_count(mut self, value: i32) -> roto_runtime::Result<Self> {
        self.builder.write_int32(5, value)?;
        self.exploit_count_written = true;
        Ok(self)
    }

    pub fn with(mut self, msg: &Tool<'_>) -> roto_runtime::Result<Self> {
        for item in msg.accessor.raw_fields() {
            let (field_number, raw_bytes) = item?;
            let is_written = match field_number {
                1 => self.name_written,
                2 => self.version_written,
                3 => self.payload_written,
                4 => self.is_active_written,
                5 => self.exploit_count_written,
                _ => false,
            };
            if !is_written {
                self.builder.write_raw(raw_bytes)?;
            }
        }
        Ok(self)
    }

    pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
        self.builder.finish()
    }
}

#[cfg(feature = "alloc")]
pub struct OwnedTool {
    pub data: bytes::Bytes,
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoOwned for OwnedTool {
    type Reader<'a> = Tool<'a>;
    fn reader(&self) -> Tool<'_> {
        Tool::new(&self.data).expect("failed to create reader")
    }
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoMessage for OwnedTool {
    fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
        Ok(OwnedTool { data: buf })
    }

    fn bytes(&self) -> bytes::Bytes {
        self.data.clone()
    }
}

pub struct Connection<'a> {
    accessor: roto_runtime::ProtoAccessor<'a>,
    host_offset: Option<usize>,
    port_offset: Option<usize>,
    encrypted_offset: Option<usize>,
    bandwidth_bps_offset: Option<usize>,
    session_key_offset: Option<usize>,
}

impl<'a> Connection<'a> {
    pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
        let accessor = roto_runtime::ProtoAccessor::new(data)?;
        let mut host_offset = None;
        let mut port_offset = None;
        let mut encrypted_offset = None;
        let mut bandwidth_bps_offset = None;
        let mut session_key_offset = None;
        for item in accessor.fields() {
            let (offset, tag, _) = item?;
            if tag.field_number == 1 { host_offset = Some(offset); }
            if tag.field_number == 2 { port_offset = Some(offset); }
            if tag.field_number == 3 { encrypted_offset = Some(offset); }
            if tag.field_number == 4 { bandwidth_bps_offset = Some(offset); }
            if tag.field_number == 5 { session_key_offset = Some(offset); }
        }

        Ok(Self {
            accessor,
host_offset,
port_offset,
encrypted_offset,
bandwidth_bps_offset,
session_key_offset,
        })
    }

    pub fn host(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.host_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn host_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.host().or(Ok(""))
    }

    pub fn has_host(&self) -> bool { self.host_offset.is_some() }

    pub fn port(&self) -> roto_runtime::Result<i32> {
        let offset = self.port_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn port_or_default(&self) -> roto_runtime::Result<i32> {
        self.port().or(Ok(0))
    }

    pub fn has_port(&self) -> bool { self.port_offset.is_some() }

    pub fn encrypted(&self) -> roto_runtime::Result<bool> {
        let offset = self.encrypted_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn encrypted_or_default(&self) -> roto_runtime::Result<bool> {
        self.encrypted().or(Ok(false))
    }

    pub fn has_encrypted(&self) -> bool { self.encrypted_offset.is_some() }

    pub fn bandwidth_bps(&self) -> roto_runtime::Result<i32> {
        let offset = self.bandwidth_bps_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn bandwidth_bps_or_default(&self) -> roto_runtime::Result<i32> {
        self.bandwidth_bps().or(Ok(0))
    }

    pub fn has_bandwidth_bps(&self) -> bool { self.bandwidth_bps_offset.is_some() }

    pub fn session_key(&self) -> roto_runtime::Result<&'a [u8]> {
        let offset = self.session_key_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        Ok(bytes)
    }

    pub fn session_key_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
        self.session_key().or(Ok(&[]))
    }

    pub fn has_session_key(&self) -> bool { self.session_key_offset.is_some() }

    pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
        self.accessor.raw_fields()
    }

}

pub struct ConnectionBuilder<'b> {
    builder: roto_runtime::ProtoBuilder<'b>,
    host_written: bool,
    port_written: bool,
    encrypted_written: bool,
    bandwidth_bps_written: bool,
    session_key_written: bool,
}

impl<'b> ConnectionBuilder<'b> {
    pub fn builder(buf: &mut [u8]) -> ConnectionBuilder<'_> {
        ConnectionBuilder {
            builder: roto_runtime::ProtoBuilder::new(buf),
            host_written: false,
            port_written: false,
            encrypted_written: false,
            bandwidth_bps_written: false,
            session_key_written: false,
        }
    }

    pub fn host(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(1, value)?;
        self.host_written = true;
        Ok(self)
    }

    pub fn port(mut self, value: i32) -> roto_runtime::Result<Self> {
        self.builder.write_int32(2, value)?;
        self.port_written = true;
        Ok(self)
    }

    pub fn encrypted(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(3, value)?;
        self.encrypted_written = true;
        Ok(self)
    }

    pub fn bandwidth_bps(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(4, value)?;
        self.bandwidth_bps_written = true;
        Ok(self)
    }

    pub fn session_key(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(5, value)?;
        self.session_key_written = true;
        Ok(self)
    }

    pub fn with(mut self, msg: &Connection<'_>) -> roto_runtime::Result<Self> {
        for item in msg.accessor.raw_fields() {
            let (field_number, raw_bytes) = item?;
            let is_written = match field_number {
                1 => self.host_written,
                2 => self.port_written,
                3 => self.encrypted_written,
                4 => self.bandwidth_bps_written,
                5 => self.session_key_written,
                _ => false,
            };
            if !is_written {
                self.builder.write_raw(raw_bytes)?;
            }
        }
        Ok(self)
    }

    pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
        self.builder.finish()
    }
}

#[cfg(feature = "alloc")]
pub struct OwnedConnection {
    pub data: bytes::Bytes,
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoOwned for OwnedConnection {
    type Reader<'a> = Connection<'a>;
    fn reader(&self) -> Connection<'_> {
        Connection::new(&self.data).expect("failed to create reader")
    }
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoMessage for OwnedConnection {
    fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
        Ok(OwnedConnection { data: buf })
    }

    fn bytes(&self) -> bytes::Bytes {
        self.data.clone()
    }
}

pub struct Hacker<'a> {
    accessor: roto_runtime::ProtoAccessor<'a>,
    handle_offset: Option<usize>,
    real_name_offset: Option<usize>,
    age_offset: Option<usize>,
    skill_level_offset: Option<usize>,
    is_elite_offset: Option<usize>,
    crew_id_offset: Option<usize>,
    exploits_start: Option<usize>,
    exploits_end: Option<usize>,
    tools_start: Option<usize>,
    tools_end: Option<usize>,
    active_connection_offset: Option<usize>,
}

impl<'a> Hacker<'a> {
    pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
        let accessor = roto_runtime::ProtoAccessor::new(data)?;
        let mut handle_offset = None;
        let mut real_name_offset = None;
        let mut age_offset = None;
        let mut skill_level_offset = None;
        let mut is_elite_offset = None;
        let mut crew_id_offset = None;
        let mut exploits_start = None;
        let mut exploits_end = None;
        let mut tools_start = None;
        let mut tools_end = None;
        let mut active_connection_offset = None;
        for item in accessor.fields() {
            let (offset, tag, _) = item?;
            if tag.field_number == 1 { handle_offset = Some(offset); }
            if tag.field_number == 2 { real_name_offset = Some(offset); }
            if tag.field_number == 3 { age_offset = Some(offset); }
            if tag.field_number == 4 { skill_level_offset = Some(offset); }
            if tag.field_number == 5 { is_elite_offset = Some(offset); }
            if tag.field_number == 6 { crew_id_offset = Some(offset); }
            if tag.field_number == 7 {
                if exploits_start.is_none() { exploits_start = Some(offset); }
                exploits_end = Some(offset);
            }
            if tag.field_number == 8 {
                if tools_start.is_none() { tools_start = Some(offset); }
                tools_end = Some(offset);
            }
            if tag.field_number == 9 { active_connection_offset = Some(offset); }
        }

        Ok(Self {
            accessor,
handle_offset,
real_name_offset,
age_offset,
skill_level_offset,
is_elite_offset,
crew_id_offset,
exploits_start, exploits_end,
tools_start, tools_end,
active_connection_offset,
        })
    }

    pub fn handle(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.handle_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn handle_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.handle().or(Ok(""))
    }

    pub fn has_handle(&self) -> bool { self.handle_offset.is_some() }

    pub fn real_name(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.real_name_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn real_name_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.real_name().or(Ok(""))
    }

    pub fn has_real_name(&self) -> bool { self.real_name_offset.is_some() }

    pub fn age(&self) -> roto_runtime::Result<i32> {
        let offset = self.age_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn age_or_default(&self) -> roto_runtime::Result<i32> {
        self.age().or(Ok(0))
    }

    pub fn has_age(&self) -> bool { self.age_offset.is_some() }

    pub fn skill_level(&self) -> roto_runtime::Result<f32> {
        let offset = self.skill_level_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        Ok(f32::from_le_bytes(bytes.try_into().map_err(|_| roto_runtime::RotoError::WireFormatViolation)?))
    }

    pub fn skill_level_or_default(&self) -> roto_runtime::Result<f32> {
        self.skill_level().or(Ok(0.0))
    }

    pub fn has_skill_level(&self) -> bool { self.skill_level_offset.is_some() }

    pub fn is_elite(&self) -> roto_runtime::Result<bool> {
        let offset = self.is_elite_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn is_elite_or_default(&self) -> roto_runtime::Result<bool> {
        self.is_elite().or(Ok(false))
    }

    pub fn has_is_elite(&self) -> bool { self.is_elite_offset.is_some() }

    pub fn crew_id(&self) -> roto_runtime::Result<i32> {
        let offset = self.crew_id_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn crew_id_or_default(&self) -> roto_runtime::Result<i32> {
        self.crew_id().or(Ok(0))
    }

    pub fn has_crew_id(&self) -> bool { self.crew_id_offset.is_some() }

    pub fn exploits(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
        match (self.exploits_start, self.exploits_end) {
            (Some(start), Some(end)) => self.accessor.iter_repeated_range(7, start, end),
            _ => self.accessor.iter_repeated(7),
        }
    }

    pub fn tools(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
        match (self.tools_start, self.tools_end) {
            (Some(start), Some(end)) => self.accessor.iter_repeated_range(8, start, end),
            _ => self.accessor.iter_repeated(8),
        }
    }

    pub fn active_connection(&self) -> roto_runtime::Result<&'a [u8]> {
        let offset = self.active_connection_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        Ok(bytes)
    }

    pub fn active_connection_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
        self.active_connection().or(Ok(&[]))
    }

    pub fn has_active_connection(&self) -> bool { self.active_connection_offset.is_some() }

    pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
        self.accessor.raw_fields()
    }

}

pub struct HackerBuilder<'b> {
    builder: roto_runtime::ProtoBuilder<'b>,
    handle_written: bool,
    real_name_written: bool,
    age_written: bool,
    skill_level_written: bool,
    is_elite_written: bool,
    crew_id_written: bool,
    exploits_written: bool,
    tools_written: bool,
    active_connection_written: bool,
}

impl<'b> HackerBuilder<'b> {
    pub fn builder(buf: &mut [u8]) -> HackerBuilder<'_> {
        HackerBuilder {
            builder: roto_runtime::ProtoBuilder::new(buf),
            handle_written: false,
            real_name_written: false,
            age_written: false,
            skill_level_written: false,
            is_elite_written: false,
            crew_id_written: false,
            exploits_written: false,
            tools_written: false,
            active_connection_written: false,
        }
    }

    pub fn handle(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(1, value)?;
        self.handle_written = true;
        Ok(self)
    }

    pub fn real_name(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(2, value)?;
        self.real_name_written = true;
        Ok(self)
    }

    pub fn age(mut self, value: i32) -> roto_runtime::Result<Self> {
        self.builder.write_int32(3, value)?;
        self.age_written = true;
        Ok(self)
    }

    pub fn skill_level(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(4, value)?;
        self.skill_level_written = true;
        Ok(self)
    }

    pub fn is_elite(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(5, value)?;
        self.is_elite_written = true;
        Ok(self)
    }

    pub fn crew_id(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(6, value)?;
        self.crew_id_written = true;
        Ok(self)
    }

    pub fn exploits(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(7, value)?;
        self.exploits_written = true;
        Ok(self)
    }

    pub fn tools(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(8, value)?;
        self.tools_written = true;
        Ok(self)
    }

    pub fn active_connection(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(9, value)?;
        self.active_connection_written = true;
        Ok(self)
    }

    pub fn with(mut self, msg: &Hacker<'_>) -> roto_runtime::Result<Self> {
        for item in msg.accessor.raw_fields() {
            let (field_number, raw_bytes) = item?;
            let is_written = match field_number {
                1 => self.handle_written,
                2 => self.real_name_written,
                3 => self.age_written,
                4 => self.skill_level_written,
                5 => self.is_elite_written,
                6 => self.crew_id_written,
                7 => self.exploits_written,
                8 => self.tools_written,
                9 => self.active_connection_written,
                _ => false,
            };
            if !is_written {
                self.builder.write_raw(raw_bytes)?;
            }
        }
        Ok(self)
    }

    pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
        self.builder.finish()
    }
}

#[cfg(feature = "alloc")]
pub struct OwnedHacker {
    pub data: bytes::Bytes,
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoOwned for OwnedHacker {
    type Reader<'a> = Hacker<'a>;
    fn reader(&self) -> Hacker<'_> {
        Hacker::new(&self.data).expect("failed to create reader")
    }
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoMessage for OwnedHacker {
    fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
        Ok(OwnedHacker { data: buf })
    }

    fn bytes(&self) -> bytes::Bytes {
        self.data.clone()
    }
}

pub struct Worm<'a> {
    accessor: roto_runtime::ProtoAccessor<'a>,
    name_offset: Option<usize>,
    variant_offset: Option<usize>,
    size_bytes_offset: Option<usize>,
    payload_offset: Option<usize>,
    polymorphic_offset: Option<usize>,
    targets_start: Option<usize>,
    targets_end: Option<usize>,
}

impl<'a> Worm<'a> {
    pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
        let accessor = roto_runtime::ProtoAccessor::new(data)?;
        let mut name_offset = None;
        let mut variant_offset = None;
        let mut size_bytes_offset = None;
        let mut payload_offset = None;
        let mut polymorphic_offset = None;
        let mut targets_start = None;
        let mut targets_end = None;
        for item in accessor.fields() {
            let (offset, tag, _) = item?;
            if tag.field_number == 1 { name_offset = Some(offset); }
            if tag.field_number == 2 { variant_offset = Some(offset); }
            if tag.field_number == 3 { size_bytes_offset = Some(offset); }
            if tag.field_number == 4 { payload_offset = Some(offset); }
            if tag.field_number == 5 { polymorphic_offset = Some(offset); }
            if tag.field_number == 6 {
                if targets_start.is_none() { targets_start = Some(offset); }
                targets_end = Some(offset);
            }
        }

        Ok(Self {
            accessor,
name_offset,
variant_offset,
size_bytes_offset,
payload_offset,
polymorphic_offset,
targets_start, targets_end,
        })
    }

    pub fn name(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.name_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn name_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.name().or(Ok(""))
    }

    pub fn has_name(&self) -> bool { self.name_offset.is_some() }

    pub fn variant(&self) -> roto_runtime::Result<i32> {
        let offset = self.variant_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn variant_or_default(&self) -> roto_runtime::Result<i32> {
        self.variant().or(Ok(0))
    }

    pub fn has_variant(&self) -> bool { self.variant_offset.is_some() }

    pub fn size_bytes(&self) -> roto_runtime::Result<i32> {
        let offset = self.size_bytes_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn size_bytes_or_default(&self) -> roto_runtime::Result<i32> {
        self.size_bytes().or(Ok(0))
    }

    pub fn has_size_bytes(&self) -> bool { self.size_bytes_offset.is_some() }

    pub fn payload(&self) -> roto_runtime::Result<&'a [u8]> {
        let offset = self.payload_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        Ok(bytes)
    }

    pub fn payload_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
        self.payload().or(Ok(&[]))
    }

    pub fn has_payload(&self) -> bool { self.payload_offset.is_some() }

    pub fn polymorphic(&self) -> roto_runtime::Result<bool> {
        let offset = self.polymorphic_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn polymorphic_or_default(&self) -> roto_runtime::Result<bool> {
        self.polymorphic().or(Ok(false))
    }

    pub fn has_polymorphic(&self) -> bool { self.polymorphic_offset.is_some() }

    pub fn targets(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
        match (self.targets_start, self.targets_end) {
            (Some(start), Some(end)) => self.accessor.iter_repeated_range(6, start, end),
            _ => self.accessor.iter_repeated(6),
        }
    }

    pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
        self.accessor.raw_fields()
    }

}

pub struct WormBuilder<'b> {
    builder: roto_runtime::ProtoBuilder<'b>,
    name_written: bool,
    variant_written: bool,
    size_bytes_written: bool,
    payload_written: bool,
    polymorphic_written: bool,
    targets_written: bool,
}

impl<'b> WormBuilder<'b> {
    pub fn builder(buf: &mut [u8]) -> WormBuilder<'_> {
        WormBuilder {
            builder: roto_runtime::ProtoBuilder::new(buf),
            name_written: false,
            variant_written: false,
            size_bytes_written: false,
            payload_written: false,
            polymorphic_written: false,
            targets_written: false,
        }
    }

    pub fn name(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(1, value)?;
        self.name_written = true;
        Ok(self)
    }

    pub fn variant(mut self, value: i32) -> roto_runtime::Result<Self> {
        self.builder.write_int32(2, value)?;
        self.variant_written = true;
        Ok(self)
    }

    pub fn size_bytes(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(3, value)?;
        self.size_bytes_written = true;
        Ok(self)
    }

    pub fn payload(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(4, value)?;
        self.payload_written = true;
        Ok(self)
    }

    pub fn polymorphic(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(5, value)?;
        self.polymorphic_written = true;
        Ok(self)
    }

    pub fn targets(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(6, value)?;
        self.targets_written = true;
        Ok(self)
    }

    pub fn with(mut self, msg: &Worm<'_>) -> roto_runtime::Result<Self> {
        for item in msg.accessor.raw_fields() {
            let (field_number, raw_bytes) = item?;
            let is_written = match field_number {
                1 => self.name_written,
                2 => self.variant_written,
                3 => self.size_bytes_written,
                4 => self.payload_written,
                5 => self.polymorphic_written,
                6 => self.targets_written,
                _ => false,
            };
            if !is_written {
                self.builder.write_raw(raw_bytes)?;
            }
        }
        Ok(self)
    }

    pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
        self.builder.finish()
    }
}

#[cfg(feature = "alloc")]
pub struct OwnedWorm {
    pub data: bytes::Bytes,
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoOwned for OwnedWorm {
    type Reader<'a> = Worm<'a>;
    fn reader(&self) -> Worm<'_> {
        Worm::new(&self.data).expect("failed to create reader")
    }
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoMessage for OwnedWorm {
    fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
        Ok(OwnedWorm { data: buf })
    }

    fn bytes(&self) -> bytes::Bytes {
        self.data.clone()
    }
}

pub struct Operation<'a> {
    accessor: roto_runtime::ProtoAccessor<'a>,
    codename_offset: Option<usize>,
    target_corp_offset: Option<usize>,
    timestamp_offset: Option<usize>,
    successful_offset: Option<usize>,
    stolen_data_offset: Option<usize>,
    crew_start: Option<usize>,
    crew_end: Option<usize>,
    worm_offset: Option<usize>,
    log_entries_start: Option<usize>,
    log_entries_end: Option<usize>,
    severity_offset: Option<usize>,
}

impl<'a> Operation<'a> {
    pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
        let accessor = roto_runtime::ProtoAccessor::new(data)?;
        let mut codename_offset = None;
        let mut target_corp_offset = None;
        let mut timestamp_offset = None;
        let mut successful_offset = None;
        let mut stolen_data_offset = None;
        let mut crew_start = None;
        let mut crew_end = None;
        let mut worm_offset = None;
        let mut log_entries_start = None;
        let mut log_entries_end = None;
        let mut severity_offset = None;
        for item in accessor.fields() {
            let (offset, tag, _) = item?;
            if tag.field_number == 1 { codename_offset = Some(offset); }
            if tag.field_number == 2 { target_corp_offset = Some(offset); }
            if tag.field_number == 3 { timestamp_offset = Some(offset); }
            if tag.field_number == 4 { successful_offset = Some(offset); }
            if tag.field_number == 5 { stolen_data_offset = Some(offset); }
            if tag.field_number == 6 {
                if crew_start.is_none() { crew_start = Some(offset); }
                crew_end = Some(offset);
            }
            if tag.field_number == 7 { worm_offset = Some(offset); }
            if tag.field_number == 8 {
                if log_entries_start.is_none() { log_entries_start = Some(offset); }
                log_entries_end = Some(offset);
            }
            if tag.field_number == 9 { severity_offset = Some(offset); }
        }

        Ok(Self {
            accessor,
codename_offset,
target_corp_offset,
timestamp_offset,
successful_offset,
stolen_data_offset,
crew_start, crew_end,
worm_offset,
log_entries_start, log_entries_end,
severity_offset,
        })
    }

    pub fn codename(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.codename_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn codename_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.codename().or(Ok(""))
    }

    pub fn has_codename(&self) -> bool { self.codename_offset.is_some() }

    pub fn target_corp(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.target_corp_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn target_corp_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.target_corp().or(Ok(""))
    }

    pub fn has_target_corp(&self) -> bool { self.target_corp_offset.is_some() }

    pub fn timestamp(&self) -> roto_runtime::Result<i32> {
        let offset = self.timestamp_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn timestamp_or_default(&self) -> roto_runtime::Result<i32> {
        self.timestamp().or(Ok(0))
    }

    pub fn has_timestamp(&self) -> bool { self.timestamp_offset.is_some() }

    pub fn successful(&self) -> roto_runtime::Result<bool> {
        let offset = self.successful_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v != 0).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn successful_or_default(&self) -> roto_runtime::Result<bool> {
        self.successful().or(Ok(false))
    }

    pub fn has_successful(&self) -> bool { self.successful_offset.is_some() }

    pub fn stolen_data(&self) -> roto_runtime::Result<&'a [u8]> {
        let offset = self.stolen_data_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        Ok(bytes)
    }

    pub fn stolen_data_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
        self.stolen_data().or(Ok(&[]))
    }

    pub fn has_stolen_data(&self) -> bool { self.stolen_data_offset.is_some() }

    pub fn crew(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
        match (self.crew_start, self.crew_end) {
            (Some(start), Some(end)) => self.accessor.iter_repeated_range(6, start, end),
            _ => self.accessor.iter_repeated(6),
        }
    }

    pub fn worm(&self) -> roto_runtime::Result<&'a [u8]> {
        let offset = self.worm_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        Ok(bytes)
    }

    pub fn worm_or_default(&self) -> roto_runtime::Result<&'a [u8]> {
        self.worm().or(Ok(&[]))
    }

    pub fn has_worm(&self) -> bool { self.worm_offset.is_some() }

    pub fn log_entries(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
        match (self.log_entries_start, self.log_entries_end) {
            (Some(start), Some(end)) => self.accessor.iter_repeated_range(8, start, end),
            _ => self.accessor.iter_repeated(8),
        }
    }

    pub fn severity(&self) -> roto_runtime::Result<i32> {
        let offset = self.severity_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn severity_or_default(&self) -> roto_runtime::Result<i32> {
        self.severity().or(Ok(0))
    }

    pub fn has_severity(&self) -> bool { self.severity_offset.is_some() }

    pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
        self.accessor.raw_fields()
    }

}

pub struct OperationBuilder<'b> {
    builder: roto_runtime::ProtoBuilder<'b>,
    codename_written: bool,
    target_corp_written: bool,
    timestamp_written: bool,
    successful_written: bool,
    stolen_data_written: bool,
    crew_written: bool,
    worm_written: bool,
    log_entries_written: bool,
    severity_written: bool,
}

impl<'b> OperationBuilder<'b> {
    pub fn builder(buf: &mut [u8]) -> OperationBuilder<'_> {
        OperationBuilder {
            builder: roto_runtime::ProtoBuilder::new(buf),
            codename_written: false,
            target_corp_written: false,
            timestamp_written: false,
            successful_written: false,
            stolen_data_written: false,
            crew_written: false,
            worm_written: false,
            log_entries_written: false,
            severity_written: false,
        }
    }

    pub fn codename(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(1, value)?;
        self.codename_written = true;
        Ok(self)
    }

    pub fn target_corp(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(2, value)?;
        self.target_corp_written = true;
        Ok(self)
    }

    pub fn timestamp(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(3, value)?;
        self.timestamp_written = true;
        Ok(self)
    }

    pub fn successful(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(4, value)?;
        self.successful_written = true;
        Ok(self)
    }

    pub fn stolen_data(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(5, value)?;
        self.stolen_data_written = true;
        Ok(self)
    }

    pub fn crew(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(6, value)?;
        self.crew_written = true;
        Ok(self)
    }

    pub fn worm(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(7, value)?;
        self.worm_written = true;
        Ok(self)
    }

    pub fn log_entries(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(8, value)?;
        self.log_entries_written = true;
        Ok(self)
    }

    pub fn severity(mut self, value: i32) -> roto_runtime::Result<Self> {
        self.builder.write_int32(9, value)?;
        self.severity_written = true;
        Ok(self)
    }

    pub fn with(mut self, msg: &Operation<'_>) -> roto_runtime::Result<Self> {
        for item in msg.accessor.raw_fields() {
            let (field_number, raw_bytes) = item?;
            let is_written = match field_number {
                1 => self.codename_written,
                2 => self.target_corp_written,
                3 => self.timestamp_written,
                4 => self.successful_written,
                5 => self.stolen_data_written,
                6 => self.crew_written,
                7 => self.worm_written,
                8 => self.log_entries_written,
                9 => self.severity_written,
                _ => false,
            };
            if !is_written {
                self.builder.write_raw(raw_bytes)?;
            }
        }
        Ok(self)
    }

    pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
        self.builder.finish()
    }
}

#[cfg(feature = "alloc")]
pub struct OwnedOperation {
    pub data: bytes::Bytes,
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoOwned for OwnedOperation {
    type Reader<'a> = Operation<'a>;
    fn reader(&self) -> Operation<'_> {
        Operation::new(&self.data).expect("failed to create reader")
    }
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoMessage for OwnedOperation {
    fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
        Ok(OwnedOperation { data: buf })
    }

    fn bytes(&self) -> bytes::Bytes {
        self.data.clone()
    }
}

pub struct Campaign<'a> {
    accessor: roto_runtime::ProtoAccessor<'a>,
    name_offset: Option<usize>,
    operations_start: Option<usize>,
    operations_end: Option<usize>,
    total_bytes_stolen_offset: Option<usize>,
}

impl<'a> Campaign<'a> {
    pub fn new(data: &'a [u8]) -> roto_runtime::Result<Self> {
        let accessor = roto_runtime::ProtoAccessor::new(data)?;
        let mut name_offset = None;
        let mut operations_start = None;
        let mut operations_end = None;
        let mut total_bytes_stolen_offset = None;
        for item in accessor.fields() {
            let (offset, tag, _) = item?;
            if tag.field_number == 1 { name_offset = Some(offset); }
            if tag.field_number == 2 {
                if operations_start.is_none() { operations_start = Some(offset); }
                operations_end = Some(offset);
            }
            if tag.field_number == 3 { total_bytes_stolen_offset = Some(offset); }
        }

        Ok(Self {
            accessor,
name_offset,
operations_start, operations_end,
total_bytes_stolen_offset,
        })
    }

    pub fn name(&self) -> roto_runtime::Result<&'a str> {
        let offset = self.name_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        core::str::from_utf8(bytes).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn name_or_default(&self) -> roto_runtime::Result<&'a str> {
        self.name().or(Ok(""))
    }

    pub fn has_name(&self) -> bool { self.name_offset.is_some() }

    pub fn operations(&self) -> roto_runtime::RepeatedFieldIterator<'a> {
        match (self.operations_start, self.operations_end) {
            (Some(start), Some(end)) => self.accessor.iter_repeated_range(2, start, end),
            _ => self.accessor.iter_repeated(2),
        }
    }

    pub fn total_bytes_stolen(&self) -> roto_runtime::Result<i32> {
        let offset = self.total_bytes_stolen_offset.ok_or(roto_runtime::RotoError::FieldNotFound)?;
        let (bytes, _) = self.accessor.get_value_at(offset)?;
        roto_runtime::read_varint(bytes).map(|(v, _)| v as i32).map_err(|_| roto_runtime::RotoError::WireFormatViolation)
    }

    pub fn total_bytes_stolen_or_default(&self) -> roto_runtime::Result<i32> {
        self.total_bytes_stolen().or(Ok(0))
    }

    pub fn has_total_bytes_stolen(&self) -> bool { self.total_bytes_stolen_offset.is_some() }

    pub fn raw_fields(&self) -> roto_runtime::RawFieldIterator<'a> {
        self.accessor.raw_fields()
    }

}

pub struct CampaignBuilder<'b> {
    builder: roto_runtime::ProtoBuilder<'b>,
    name_written: bool,
    operations_written: bool,
    total_bytes_stolen_written: bool,
}

impl<'b> CampaignBuilder<'b> {
    pub fn builder(buf: &mut [u8]) -> CampaignBuilder<'_> {
        CampaignBuilder {
            builder: roto_runtime::ProtoBuilder::new(buf),
            name_written: false,
            operations_written: false,
            total_bytes_stolen_written: false,
        }
    }

    pub fn name(mut self, value: &str) -> roto_runtime::Result<Self> {
        self.builder.write_string(1, value)?;
        self.name_written = true;
        Ok(self)
    }

    pub fn operations(mut self, value: &[u8]) -> roto_runtime::Result<Self> {
        self.builder.write_bytes(2, value)?;
        self.operations_written = true;
        Ok(self)
    }

    pub fn total_bytes_stolen(mut self, value: u64) -> roto_runtime::Result<Self> {
        self.builder.write_varint(3, value)?;
        self.total_bytes_stolen_written = true;
        Ok(self)
    }

    pub fn with(mut self, msg: &Campaign<'_>) -> roto_runtime::Result<Self> {
        for item in msg.accessor.raw_fields() {
            let (field_number, raw_bytes) = item?;
            let is_written = match field_number {
                1 => self.name_written,
                2 => self.operations_written,
                3 => self.total_bytes_stolen_written,
                _ => false,
            };
            if !is_written {
                self.builder.write_raw(raw_bytes)?;
            }
        }
        Ok(self)
    }

    pub fn finish(self) -> roto_runtime::Result<&'b mut [u8]> {
        self.builder.finish()
    }
}

#[cfg(feature = "alloc")]
pub struct OwnedCampaign {
    pub data: bytes::Bytes,
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoOwned for OwnedCampaign {
    type Reader<'a> = Campaign<'a>;
    fn reader(&self) -> Campaign<'_> {
        Campaign::new(&self.data).expect("failed to create reader")
    }
}

#[cfg(feature = "alloc")]
impl roto_runtime::RotoMessage for OwnedCampaign {
    fn decode(buf: bytes::Bytes) -> roto_runtime::Result<Self> {
        Ok(OwnedCampaign { data: buf })
    }

    fn bytes(&self) -> bytes::Bytes {
        self.data.clone()
    }
}



