Skip to content

Commit 6a4b7c3

Browse files
committed
Made more things public, added From impls
1 parent c50e1c6 commit 6a4b7c3

File tree

2 files changed

+49
-26
lines changed

2 files changed

+49
-26
lines changed

src/netmd/commands.rs

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,15 @@ pub struct Time {
4040
pub frame: u16,
4141
}
4242

43+
impl From<Time> for Duration {
44+
fn from(value: Time) -> Self {
45+
Duration::from_millis(
46+
(value.minute as u64 * 60000)
47+
+ (value.second as u64 * 1000)
48+
)
49+
}
50+
}
51+
4352
/// A representation of the current status of the device.
4453
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4554
pub struct DeviceStatus {
@@ -131,6 +140,22 @@ pub struct Disc {
131140
}
132141

133142
impl Disc {
143+
pub fn title(&self) -> &String {
144+
&self.title
145+
}
146+
147+
pub fn full_width_title(&self) -> &String {
148+
&self.full_width_title
149+
}
150+
151+
pub fn writeable(&self) -> bool {
152+
self.writeable
153+
}
154+
155+
pub fn write_protected(&self) -> bool {
156+
self.write_protected
157+
}
158+
134159
pub fn track_count(&self) -> u16 {
135160
self.groups
136161
.iter()
@@ -328,7 +353,7 @@ impl NetMDContext {
328353
}
329354

330355
/// Get the current status of the device
331-
pub async fn device_status(&mut self) -> Result<DeviceStatus, Box<dyn Error>> {
356+
pub async fn device_status(&mut self) -> Result<DeviceStatus, InterfaceError> {
332357
let status = self.interface.status().await?;
333358
let playback_status = self.interface.playback_status2().await?;
334359
let b1: u16 = playback_status[4] as u16;
@@ -359,7 +384,7 @@ impl NetMDContext {
359384
}
360385

361386
/// Get a representation of the current disc inserted in the device.
362-
pub async fn list_content(&mut self) -> Result<Disc, Box<dyn Error>> {
387+
pub async fn list_content(&mut self) -> Result<Disc, InterfaceError> {
363388
let flags = self.interface.disc_flags().await?;
364389
let title = self.interface.disc_title(false).await?;
365390
let full_width_title = self.interface.disc_title(true).await?;
@@ -439,7 +464,7 @@ impl NetMDContext {
439464
&mut self,
440465
new_name: &str,
441466
new_fw_name: Option<&str>,
442-
) -> Result<(), Box<dyn Error>> {
467+
) -> Result<(), InterfaceError> {
443468
let new_name = sanitize_half_width_title(new_name);
444469
let new_fw_name = new_fw_name.map(sanitize_full_width_title);
445470

@@ -519,7 +544,7 @@ impl NetMDContext {
519544
&mut self,
520545
track: u16,
521546
progress_callback: Option<F>,
522-
) -> Result<(DiscFormat, Vec<u8>), Box<dyn Error>> {
547+
) -> Result<(DiscFormat, Vec<u8>), InterfaceError> {
523548
let mut output_vec = Vec::new();
524549
let (format, _frames, result) = self
525550
.interface
@@ -548,7 +573,7 @@ impl NetMDContext {
548573
Ok((format, header))
549574
}
550575

551-
async fn prepare_download(&mut self) -> Result<(), Box<dyn Error>> {
576+
async fn prepare_download(&mut self) -> Result<(), InterfaceError> {
552577
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(
553578
&self
554579
.device_status()
@@ -611,7 +636,7 @@ impl NetMDContext {
611636
&mut self,
612637
track: MDTrack,
613638
progress_callback: F,
614-
) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>>
639+
) -> Result<(u16, Vec<u8>, Vec<u8>), InterfaceError>
615640
{
616641
self.prepare_download().await?;
617642
// Lock the interface by providing it to the session

src/netmd/interface.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use log::debug;
1212
use num_derive::FromPrimitive;
1313
use rand::RngCore;
1414
use std::collections::HashMap;
15-
use std::error::Error;
1615
use std::time::Duration;
1716
use thiserror::Error;
1817

@@ -22,7 +21,7 @@ use super::utils::{cross_sleep, to_sjis};
2221

2322
/// An action to take on the player
2423
#[derive(Copy, Clone)]
25-
enum Action {
24+
pub enum Action {
2625
Play = 0x75,
2726
Pause = 0x7d,
2827
FastForward = 0x39,
@@ -251,6 +250,9 @@ pub enum EncryptionError {
251250

252251
#[error("supplied {0} length of {1} is invalid")]
253252
InvalidLength(&'static str, usize),
253+
254+
#[error("{0}")]
255+
InvalidState(&'static str)
254256
}
255257

256258
/// An error for any action in the interface
@@ -494,14 +496,14 @@ impl NetMDInterface {
494496
data = self.device.read_reply(None).await?;
495497

496498
let status = NetmdStatus::try_from(data[0])?;
497-
debug!("Device status: {:?}", status);
499+
debug!("Device status: {status:?}");
498500

499501
match status {
500502
NetmdStatus::NotImplemented => {
501-
return Err(InterfaceError::NotImplemented(format!("{:02X?}", data)))
503+
return Err(InterfaceError::NotImplemented(format!("{data:02X?}")))
502504
}
503505
NetmdStatus::Rejected => {
504-
return Err(InterfaceError::Rejected(format!("{:02X?}", data)))
506+
return Err(InterfaceError::Rejected(format!("{data:02X?}")))
505507
}
506508
NetmdStatus::Interim if !accept_interim => {
507509
let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL
@@ -518,15 +520,15 @@ impl NetMDInterface {
518520
}
519521
return Ok(data);
520522
}
521-
_ => return Err(InterfaceError::Unknown(format!("{:02X?}", data))),
523+
_ => return Err(InterfaceError::Unknown(format!("{data:02X?}"))),
522524
}
523525
}
524526

525527
// This should NEVER happen unless the code is changed wrongly
526528
unreachable!("The max number of retries is set to 0!")
527529
}
528530

529-
async fn playback_control(&mut self, action: Action) -> Result<(), InterfaceError> {
531+
pub async fn playback_control(&mut self, action: Action) -> Result<(), InterfaceError> {
530532
let query = format_query(
531533
"18c3 ff %b 000000".to_string(),
532534
vec![QueryValue::Number(action as i64)],
@@ -974,9 +976,6 @@ impl NetMDInterface {
974976

975977
let raw_full_title = self.raw_disc_title(true).await?;
976978

977-
dbg!(&raw_title);
978-
dbg!(&raw_full_title);
979-
980979
let mut full_width_group_list = raw_full_title.split("//");
981980

982981
for (i, group) in group_list.enumerate() {
@@ -1029,8 +1028,7 @@ impl NetMDInterface {
10291028
for track in track_min - 1..track_max {
10301029
if track_dict.contains_key(&track) {
10311030
return Err(InterfaceError::GroupError(format!(
1032-
"track {} is in 2 groups",
1033-
track
1031+
"track {track} is in 2 groups",
10341032
)));
10351033
}
10361034
track_dict.insert(track, (String::from(group_name), i as u16));
@@ -1119,10 +1117,10 @@ impl NetMDInterface {
11191117
Ok(title)
11201118
}
11211119

1122-
// Sets the title of the disc
1123-
//
1124-
// Caution: This does not respect groups. Use the functions available in
1125-
// NetMDContext to properly rename a disc.
1120+
/// Sets the title of the disc
1121+
///
1122+
/// ## Caution: This does not respect groups.
1123+
/// Use the functions available in NetMDContext to properly rename a disc.
11261124
pub async fn set_disc_title(&mut self, title: &str, wchar: bool) -> Result<(), InterfaceError> {
11271125
let current_title = self.raw_disc_title(wchar).await?;
11281126
if current_title == title {
@@ -1948,7 +1946,7 @@ pub(super) struct MDSession<'a> {
19481946
}
19491947

19501948
impl<'a> MDSession<'a> {
1951-
pub async fn init(&mut self) -> Result<(), Box<dyn Error>> {
1949+
pub async fn init(&mut self) -> Result<(), InterfaceError> {
19521950
self.md.enter_secure_session().await?;
19531951
self.md.leaf_id().await?;
19541952

@@ -1971,7 +1969,7 @@ impl<'a> MDSession<'a> {
19711969
Ok(())
19721970
}
19731971

1974-
pub async fn close(&mut self) -> Result<(), Box<dyn Error>> {
1972+
pub async fn close(&mut self) -> Result<(), InterfaceError> {
19751973
if self.hex_session_key.is_none() {
19761974
self.md.session_key_forget().await?;
19771975
}
@@ -1985,10 +1983,10 @@ impl<'a> MDSession<'a> {
19851983
mut track: MDTrack,
19861984
progress_callback: F,
19871985
disc_format: Option<DiscFormat>,
1988-
) -> Result<(u16, Vec<u8>, Vec<u8>), Box<dyn Error>>
1986+
) -> Result<(u16, Vec<u8>, Vec<u8>), InterfaceError>
19891987
{
19901988
if self.hex_session_key.is_none() {
1991-
return Err("Cannot download a track using a non-init()'ed session!".into());
1989+
return Err(EncryptionError::InvalidState("Cannot download a track using a non-init()'ed session!").into());
19921990
}
19931991
self.md
19941992
.setup_download(

0 commit comments

Comments
 (0)