-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: backup and restore of samples using yaml file #3
base: main
Are you sure you want to change the base?
Conversation
Thanks for the PR! Will give it a look today |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great feature, thanks!
Got some minor comments. Also, please do a cargo clippy
run and fix the issues from its output.
use super::sample_slots::SampleSlots; | ||
use serde::{Deserialize, Serialize}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please regroup imports the way it's done in other files
// Std
use std::{...};
// External crate
use external_crate::{...};
// This crate
use crate::{...};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use super::sample_slots::SampleSlots; | |
use serde::{Deserialize, Serialize}; | |
use serde::{Deserialize, Serialize}; | |
use super::sample_slots::SampleSlots; |
fn upload_sample_from_file( | ||
&mut self, | ||
input: PathBuf, | ||
sample_no: Option<u8>, | ||
mono_mode: MonoMode, | ||
output: Option<PathBuf>, | ||
dry_run: bool, | ||
name: Option<&str>, | ||
check_overwrite: bool, | ||
) -> Result<()> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Too many arguments (clippy). I would split it into two functions: load and upload. Writing processed audio can be inside of "load" I think.
// This is used instead of a HashMap to enforce a fixed set of keys and | ||
// customize serialization behaviour for readability | ||
pub struct SampleSlots { | ||
slots: [Option<String>; 200], | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe there's an easier way to achieve this. Smth like
type SampleSlotsInner = BTreeMap<u8, String>; // No need to be public
#[derive(Default, Serialize, Deserialize)]
#[serde(try_from = "SampleSlotsInner")]
pub struct SampleSlots(pub SampleSlotsInner);
// If you want to restrict sample index
impl TryFrom<SampleSlotsInner> for SampleSlots {
type Error = anyhow::Error; // or String?
fn try_from(map: SampleSlotsInner) -> Result<Self, Self::Error> {
if value.last_key_value().map_or(false, |(&idx, _)| idx >= 200 /* make me const */ ) {
bail!("Sample idx too big");
}
}
}
This way we don't need to implement (and don't have to support) [De-]Serialize
, visitors and other stuff. Tough it lacks slot index restriction when the struct is constructed in a way other than through deserialization, but I doubt this is important at this point.
Also, this struct feels too big to be stored on stack (nothing critical though). And I can imagine String
being replaced with something like this in the future.
struct SampleSlot {
name: String,
path: Option<PathBuf>,
channel: Option<MonoMode>,
... // Some other metadata?
}
This is just an idea of how to extend this in the future, so one could have several "volca loadouts" of their sample collection without need to store multiple sample copies.
self.upload_sample_from_file( | ||
file_name, | ||
Some(i as u8), | ||
MonoMode::Mid, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a cli argument for default mode?
} else { | ||
self.delete_sample(i as u8, true)?; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would make this optional. Imagine swapping sample "slices" or "masks".
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct BackupData { | ||
pub sample_slots: SampleSlots, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
samples
would be better, I believe.
If someday pattern-related stuff gets implemented backups could look like
samples:
... # sample data
patterns: # or sequences
... # pattern data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove this file please.
None => "", | ||
}; | ||
|
||
if ext != "yaml" { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe yml
is also a valid extension. I think you can remove this check and let serde do its job.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I find this naming ("domain") a little confusing in this crate. While I realise it's more of a personal preference, I think calling this module a "library" and (maybe) reducing nesting (moving BackupData
and SampleSlots
up) will make it easier to read the code.
use serde::de::{Deserialize, Deserializer, MapAccess, Visitor}; | ||
use serde::ser::{Serialize, SerializeMap, Serializer}; | ||
use std::fmt; | ||
use std::ops::{Index, IndexMut}; | ||
|
||
extern crate serde_yaml; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use serde::de::{Deserialize, Deserializer, MapAccess, Visitor}; | |
use serde::ser::{Serialize, SerializeMap, Serializer}; | |
use std::fmt; | |
use std::ops::{Index, IndexMut}; | |
extern crate serde_yaml; | |
use std::fmt; | |
use std::ops::{Index, IndexMut}; | |
use serde::de::{Deserialize, Deserializer, MapAccess, Visitor}; | |
use serde::ser::{Serialize, SerializeMap, Serializer}; |
I've run basic tests on my own Volca Sample 2 to confirm the functionality is working, I will probably test more extensively soon.
See #1