change uniform interface for more comprehensive access to entity data

This commit is contained in:
lurchi 2016-09-10 13:11:50 +02:00
parent 29103a611a
commit 393d3d0883
4 changed files with 83 additions and 21 deletions

View File

@ -13,4 +13,5 @@ pub use parser::*;
pub use packet::*;
pub use packet_id::*;
pub use uniform::*;
pub use uniform_types::{UniformParseError, PsycScheme, PsycEntity};
pub use packet_types::{PsycOperator, PsycStateOp};

View File

@ -1,6 +1,13 @@
use std::os::raw::c_char;
#[derive(Clone)]
/// Return code: OK/error.
#[repr(C)]
pub enum PsycRC {
PSYC_OK = 1,
PSYC_ERROR = -1,
}
#[derive(Debug)]
#[repr(C)]
pub struct PsycString {
pub length: usize,

View File

@ -1,3 +1,4 @@
use types::PsycRC;
use uniform_types::*;
use std::os::raw::c_char;
use std::os::raw::c_int;
@ -10,8 +11,11 @@ extern "C" {
buffer: *const c_char,
length: usize)
-> c_int;
fn psyc_entity_type(entity: c_char) -> c_int;
}
#[derive(Debug)]
pub struct Uniform<'a> {
uniform: PsycUniform,
phantom: PhantomData<&'a Vec<u8>>
@ -47,14 +51,45 @@ impl<'a> Uniform<'a> {
self.uniform.valid
}
pub fn scheme(&self) -> PsycScheme {
self.uniform.uniform_type
pub fn entity(&self) -> PsycEntity<'a> {
match self.resource() {
"" => PsycEntity::Root,
_resource => unsafe {
let type_specifier = *self.uniform.resource.data;
let entity_type_int = psyc_entity_type(type_specifier);
if entity_type_int == PsycRC::PSYC_ERROR as c_int {
return PsycEntity::Unknown {
object: self.resource(),
channel: self.channel()
}
}
let entity_type: PsycEntityType = mem::transmute(entity_type_int);
match entity_type {
PsycEntityType::PSYC_ENTITY_PERSON => PsycEntity::Person {
name: self.nick(),
channel: self.channel()
},
PsycEntityType::PSYC_ENTITY_PLACE => PsycEntity::Place {
name: self.nick(),
channel: self.channel()
},
PsycEntityType::PSYC_ENTITY_SERVICE => PsycEntity::Service {
name: self.nick(),
channel: self.channel()
},
PsycEntityType::PSYC_ENTITY_ROOT => PsycEntity::Root
}
}
}
}
pub fn name(&self) -> &'a str {
unsafe {
util::cstring_to_str(self.uniform.user.data, self.uniform.user.length)
}
pub fn scheme(&self) -> PsycScheme {
self.uniform.uniform_type
}
pub fn password(&self) -> &'a str {
@ -114,12 +149,6 @@ impl<'a> Uniform<'a> {
}
}
pub fn channel(&self) -> &'a str {
unsafe {
util::cstring_to_str(self.uniform.channel.data, self.uniform.channel.length)
}
}
pub fn full(&self) -> &'a str {
unsafe {
util::cstring_to_str(self.uniform.full.data, self.uniform.full.length)
@ -138,21 +167,22 @@ impl<'a> Uniform<'a> {
}
}
pub fn entity(&self) -> &'a str {
unsafe {
util::cstring_to_str(self.uniform.entity.data, self.uniform.entity.length)
}
}
pub fn path(&self) -> &'a str {
unsafe {
util::cstring_to_str(self.uniform.path.data, self.uniform.path.length)
}
}
pub fn nick(&self) -> &'a str {
/* private functions */
fn nick(&self) -> &'a str {
unsafe {
util::cstring_to_str(self.uniform.nick.data, self.uniform.nick.length)
}
}
fn channel(&self) -> &'a str {
unsafe {
util::cstring_to_str(self.uniform.channel.data, self.uniform.channel.length)
}
}
}

View File

@ -1,6 +1,28 @@
use types::*;
#[derive(Debug, PartialEq)]
pub enum PsycEntity<'a> {
Root,
Person {
name: &'a str,
channel: &'a str
},
Place {
name: &'a str,
channel: &'a str
},
Service {
name: &'a str,
channel: &'a str
},
Unknown {
object: &'a str,
channel: &'a str
}
}
#[derive(Clone, Copy)]
#[derive(Debug, PartialEq)]
#[repr(C)]
pub enum PsycScheme {
PSYC_SCHEME_PSYC = 0,
@ -17,6 +39,7 @@ pub enum PsycEntityType {
PSYC_ENTITY_SERVICE = '$' as _
}
#[derive(Debug, PartialEq)]
#[repr(C)]
pub enum PsycTransport {
PSYC_TRANSPORT_TCP = 'c' as _,
@ -25,6 +48,7 @@ pub enum PsycTransport {
PSYC_TRANSPORT_GNUNET = 'g' as _,
}
#[derive(Debug, PartialEq)]
#[repr(C)]
pub enum UniformParseError {
PSYC_PARSE_UNIFORM_INVALID_SLASHES = -7,
@ -36,7 +60,7 @@ pub enum UniformParseError {
PSYC_PARSE_UNIFORM_INVALID_SCHEME = -1,
}
#[derive(Clone)]
#[derive(Debug)]
#[repr(C)]
pub struct PsycUniform {
pub valid: bool,