use std::time::Duration;
use mln_style::tile_id::CanonicalTileID;
use mln_wire::payload::Payload;
use mln_wire::{Context, Reader, Wire, Writer};
use crate::tile::OverscaledTileID;
use crate::worker::job::{JobId, JobTimes, SourceKey, TileKey};
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Encoded {
pub bytes: Vec<u8>,
pub payloads: Vec<Payload>,
}
impl From<Vec<u8>> for Encoded {
fn from(bytes: Vec<u8>) -> Self {
Encoded { bytes, payloads: Vec::new() }
}
}
const JOB_ID_SIZE: usize = 8 + 1 + 2 + 1 + 4 + 4 + 8;
pub(crate) fn write_job_header(bytes: &mut Vec<u8>, id: &JobId) {
let JobId { tile: TileKey { source: SourceKey(source), id: tile }, generation } = *id;
bytes.extend_from_slice(&source.to_le_bytes());
bytes.push(tile.overscaled_z);
bytes.extend_from_slice(&tile.wrap.to_le_bytes());
bytes.push(tile.canonical.z);
bytes.extend_from_slice(&tile.canonical.x.to_le_bytes());
bytes.extend_from_slice(&tile.canonical.y.to_le_bytes());
bytes.extend_from_slice(&generation.to_le_bytes());
}
pub(crate) fn read_job_id(bytes: &[u8]) -> Option<(JobId, &[u8])> {
let (header, rest) = bytes.split_at_checked(JOB_ID_SIZE)?;
let (source, header) = header.split_first_chunk::<8>()?;
let (&[overscaled_z], header) = header.split_first_chunk::<1>()?;
let (wrap, header) = header.split_first_chunk::<2>()?;
let (&[z], header) = header.split_first_chunk::<1>()?;
let (x, header) = header.split_first_chunk::<4>()?;
let (y, header) = header.split_first_chunk::<4>()?;
let (generation, _) = header.split_first_chunk::<8>()?;
let canonical = CanonicalTileID { z, x: u32::from_le_bytes(*x), y: u32::from_le_bytes(*y) };
let tile = OverscaledTileID { overscaled_z, wrap: i16::from_le_bytes(*wrap), canonical };
let tile = TileKey { source: SourceKey(u64::from_le_bytes(*source)), id: tile };
Some((JobId { tile, generation: u64::from_le_bytes(*generation) }, rest))
}
pub(crate) fn write_reply_header(bytes: &mut Vec<u8>, id: &JobId, times: &JobTimes) {
write_job_header(bytes, id);
let micros = |time: Duration| u32::try_from(time.as_micros()).unwrap_or(u32::MAX);
bytes.extend_from_slice(µs(times.discover).to_le_bytes());
bytes.extend_from_slice(µs(times.build).to_le_bytes());
}
pub(crate) fn read_reply_header(bytes: &[u8]) -> Option<(JobId, JobTimes, &[u8])> {
let (id, rest) = read_job_id(bytes)?;
let (discover, rest) = rest.split_first_chunk::<4>()?;
let (build, rest) = rest.split_first_chunk::<4>()?;
let time = |micros: &[u8; 4]| Duration::from_micros(u64::from(u32::from_le_bytes(*micros)));
Some((id, JobTimes { discover: time(discover), build: time(build) }, rest))
}
pub(crate) fn append<T: Wire>(bytes: &mut Vec<u8>, value: &T, mut context: impl Context) -> Result<(), String> {
let mut w = Writer::from_vec(std::mem::take(bytes)).with_context(&mut context);
value.write(&mut w);
*bytes = w.finish().map_err(|e| format!("can't encode: {e}"))?;
Ok(())
}
pub(crate) fn take<T: Wire>(bytes: &[u8]) -> Result<(T, &[u8]), String> {
let mut r = Reader::new(bytes);
match T::read(&mut r) {
Some(value) => Ok((value, r.rest())),
None => Err(format!("malformed: {}", r.error())),
}
}
pub(crate) fn decode<T: Wire>(bytes: &[u8], mut context: impl Context) -> Result<T, String> {
let mut r = Reader::new(bytes).with_context(&mut context);
match T::read(&mut r) {
Some(_) if !r.rest().is_empty() => Err(format!("malformed: {} bytes after the end", r.rest().len())),
Some(value) => Ok(value),
None => Err(format!("malformed: {}", r.error())),
}
}