Applied rustfmt

This commit is contained in:
Antoine Gersant 2017-05-07 21:21:30 -07:00
parent 56bb1d4e23
commit d073e02549
8 changed files with 155 additions and 112 deletions

View file

@ -74,7 +74,7 @@ pub fn get_api_handler(collection: Arc<Collection>) -> Mount {
} }
let mut auth_api_chain = Chain::new(auth_api_mount); let mut auth_api_chain = Chain::new(auth_api_mount);
let auth = AuthRequirement { collection:collection.clone() }; let auth = AuthRequirement { collection: collection.clone() };
auth_api_chain.link_around(auth); auth_api_chain.link_around(auth);
api_handler.mount("/", auth_api_chain); api_handler.mount("/", auth_api_chain);
@ -83,7 +83,10 @@ pub fn get_api_handler(collection: Arc<Collection>) -> Mount {
} }
fn path_from_request(request: &Request) -> Result<PathBuf> { fn path_from_request(request: &Request) -> Result<PathBuf> {
let path_string = request.url.path().join(&::std::path::MAIN_SEPARATOR.to_string()); let path_string = request
.url
.path()
.join(&::std::path::MAIN_SEPARATOR.to_string());
let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8()?; let decoded_path = percent_decode(path_string.as_bytes()).decode_utf8()?;
Ok(PathBuf::from(decoded_path.deref())) Ok(PathBuf::from(decoded_path.deref()))
} }
@ -96,7 +99,7 @@ impl AroundMiddleware for AuthRequirement {
fn around(self, handler: Box<Handler>) -> Box<Handler> { fn around(self, handler: Box<Handler>) -> Box<Handler> {
Box::new(AuthHandler { Box::new(AuthHandler {
collection: self.collection, collection: self.collection,
handler: handler handler: handler,
}) as Box<Handler> }) as Box<Handler>
} }
} }
@ -121,7 +124,8 @@ impl Handler for AuthHandler {
// Auth via Authorization header // Auth via Authorization header
if let Some(auth) = req.headers.get::<Authorization<Basic>>() { if let Some(auth) = req.headers.get::<Authorization<Basic>>() {
if let Some(ref password) = auth.password { if let Some(ref password) = auth.password {
auth_success = self.collection.auth(auth.username.as_str(), password.as_str()); auth_success = self.collection
.auth(auth.username.as_str(), password.as_str());
username = Some(auth.username.clone()); username = Some(auth.username.clone());
} }
} }

View file

@ -45,7 +45,9 @@ impl Collection {
} }
pub fn auth(&self, username: &str, password: &str) -> bool { pub fn auth(&self, username: &str, password: &str) -> bool {
self.users.iter().any(|u| u.name == username && u.password == password) self.users
.iter()
.any(|u| u.name == username && u.password == password)
} }
pub fn browse(&self, virtual_path: &Path) -> Result<Vec<CollectionFile>> { pub fn browse(&self, virtual_path: &Path) -> Result<Vec<CollectionFile>> {

View file

@ -52,7 +52,8 @@ impl Config {
let mut config_file_content = String::new(); let mut config_file_content = String::new();
config_file.read_to_string(&mut config_file_content)?; config_file.read_to_string(&mut config_file_content)?;
let parsed_config = toml::Parser::new(config_file_content.as_str()).parse(); let parsed_config = toml::Parser::new(config_file_content.as_str()).parse();
let parsed_config = parsed_config.ok_or("Could not parse config as valid TOML")?; let parsed_config = parsed_config
.ok_or("Could not parse config as valid TOML")?;
let mut config = Config { let mut config = Config {
secret: String::new(), secret: String::new(),
@ -77,7 +78,8 @@ impl Config {
} }
fn parse_secret(&mut self, source: &toml::Table) -> Result<()> { fn parse_secret(&mut self, source: &toml::Table) -> Result<()> {
self.secret = source.get(CONFIG_SECRET) self.secret = source
.get(CONFIG_SECRET)
.and_then(|s| s.as_str()) .and_then(|s| s.as_str())
.map(|s| s.to_owned()) .map(|s| s.to_owned())
.ok_or("Could not parse config secret")?; .ok_or("Could not parse config secret")?;
@ -173,8 +175,9 @@ impl Config {
_ => bail!("Could not parse DDNS settings table"), _ => bail!("Could not parse DDNS settings table"),
}; };
let host = let host = ddns.get(CONFIG_DDNS_HOST)
ddns.get(CONFIG_DDNS_HOST).and_then(|n| n.as_str()).ok_or("Could not parse DDNS host")?; .and_then(|n| n.as_str())
.ok_or("Could not parse DDNS host")?;
let username = ddns.get(CONFIG_DDNS_USERNAME) let username = ddns.get(CONFIG_DDNS_USERNAME)
.and_then(|n| n.as_str()) .and_then(|n| n.as_str())
.ok_or("Could not parse DDNS username")?; .ok_or("Could not parse DDNS username")?;

View file

@ -53,7 +53,10 @@ fn update_my_ip(ip: &String, config: &DDNSConfig) -> Result<(), DDNSError> {
password: Some(config.password.to_owned()), password: Some(config.password.to_owned()),
}); });
let res = client.get(full_url.as_str()).header(auth_header).send()?; let res = client
.get(full_url.as_str())
.header(auth_header)
.send()?;
match res.status { match res.status {
hyper::status::StatusCode::Ok => Ok(()), hyper::status::StatusCode::Ok => Ok(()),
s => Err(DDNSError::UpdateError(s)), s => Err(DDNSError::UpdateError(s)),

View file

@ -101,11 +101,9 @@ impl<'db> IndexBuilder<'db> {
Ok(IndexBuilder { Ok(IndexBuilder {
queue: queue, queue: queue,
db: db, db: db,
insert_directory: insert_directory: db.prepare("INSERT OR REPLACE INTO directories (path, parent, artwork, year, \
db.prepare("INSERT OR REPLACE INTO directories (path, parent, artwork, year, \
artist, album) VALUES (?, ?, ?, ?, ?, ?)")?, artist, album) VALUES (?, ?, ?, ?, ?, ?)")?,
insert_song: insert_song: db.prepare("INSERT OR REPLACE INTO songs (path, parent, disc_number, track_number, \
db.prepare("INSERT OR REPLACE INTO songs (path, parent, disc_number, track_number, \
title, year, album_artist, artist, album, artwork) VALUES (?, ?, ?, ?, \ title, year, album_artist, artist, album, artwork) VALUES (?, ?, ?, ?, \
?, ?, ?, ?, ?, ?)")?, ?, ?, ?, ?, ?, ?)")?,
}) })
@ -130,11 +128,14 @@ impl<'db> IndexBuilder<'db> {
CollectionFile::Directory(directory) => { CollectionFile::Directory(directory) => {
let parent = IndexBuilder::get_parent(directory.path.as_str()); let parent = IndexBuilder::get_parent(directory.path.as_str());
self.insert_directory.reset()?; self.insert_directory.reset()?;
self.insert_directory.bind(1, &Value::String(directory.path))?; self.insert_directory
self.insert_directory.bind(2, &string_option_to_value(parent))?; .bind(1, &Value::String(directory.path))?;
self.insert_directory
.bind(2, &string_option_to_value(parent))?;
self.insert_directory self.insert_directory
.bind(3, &string_option_to_value(directory.artwork))?; .bind(3, &string_option_to_value(directory.artwork))?;
self.insert_directory.bind(4, &i32_option_to_value(directory.year))?; self.insert_directory
.bind(4, &i32_option_to_value(directory.year))?;
self.insert_directory self.insert_directory
.bind(5, &string_option_to_value(directory.artist))?; .bind(5, &string_option_to_value(directory.artist))?;
self.insert_directory self.insert_directory
@ -147,15 +148,24 @@ impl<'db> IndexBuilder<'db> {
let parent = IndexBuilder::get_parent(song.path.as_str()); let parent = IndexBuilder::get_parent(song.path.as_str());
self.insert_song.reset()?; self.insert_song.reset()?;
self.insert_song.bind(1, &Value::String(song.path))?; self.insert_song.bind(1, &Value::String(song.path))?;
self.insert_song.bind(2, &string_option_to_value(parent))?; self.insert_song
self.insert_song.bind(3, &u32_option_to_value(song.disc_number))?; .bind(2, &string_option_to_value(parent))?;
self.insert_song.bind(4, &u32_option_to_value(song.track_number))?; self.insert_song
self.insert_song.bind(5, &string_option_to_value(song.title))?; .bind(3, &u32_option_to_value(song.disc_number))?;
self.insert_song.bind(6, &i32_option_to_value(song.year))?; self.insert_song
self.insert_song.bind(7, &string_option_to_value(song.album_artist))?; .bind(4, &u32_option_to_value(song.track_number))?;
self.insert_song.bind(8, &string_option_to_value(song.artist))?; self.insert_song
self.insert_song.bind(9, &string_option_to_value(song.album))?; .bind(5, &string_option_to_value(song.title))?;
self.insert_song.bind(10, &string_option_to_value(song.artwork))?; self.insert_song
.bind(6, &i32_option_to_value(song.year))?;
self.insert_song
.bind(7, &string_option_to_value(song.album_artist))?;
self.insert_song
.bind(8, &string_option_to_value(song.artist))?;
self.insert_song
.bind(9, &string_option_to_value(song.album))?;
self.insert_song
.bind(10, &string_option_to_value(song.artwork))?;
self.insert_song.next()?; self.insert_song.next()?;
} }
@ -458,7 +468,11 @@ impl Index {
let mut artwork = None; let mut artwork = None;
if let Some(artwork_path) = artwork_path { if let Some(artwork_path) = artwork_path {
artwork = match self.vfs.real_to_virtual(artwork_path) { artwork = match self.vfs.real_to_virtual(artwork_path) {
Ok(p) => Some(p.to_str().ok_or("Invalid song artwork path")?.to_owned()), Ok(p) => {
Some(p.to_str()
.ok_or("Invalid song artwork path")?
.to_owned())
}
_ => None, _ => None,
}; };
} }
@ -500,7 +514,11 @@ impl Index {
let mut artwork = None; let mut artwork = None;
if let Some(artwork_path) = artwork_path { if let Some(artwork_path) = artwork_path {
artwork = match self.vfs.real_to_virtual(artwork_path) { artwork = match self.vfs.real_to_virtual(artwork_path) {
Ok(p) => Some(p.to_str().ok_or("Invalid directory artwork path")?.to_owned()), Ok(p) => {
Some(p.to_str()
.ok_or("Invalid directory artwork path")?
.to_owned())
}
_ => None, _ => None,
}; };
} }
@ -523,13 +541,16 @@ impl Index {
let db = self.connect()?; let db = self.connect()?;
let path_string = real_path.to_string_lossy(); let path_string = real_path.to_string_lossy();
let mut select = let mut select = db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \
db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE \
parent = ? ORDER BY path COLLATE NOCASE ASC")?; parent = ? ORDER BY path COLLATE NOCASE ASC")?;
select.bind(1, &Value::String(path_string.deref().to_owned()))?; select
.bind(1, &Value::String(path_string.deref().to_owned()))?;
let output = self.select_directories(&mut select)?; let output = self.select_directories(&mut select)?;
let output = output.into_iter().map(|d| CollectionFile::Directory(d)).collect(); let output = output
.into_iter()
.map(|d| CollectionFile::Directory(d))
.collect();
Ok(output) Ok(output)
} }
@ -537,12 +558,15 @@ impl Index {
fn browse_songs(&self, real_path: &Path) -> Result<Vec<CollectionFile>> { fn browse_songs(&self, real_path: &Path) -> Result<Vec<CollectionFile>> {
let db = self.connect()?; let db = self.connect()?;
let path_string = real_path.to_string_lossy(); let path_string = real_path.to_string_lossy();
let mut select = let mut select = db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
artist, album, artwork FROM songs WHERE parent = ? ORDER BY track_number, path \ artist, album, artwork FROM songs WHERE parent = ? ORDER BY track_number, path \
COLLATE NOCASE ASC")?; COLLATE NOCASE ASC")?;
select.bind(1, &Value::String(path_string.deref().to_owned()))?; select
Ok(self.select_songs(&mut select)?.into_iter().map(|s| CollectionFile::Song(s)).collect()) .bind(1, &Value::String(path_string.deref().to_owned()))?;
Ok(self.select_songs(&mut select)?
.into_iter()
.map(|s| CollectionFile::Song(s))
.collect())
} }
pub fn browse(&self, virtual_path: &Path) -> Result<Vec<CollectionFile>> { pub fn browse(&self, virtual_path: &Path) -> Result<Vec<CollectionFile>> {
@ -578,18 +602,17 @@ impl Index {
let db = self.connect()?; let db = self.connect()?;
let real_path = self.vfs.virtual_to_real(virtual_path)?; let real_path = self.vfs.virtual_to_real(virtual_path)?;
let path_string = real_path.to_string_lossy().into_owned() + "%"; let path_string = real_path.to_string_lossy().into_owned() + "%";
let mut select = let mut select = db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
db.prepare("SELECT path, disc_number, track_number, title, year, album_artist, \
artist, album, artwork FROM songs WHERE path LIKE ? ORDER BY path \ artist, album, artwork FROM songs WHERE path LIKE ? ORDER BY path \
COLLATE NOCASE ASC")?; COLLATE NOCASE ASC")?;
select.bind(1, &Value::String(path_string.deref().to_owned()))?; select
.bind(1, &Value::String(path_string.deref().to_owned()))?;
self.select_songs(&mut select) self.select_songs(&mut select)
} }
pub fn get_random_albums(&self, count: u32) -> Result<Vec<Directory>> { pub fn get_random_albums(&self, count: u32) -> Result<Vec<Directory>> {
let db = self.connect()?; let db = self.connect()?;
let mut select = let mut select = db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album \
db.prepare("SELECT path, artwork, year, artist, album FROM directories WHERE album \
IS NOT NULL ORDER BY RANDOM() LIMIT ?")?; IS NOT NULL ORDER BY RANDOM() LIMIT ?")?;
select.bind(1, &Value::Integer(count as i64))?; select.bind(1, &Value::Integer(count as i64))?;
self.select_directories(&mut select) self.select_directories(&mut select)
@ -665,7 +688,8 @@ fn test_browse() {
khemmis_path.push("root"); khemmis_path.push("root");
khemmis_path.push("Khemmis"); khemmis_path.push("Khemmis");
let khemmis = CollectionFile::Directory(Directory { let khemmis =
CollectionFile::Directory(Directory {
path: khemmis_path.to_string_lossy().deref().to_string(), path: khemmis_path.to_string_lossy().deref().to_string(),
artist: None, artist: None,
album: None, album: None,
@ -677,7 +701,8 @@ fn test_browse() {
tobokegao_path.push("root"); tobokegao_path.push("root");
tobokegao_path.push("Tobokegao"); tobokegao_path.push("Tobokegao");
let tobokegao = CollectionFile::Directory(Directory { let tobokegao =
CollectionFile::Directory(Directory {
path: tobokegao_path.to_string_lossy().deref().to_string(), path: tobokegao_path.to_string_lossy().deref().to_string(),
artist: None, artist: None,
album: None, album: None,

View file

@ -120,9 +120,7 @@ fn run() -> Result<()> {
match config.ddns { match config.ddns {
Some(ref ddns_config) => { Some(ref ddns_config) => {
let ddns_config = ddns_config.clone(); let ddns_config = ddns_config.clone();
std::thread::spawn(|| { std::thread::spawn(|| { ddns::run(ddns_config); });
ddns::run(ddns_config);
});
} }
None => (), None => (),
}; };

View file

@ -139,7 +139,9 @@ fn read_vorbis(path: &Path) -> Result<SongTags> {
fn read_flac(path: &Path) -> Result<SongTags> { fn read_flac(path: &Path) -> Result<SongTags> {
let tag = metaflac::Tag::read_from_path(path)?; let tag = metaflac::Tag::read_from_path(path)?;
let vorbis = tag.vorbis_comments().ok_or("Missing Vorbis comments")?; let vorbis = tag.vorbis_comments().ok_or("Missing Vorbis comments")?;
let disc_number = vorbis.get("DISCNUMBER").and_then(|d| d[0].parse::<u32>().ok()); let disc_number = vorbis
.get("DISCNUMBER")
.and_then(|d| d[0].parse::<u32>().ok());
let year = vorbis.get("DATE").and_then(|d| d[0].parse::<i32>().ok()); let year = vorbis.get("DATE").and_then(|d| d[0].parse::<i32>().ok());
Ok(SongTags { Ok(SongTags {
artist: vorbis.artist().map(|v| v[0].clone()), artist: vorbis.artist().map(|v| v[0].clone()),

View file

@ -62,7 +62,9 @@ impl Vfs {
#[test] #[test]
fn test_virtual_to_real() { fn test_virtual_to_real() {
let mut config = VfsConfig::new(); let mut config = VfsConfig::new();
config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf()); config
.mount_points
.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
let vfs = Vfs::new(config); let vfs = Vfs::new(config);
let mut correct_path = PathBuf::new(); let mut correct_path = PathBuf::new();
@ -82,7 +84,9 @@ fn test_virtual_to_real() {
#[test] #[test]
fn test_virtual_to_real_no_trail() { fn test_virtual_to_real_no_trail() {
let mut config = VfsConfig::new(); let mut config = VfsConfig::new();
config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf()); config
.mount_points
.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
let vfs = Vfs::new(config); let vfs = Vfs::new(config);
let correct_path = Path::new("test_dir"); let correct_path = Path::new("test_dir");
let found_path = vfs.virtual_to_real(Path::new("root")).unwrap(); let found_path = vfs.virtual_to_real(Path::new("root")).unwrap();
@ -92,7 +96,9 @@ fn test_virtual_to_real_no_trail() {
#[test] #[test]
fn test_real_to_virtual() { fn test_real_to_virtual() {
let mut config = VfsConfig::new(); let mut config = VfsConfig::new();
config.mount_points.insert("root".to_owned(), Path::new("test_dir").to_path_buf()); config
.mount_points
.insert("root".to_owned(), Path::new("test_dir").to_path_buf());
let vfs = Vfs::new(config); let vfs = Vfs::new(config);
let mut correct_path = PathBuf::new(); let mut correct_path = PathBuf::new();