mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
change uniform interface for more comprehensive access to entity data
This commit is contained in:
parent
29103a611a
commit
393d3d0883
4 changed files with 83 additions and 21 deletions
|
@ -13,4 +13,5 @@ pub use parser::*;
|
||||||
pub use packet::*;
|
pub use packet::*;
|
||||||
pub use packet_id::*;
|
pub use packet_id::*;
|
||||||
pub use uniform::*;
|
pub use uniform::*;
|
||||||
|
pub use uniform_types::{UniformParseError, PsycScheme, PsycEntity};
|
||||||
pub use packet_types::{PsycOperator, PsycStateOp};
|
pub use packet_types::{PsycOperator, PsycStateOp};
|
||||||
|
|
|
@ -1,6 +1,13 @@
|
||||||
use std::os::raw::c_char;
|
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)]
|
#[repr(C)]
|
||||||
pub struct PsycString {
|
pub struct PsycString {
|
||||||
pub length: usize,
|
pub length: usize,
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use types::PsycRC;
|
||||||
use uniform_types::*;
|
use uniform_types::*;
|
||||||
use std::os::raw::c_char;
|
use std::os::raw::c_char;
|
||||||
use std::os::raw::c_int;
|
use std::os::raw::c_int;
|
||||||
|
@ -10,8 +11,11 @@ extern "C" {
|
||||||
buffer: *const c_char,
|
buffer: *const c_char,
|
||||||
length: usize)
|
length: usize)
|
||||||
-> c_int;
|
-> c_int;
|
||||||
|
|
||||||
|
fn psyc_entity_type(entity: c_char) -> c_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
pub struct Uniform<'a> {
|
pub struct Uniform<'a> {
|
||||||
uniform: PsycUniform,
|
uniform: PsycUniform,
|
||||||
phantom: PhantomData<&'a Vec<u8>>
|
phantom: PhantomData<&'a Vec<u8>>
|
||||||
|
@ -47,15 +51,46 @@ impl<'a> Uniform<'a> {
|
||||||
self.uniform.valid
|
self.uniform.valid
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scheme(&self) -> PsycScheme {
|
pub fn entity(&self) -> PsycEntity<'a> {
|
||||||
self.uniform.uniform_type
|
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()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn name(&self) -> &'a str {
|
let entity_type: PsycEntityType = mem::transmute(entity_type_int);
|
||||||
unsafe {
|
|
||||||
util::cstring_to_str(self.uniform.user.data, self.uniform.user.length)
|
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 scheme(&self) -> PsycScheme {
|
||||||
|
self.uniform.uniform_type
|
||||||
|
}
|
||||||
|
|
||||||
pub fn password(&self) -> &'a str {
|
pub fn password(&self) -> &'a str {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
@ -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 {
|
pub fn full(&self) -> &'a str {
|
||||||
unsafe {
|
unsafe {
|
||||||
util::cstring_to_str(self.uniform.full.data, self.uniform.full.length)
|
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 {
|
pub fn path(&self) -> &'a str {
|
||||||
unsafe {
|
unsafe {
|
||||||
util::cstring_to_str(self.uniform.path.data, self.uniform.path.length)
|
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 {
|
unsafe {
|
||||||
util::cstring_to_str(self.uniform.nick.data, self.uniform.nick.length)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,28 @@
|
||||||
use types::*;
|
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(Clone, Copy)]
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub enum PsycScheme {
|
pub enum PsycScheme {
|
||||||
PSYC_SCHEME_PSYC = 0,
|
PSYC_SCHEME_PSYC = 0,
|
||||||
|
@ -17,6 +39,7 @@ pub enum PsycEntityType {
|
||||||
PSYC_ENTITY_SERVICE = '$' as _
|
PSYC_ENTITY_SERVICE = '$' as _
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub enum PsycTransport {
|
pub enum PsycTransport {
|
||||||
PSYC_TRANSPORT_TCP = 'c' as _,
|
PSYC_TRANSPORT_TCP = 'c' as _,
|
||||||
|
@ -25,6 +48,7 @@ pub enum PsycTransport {
|
||||||
PSYC_TRANSPORT_GNUNET = 'g' as _,
|
PSYC_TRANSPORT_GNUNET = 'g' as _,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, PartialEq)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub enum UniformParseError {
|
pub enum UniformParseError {
|
||||||
PSYC_PARSE_UNIFORM_INVALID_SLASHES = -7,
|
PSYC_PARSE_UNIFORM_INVALID_SLASHES = -7,
|
||||||
|
@ -36,7 +60,7 @@ pub enum UniformParseError {
|
||||||
PSYC_PARSE_UNIFORM_INVALID_SCHEME = -1,
|
PSYC_PARSE_UNIFORM_INVALID_SCHEME = -1,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Debug)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
pub struct PsycUniform {
|
pub struct PsycUniform {
|
||||||
pub valid: bool,
|
pub valid: bool,
|
||||||
|
|
Loading…
Reference in a new issue