mod error;
#[cfg(feature = "http")]
mod http;
mod parse;
mod replication;
mod value;
use std::fmt;
use std::sync::Arc;
pub use self::error::{OscError, StateError, XmlError, XmlPosition};
#[cfg(feature = "http")]
pub use self::http::HttpFetch;
pub use self::parse::OscReader;
pub use self::replication::{Diff, Diffs, Fetch, Replication, State, path_sequence, sequence_path};
use crate::model::{Location, Member, MemberType};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Action {
Create,
Modify,
Delete,
}
impl Action {
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Create => "create",
Self::Modify => "modify",
Self::Delete => "delete",
}
}
}
impl fmt::Display for Action {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.name())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Change {
pub action: Action,
pub object: Object,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Object {
Node(Node),
Way(Way),
Relation(Relation),
}
impl Object {
#[must_use]
pub fn kind(&self) -> MemberType {
match self {
Self::Node(_) => MemberType::Node,
Self::Way(_) => MemberType::Way,
Self::Relation(_) => MemberType::Relation,
}
}
#[must_use]
pub fn id(&self) -> i64 {
match self {
Self::Node(o) => o.id,
Self::Way(o) => o.id,
Self::Relation(o) => o.id,
}
}
#[must_use]
pub fn meta(&self) -> &Meta {
match self {
Self::Node(o) => &o.meta,
Self::Way(o) => &o.meta,
Self::Relation(o) => &o.meta,
}
}
#[must_use]
pub fn tags(&self) -> &Tags {
match self {
Self::Node(o) => &o.tags,
Self::Way(o) => &o.tags,
Self::Relation(o) => &o.tags,
}
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Meta {
pub version: Option<i32>,
pub timestamp_ms: Option<i64>,
pub changeset: Option<i64>,
pub uid: Option<i32>,
pub user: Option<Arc<str>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Node {
pub id: i64,
pub meta: Meta,
pub tags: Tags,
pub location: Option<Location>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Way {
pub id: i64,
pub meta: Meta,
pub tags: Tags,
pub refs: Vec<i64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Relation {
pub id: i64,
pub meta: Meta,
pub tags: Tags,
members: Box<[(MemberType, i64)]>,
roles: Strings,
}
impl Relation {
pub fn members(&self) -> impl ExactSizeIterator<Item = Member<'_>> {
self.members
.iter()
.enumerate()
.map(|(i, &(kind, id))| Member {
kind,
id,
role: self.roles.get(i),
})
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Tags(Strings);
impl Tags {
#[must_use]
pub fn len(&self) -> usize {
self.0.ends.len() / 2
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.ends.is_empty()
}
pub fn iter(&self) -> impl ExactSizeIterator<Item = (&str, &str)> + Clone {
(0..self.len()).map(|i| (self.0.get(2 * i), self.0.get(2 * i + 1)))
}
#[must_use]
pub fn get(&self, key: &str) -> Option<&str> {
self.iter().find(|&(k, _)| k == key).map(|(_, v)| v)
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
struct Strings {
text: Box<str>,
ends: Box<[u32]>,
}
impl Strings {
fn get(&self, i: usize) -> &str {
let start = i.checked_sub(1).map_or(0, |prev| self.ends[prev]);
&self.text[start as usize..self.ends[i] as usize]
}
}
#[derive(Debug, Default)]
struct StringsBuf {
text: String,
ends: Vec<u32>,
}
const MAX_STRINGS: usize = 2 << 20;
const MAX_TEXT: usize = 64 << 20;
impl StringsBuf {
fn push(&mut self, raw: &[u8]) -> Result<(), XmlError> {
value::push_text(raw, &mut self.text)?;
if self.ends.len() >= MAX_STRINGS || self.text.len() > MAX_TEXT {
return Err(XmlError::TooLarge);
}
self.ends
.push(u32::try_from(self.text.len()).map_err(|_| XmlError::TooLarge)?);
Ok(())
}
fn clear(&mut self) {
self.text.clear();
self.ends.clear();
}
fn to_strings(&self) -> Strings {
Strings {
text: self.text.as_str().into(),
ends: self.ends.as_slice().into(),
}
}
}