use std::borrow::Borrow;
use std::marker::PhantomData;
use rustorb_cdr::{ByteOrder, CdrEncode, CdrVersion, Decoder, Encoder};
use crate::{
CompletionStatus, Exception, InStream, ObjectRef, OutStream, SystemException,
SystemExceptionKind, TCKind, TypeCode, minor,
};
mod value;
pub use value::{
CustomMarshal, DataInputStream, DataOutputStream, ValueHandle, ValueInfo, ValueState, read_box,
register_value, write_box,
};
pub const GENERATED_CODE_VERSION_1: () = ();
pub type Sys<T> = Result<T, SystemException>;
pub trait IdlType: 'static {
type Rust: Borrow<Self::Borrowed> + 'static;
type Borrowed: ?Sized;
const MIN_SIZE: usize;
fn type_code() -> TypeCode;
fn write(v: &Self::Borrowed, out: &mut OutStream<'_>) -> Sys<()>;
fn read(input: &mut InStream<'_>) -> Sys<Self::Rust>;
fn write_items(items: &[Self::Rust], out: &mut OutStream<'_>) -> Sys<()> {
items.iter().try_for_each(|v| Self::write(v.borrow(), out))
}
fn read_items(len: usize, input: &mut InStream<'_>) -> Sys<Vec<Self::Rust>> {
let mut items = Vec::with_capacity(len.min(4096));
for _ in 0..len {
items.push(Self::read(input)?);
}
Ok(items)
}
}
pub fn bad_param() -> SystemException {
SystemException::new(SystemExceptionKind::BAD_PARAM, 0, CompletionStatus::No)
}
pub fn data_conversion() -> SystemException {
SystemException::new(
SystemExceptionKind::DATA_CONVERSION,
0,
CompletionStatus::No,
)
}
pub fn unknown_operation() -> SystemException {
SystemException::new(
SystemExceptionKind::BAD_OPERATION,
minor::UNKNOWN_OPERATION,
CompletionStatus::No,
)
}
pub fn system_exception(e: Exception) -> SystemException {
match e {
Exception::System(e) => e,
Exception::User(_) => unlisted_user_exception(),
}
}
pub fn unlisted_user_exception() -> SystemException {
SystemException::new(
SystemExceptionKind::UNKNOWN,
minor::UNLISTED_USER_EXCEPTION,
CompletionStatus::Maybe,
)
}
pub fn is_a(obj: &ObjectRef, repository_id: &str) -> Sys<bool> {
if obj.type_id() == repository_id {
return Ok(true);
}
obj.is_a(repository_id).map_err(system_exception)
}
pub fn simple_tc(kind: TCKind) -> TypeCode {
TypeCode::simple(kind).expect("a simple kind")
}
pub fn tc(r: Result<TypeCode, crate::TypeCodeError>) -> TypeCode {
r.expect("a TypeCode checked by rustorb-idl")
}
macro_rules! numeric {
($($(#[$doc:meta])* $name:ident: $ty:ty, $kind:ident, $size:expr;)*) => {
$(
$(#[$doc])*
#[derive(Debug)]
pub struct $name;
impl IdlType for $name {
type Rust = $ty;
type Borrowed = $ty;
const MIN_SIZE: usize = $size;
fn type_code() -> TypeCode {
simple_tc(TCKind::$kind)
}
fn write(v: &$ty, out: &mut OutStream<'_>) -> Sys<()> {
Ok(v.encode(out)?)
}
fn read(input: &mut InStream<'_>) -> Sys<$ty> {
input.read::<$ty>()
}
}
)*
};
}
numeric! {
Boolean: bool, Boolean, 1;
Short: i16, Short, 2;
UShort: u16, UShort, 2;
Long: i32, Long, 4;
ULong: u32, ULong, 4;
LongLong: i64, LongLong, 8;
ULongLong: u64, ULongLong, 8;
Float: f32, Float, 4;
Double: f64, Double, 8;
LongDouble: crate::LongDouble, LongDouble, 16;
}
#[derive(Debug)]
pub struct Octet;
impl IdlType for Octet {
type Rust = u8;
type Borrowed = u8;
const MIN_SIZE: usize = 1;
fn type_code() -> TypeCode {
simple_tc(TCKind::Octet)
}
fn write(v: &u8, out: &mut OutStream<'_>) -> Sys<()> {
out.write_octet(*v);
Ok(())
}
fn read(input: &mut InStream<'_>) -> Sys<u8> {
input.read::<u8>()
}
fn write_items(items: &[u8], out: &mut OutStream<'_>) -> Sys<()> {
out.write_octets(items);
Ok(())
}
fn read_items(len: usize, input: &mut InStream<'_>) -> Sys<Vec<u8>> {
let e = input.marshal_error();
Ok(input.read_octets(len).map_err(|_| e)?.to_vec())
}
}
#[derive(Debug)]
pub struct Char;
impl IdlType for Char {
type Rust = char;
type Borrowed = char;
const MIN_SIZE: usize = 1;
fn type_code() -> TypeCode {
simple_tc(TCKind::Char)
}
fn write(v: &char, out: &mut OutStream<'_>) -> Sys<()> {
let bytes = out.code_sets().encode_string(v.encode_utf8(&mut [0; 4]))?;
match bytes[..] {
[b] => {
out.write_char(b);
Ok(())
}
_ => Err(data_conversion()),
}
}
fn read(input: &mut InStream<'_>) -> Sys<char> {
input.read_char_value()
}
}
#[derive(Debug)]
pub struct WChar;
impl IdlType for WChar {
type Rust = char;
type Borrowed = char;
const MIN_SIZE: usize = 1;
fn type_code() -> TypeCode {
simple_tc(TCKind::WChar)
}
fn write(v: &char, out: &mut OutStream<'_>) -> Sys<()> {
let unit = u16::try_from(u32::from(*v)).map_err(|_| data_conversion())?;
out.write_wchar(unit)
}
fn read(input: &mut InStream<'_>) -> Sys<char> {
let unit = input.read_wchar()?;
char::from_u32(u32::from(unit)).ok_or_else(|| {
SystemException::new(
SystemExceptionKind::DATA_CONVERSION,
0,
input.completion_status(),
)
})
}
}
#[derive(Debug)]
pub struct String;
impl IdlType for String {
type Rust = std::string::String;
type Borrowed = str;
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
TypeCode::string(0)
}
fn write(v: &str, out: &mut OutStream<'_>) -> Sys<()> {
out.write_string(v)
}
fn read(input: &mut InStream<'_>) -> Sys<std::string::String> {
input.read_string()
}
}
#[derive(Debug)]
pub struct WString;
impl IdlType for WString {
type Rust = std::string::String;
type Borrowed = str;
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
TypeCode::wstring(0)
}
fn write(v: &str, out: &mut OutStream<'_>) -> Sys<()> {
out.write_wstring(v)
}
fn read(input: &mut InStream<'_>) -> Sys<std::string::String> {
input.read_wstring()
}
}
fn check_len_out(len: usize, bound: u32) -> Sys<()> {
if len > bound as usize {
return Err(bad_param());
}
Ok(())
}
fn check_len_in(len: usize, bound: u32, input: &InStream<'_>) -> Sys<()> {
if len > bound as usize {
return Err(input.marshal_error());
}
Ok(())
}
#[derive(Debug)]
pub struct BoundedString<const N: u32>;
impl<const N: u32> IdlType for BoundedString<N> {
type Rust = std::string::String;
type Borrowed = str;
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
TypeCode::string(N)
}
fn write(v: &str, out: &mut OutStream<'_>) -> Sys<()> {
check_len_out(v.chars().count(), N)?;
out.write_string(v)
}
fn read(input: &mut InStream<'_>) -> Sys<std::string::String> {
let s = input.read_string()?;
check_len_in(s.chars().count(), N, input)?;
Ok(s)
}
}
#[derive(Debug)]
pub struct BoundedWString<const N: u32>;
impl<const N: u32> IdlType for BoundedWString<N> {
type Rust = std::string::String;
type Borrowed = str;
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
TypeCode::wstring(N)
}
fn write(v: &str, out: &mut OutStream<'_>) -> Sys<()> {
check_len_out(v.chars().count(), N)?;
out.write_wstring(v)
}
fn read(input: &mut InStream<'_>) -> Sys<std::string::String> {
let s = input.read_wstring()?;
check_len_in(s.chars().count(), N, input)?;
Ok(s)
}
}
#[derive(Debug)]
pub struct Sequence<T>(PhantomData<T>);
impl<T: IdlType> IdlType for Sequence<T> {
type Rust = Vec<T::Rust>;
type Borrowed = [T::Rust];
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
tc(TypeCode::sequence(&T::type_code(), 0))
}
fn write(v: &[T::Rust], out: &mut OutStream<'_>) -> Sys<()> {
out.write_len(v.len())?;
T::write_items(v, out)
}
fn read(input: &mut InStream<'_>) -> Sys<Vec<T::Rust>> {
let len = read_len(input, T::MIN_SIZE)?;
T::read_items(len, input)
}
}
#[derive(Debug)]
pub struct BoundedSequence<T, const N: u32>(PhantomData<T>);
impl<T: IdlType, const N: u32> IdlType for BoundedSequence<T, N> {
type Rust = Vec<T::Rust>;
type Borrowed = [T::Rust];
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
tc(TypeCode::sequence(&T::type_code(), N))
}
fn write(v: &[T::Rust], out: &mut OutStream<'_>) -> Sys<()> {
check_len_out(v.len(), N)?;
out.write_len(v.len())?;
T::write_items(v, out)
}
fn read(input: &mut InStream<'_>) -> Sys<Vec<T::Rust>> {
let len = read_len(input, T::MIN_SIZE)?;
check_len_in(len, N, input)?;
T::read_items(len, input)
}
}
pub fn read_len(input: &mut InStream<'_>, min_size: usize) -> Sys<usize> {
let e = input.marshal_error();
input.read_len(min_size.max(1)).map_err(|_| e)
}
#[derive(Debug)]
pub struct Array<T, const N: usize>(PhantomData<T>);
impl<T: IdlType, const N: usize> IdlType for Array<T, N> {
type Rust = [T::Rust; N];
type Borrowed = [T::Rust; N];
const MIN_SIZE: usize = T::MIN_SIZE.saturating_mul(N);
fn type_code() -> TypeCode {
let n = u32::try_from(N).expect("array length checked by rustorb-idl");
tc(TypeCode::array(&T::type_code(), n))
}
fn write(v: &[T::Rust; N], out: &mut OutStream<'_>) -> Sys<()> {
T::write_items(v, out)
}
fn read(input: &mut InStream<'_>) -> Sys<[T::Rust; N]> {
let items = T::read_items(N, input)?;
items.try_into().map_err(|_| input.marshal_error())
}
}
#[derive(Debug)]
pub struct Object;
impl IdlType for Object {
type Rust = Option<ObjectRef>;
type Borrowed = Option<ObjectRef>;
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
TypeCode::object()
}
fn write(v: &Option<ObjectRef>, out: &mut OutStream<'_>) -> Sys<()> {
out.write_object(v.as_ref())
}
fn read(input: &mut InStream<'_>) -> Sys<Option<ObjectRef>> {
input.read_object()
}
}
#[derive(Debug)]
pub struct Any;
impl IdlType for Any {
type Rust = crate::Any;
type Borrowed = crate::Any;
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
simple_tc(TCKind::Any)
}
fn write(v: &crate::Any, out: &mut OutStream<'_>) -> Sys<()> {
out.write_any(v)
}
fn read(input: &mut InStream<'_>) -> Sys<crate::Any> {
input.read_any()
}
}
#[derive(Debug)]
pub struct TypeCodeType;
impl IdlType for TypeCodeType {
type Rust = TypeCode;
type Borrowed = TypeCode;
const MIN_SIZE: usize = 4;
fn type_code() -> TypeCode {
simple_tc(TCKind::TypeCode)
}
fn write(v: &TypeCode, out: &mut OutStream<'_>) -> Sys<()> {
out.write_typecode(v)
}
fn read(input: &mut InStream<'_>) -> Sys<TypeCode> {
input.read_typecode()
}
}
#[derive(Debug)]
pub struct Fixed<const D: u16, const S: u16>;
impl<const D: u16, const S: u16> IdlType for Fixed<D, S> {
type Rust = crate::Fixed;
type Borrowed = crate::Fixed;
const MIN_SIZE: usize = (D as usize + 2) / 2;
fn type_code() -> TypeCode {
let scale = i16::try_from(S).expect("scale checked by rustorb-idl");
tc(TypeCode::fixed(D, scale))
}
fn write(v: &crate::Fixed, out: &mut OutStream<'_>) -> Sys<()> {
let digits = v.to_digits(D, S).ok_or_else(data_conversion)?;
out.write_fixed(&digits, v.is_negative())
}
fn read(input: &mut InStream<'_>) -> Sys<crate::Fixed> {
let (digits, negative) = input.read_fixed(D as usize)?;
crate::Fixed::from_digits(&digits, S, negative).ok_or_else(|| input.marshal_error())
}
}
impl crate::Any {
pub fn insert<T: IdlType>(v: &T::Borrowed) -> Sys<crate::Any> {
let mut enc = Encoder::new(ByteOrder::Big, CdrVersion::Giop1_2);
let mut out = OutStream::standalone(&mut enc);
T::write(v, &mut out)?;
let orb = out.orb.take();
let bytes = enc.into_bytes();
let dec = Decoder::new(&bytes, ByteOrder::Big, CdrVersion::Giop1_2);
let mut input = InStream::standalone(dec, orb.as_ref());
let tc = T::type_code();
let value = input.read_value(&tc)?;
Ok(crate::Any::new(tc, value))
}
pub fn extract<T: IdlType>(&self) -> Option<T::Rust> {
if !self.typecode().equivalent(&T::type_code()) {
return None;
}
let mut enc = Encoder::new(ByteOrder::Big, CdrVersion::Giop1_2);
let mut out = OutStream::standalone(&mut enc);
out.write_value(self.typecode(), self.value()).ok()?;
let orb = out.orb.take();
let bytes = enc.into_bytes();
let dec = Decoder::new(&bytes, ByteOrder::Big, CdrVersion::Giop1_2);
let mut input = InStream::standalone(dec, orb.as_ref());
T::read(&mut input).ok()
}
}
#[cfg(test)]
mod tests;