fn from_bytes()

in core/sdk/src/users/create_user.rs [108:161]


    fn from_bytes(bytes: Bytes) -> Result<CreateUser, IggyError> {
        if bytes.len() < 10 {
            return Err(IggyError::InvalidCommand);
        }

        let username_length = bytes[0];
        let username = from_utf8(&bytes[1..1 + username_length as usize])
            .map_err(|_| IggyError::InvalidUtf8)?
            .to_string();
        if username.len() != username_length as usize {
            return Err(IggyError::InvalidCommand);
        }

        let mut position = 1 + username_length as usize;
        let password_length = bytes[position];
        position += 1;
        let password = from_utf8(&bytes[position..position + password_length as usize])
            .map_err(|_| IggyError::InvalidUtf8)?
            .to_string();
        if password.len() != password_length as usize {
            return Err(IggyError::InvalidCommand);
        }

        position += password_length as usize;
        let status = UserStatus::from_code(bytes[position])?;
        position += 1;
        let has_permissions = bytes[position];
        if has_permissions > 1 {
            return Err(IggyError::InvalidCommand);
        }

        position += 1;
        let permissions = if has_permissions == 1 {
            let permissions_length = u32::from_le_bytes(
                bytes[position..position + 4]
                    .try_into()
                    .map_err(|_| IggyError::InvalidNumberEncoding)?,
            );
            position += 4;
            Some(Permissions::from_bytes(
                bytes.slice(position..position + permissions_length as usize),
            )?)
        } else {
            None
        };

        let command = CreateUser {
            username,
            password,
            status,
            permissions,
        };
        Ok(command)
    }