1use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
2use std::rc::Rc;
3
4use rustduck_sys::bridge::ffi;
5
6use crate::buffer::encode_utf16;
7use crate::error::native_exception;
8use crate::{Error, Report, Result};
9
10#[repr(transparent)]
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
13pub struct Standards(pub u32);
14
15impl Standards {
16 pub const NONE: Self = Self(0x00);
18 pub const MPEG: Self = Self(0x01);
20 pub const DVB: Self = Self(0x02);
22 pub const SCTE: Self = Self(0x04);
24 pub const ATSC: Self = Self(0x08);
26 pub const ISDB: Self = Self(0x10);
28 pub const JAPAN: Self = Self(0x20);
30 pub const ABNT: Self = Self(0x40);
32
33 pub const fn bits(self) -> u32 {
35 self.0
36 }
37}
38
39impl BitOr for Standards {
40 type Output = Self;
41 fn bitor(self, rhs: Self) -> Self::Output {
42 Self(self.0 | rhs.0)
43 }
44}
45impl BitOrAssign for Standards {
46 fn bitor_assign(&mut self, rhs: Self) {
47 self.0 |= rhs.0;
48 }
49}
50impl BitAnd for Standards {
51 type Output = Self;
52 fn bitand(self, rhs: Self) -> Self::Output {
53 Self(self.0 & rhs.0)
54 }
55}
56impl BitAndAssign for Standards {
57 fn bitand_assign(&mut self, rhs: Self) {
58 self.0 &= rhs.0;
59 }
60}
61impl Not for Standards {
62 type Output = Self;
63 fn not(self) -> Self::Output {
64 Self(!self.0)
65 }
66}
67
68pub struct DuckContext {
73 native: cxx::UniquePtr<ffi::RustDuckDuckContext>,
74 report: Rc<Report>,
75}
76
77impl DuckContext {
78 pub fn new(report: Rc<Report>) -> Result<Rc<Self>> {
80 let native = ffi::rustduck_context_new(report.native())
81 .map_err(|error| native_exception("DuckContext::new", error))?;
82 if native.is_null() {
83 return Err(Error::NativeFailure {
84 operation: "DuckContext::new",
85 });
86 }
87 Ok(Rc::new(Self { native, report }))
88 }
89
90 pub fn report(&self) -> &Rc<Report> {
92 &self.report
93 }
94
95 pub fn set_default_charset(&mut self, charset: &str) -> Result<()> {
97 let charset = encode_utf16(charset);
98 let accepted =
99 ffi::rustduck_context_set_default_charset(self.native.pin_mut(), &charset)
100 .map_err(|error| native_exception("DuckContext::set_default_charset", error))?;
101 accepted.then_some(()).ok_or(Error::NativeFailure {
102 operation: "DuckContext::set_default_charset",
103 })
104 }
105
106 pub fn set_default_cas_id(&mut self, cas_id: u16) -> Result<()> {
108 ffi::rustduck_context_set_default_cas_id(self.native.pin_mut(), cas_id)
109 .map_err(|error| native_exception("DuckContext::set_default_cas_id", error))
110 }
111
112 pub fn set_default_pds(&mut self, pds: u32) -> Result<()> {
114 ffi::rustduck_context_set_default_pds(self.native.pin_mut(), pds)
115 .map_err(|error| native_exception("DuckContext::set_default_pds", error))
116 }
117
118 pub fn add_standards(&mut self, standards: Standards) -> Result<()> {
120 ffi::rustduck_context_add_standards(self.native.pin_mut(), standards.bits())
121 .map_err(|error| native_exception("DuckContext::add_standards", error))
122 }
123
124 pub fn standards(&self) -> Result<Standards> {
126 ffi::rustduck_context_standards(
127 self.native
128 .as_ref()
129 .expect("DuckContext construction rejects null owners"),
130 )
131 .map(Standards)
132 .map_err(|error| native_exception("DuckContext::standards", error))
133 }
134
135 pub fn reset_standards(&mut self, standards: Standards) -> Result<()> {
137 ffi::rustduck_context_reset_standards(self.native.pin_mut(), standards.bits())
138 .map_err(|error| native_exception("DuckContext::reset_standards", error))
139 }
140
141 pub fn set_time_reference_offset(&mut self, offset_millis: i64) -> Result<()> {
143 ffi::rustduck_context_set_time_reference_offset(self.native.pin_mut(), offset_millis)
144 .map_err(|error| native_exception("DuckContext::set_time_reference_offset", error))
145 }
146
147 pub fn set_time_reference(&mut self, name: &str) -> Result<()> {
149 let name = encode_utf16(name);
150 let accepted = ffi::rustduck_context_set_time_reference(self.native.pin_mut(), &name)
151 .map_err(|error| native_exception("DuckContext::set_time_reference", error))?;
152 accepted.then_some(()).ok_or(Error::NativeFailure {
153 operation: "DuckContext::set_time_reference",
154 })
155 }
156
157 pub(crate) fn native(&self) -> &ffi::RustDuckDuckContext {
158 self.native
159 .as_ref()
160 .expect("DuckContext construction rejects null owners")
161 }
162}