Commit 80b3c02f authored by Leonardo Schwarz's avatar Leonardo Schwarz

Update Area to new entity api.

parent 205c2bce
Pipeline #121834 passed with stage
in 1 minute and 22 seconds
......@@ -12,7 +12,7 @@ use xpath_reader::reader::{FromXml, Reader};
use std::time::{Duration, Instant};
use std::thread::sleep;
use crate::search::{AreaSearchBuilder, ArtistSearchBuilder, ReleaseGroupSearchBuilder, SearchBuilder};
use crate::search::{ArtistSearchBuilder, ReleaseGroupSearchBuilder, SearchBuilder};
mod error;
pub(crate) use self::error::check_response_error;
......@@ -196,11 +196,11 @@ impl Client {
ErrorKind::Communication,
))
}
/*
/// Returns a search builder to search for an area.
pub fn search_area<'cl>(&'cl mut self) -> AreaSearchBuilder<'cl> {
AreaSearchBuilder::new(self)
}
}*/
/// Returns a search biulder to search for an artist.
pub fn search_artist<'cl>(&'cl mut self) -> ArtistSearchBuilder<'cl> {
......
use xpath_reader::{FromXml, Error, Reader};
use crate::entities::{Mbid, ResourceOld};
use crate::entities::{Mbid, Resource};
use crate::client::Request;
enum_mb_xml! {
/// Specifies what a specific `Area` instance actually is.
......@@ -39,28 +40,52 @@ enum_mb_xml! {
/// This is one of the *core entities* of MusicBrainz.
///
/// [MusicBrainz documentation](https://musicbrainz.org/doc/Area).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Area {
response: AreaResponse,
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct AreaResponse {
mbid: Mbid,
name: String,
sort_name: String,
area_type: AreaType,
iso_3166: Option<String>,
}
impl Area {
/// MBID of the entity in the MusicBrainz database.
pub mbid: Mbid,
pub fn mbid(&self) -> &Mbid {
&self.response.mbid
}
/// The name of the area.
pub name: String,
pub fn name(&self) -> &String {
&self.response.name
}
/// Name that is supposed to be used for sorting, containing only latin
/// characters.
pub sort_name: String,
pub fn sort_name(&self) -> &String {
&self.response.sort_name
}
/// Type of the area, gives more information about
pub area_type: AreaType,
pub fn area_type(&self) -> AreaType {
self.response.area_type.clone()
}
/// ISO 3166 code, assigned to countries and subdivisions.
pub iso_3166: Option<String>,
pub fn iso_3166(&self) -> Option<&String> {
self.response.iso_3166.as_ref()
}
}
impl FromXml for Area {
fn from_xml<'d>(reader: &'d Reader<'d>) -> Result<Area, Error> {
Ok(Area {
impl FromXml for AreaResponse {
fn from_xml<'d>(reader: &'d Reader<'d>) -> Result<AreaResponse, Error> {
Ok(AreaResponse {
mbid: reader.read(".//mb:area/@id")?,
name: reader.read(".//mb:area/mb:name/text()")?,
sort_name: reader.read(".//mb:area/mb:sort-name/text()")?,
......@@ -71,9 +96,22 @@ impl FromXml for Area {
}
}
impl ResourceOld for Area {
impl Resource for Area {
type Options = ();
type Response = AreaResponse;
const NAME: &'static str = "area";
const INCL: &'static str = "";
fn request(options: &Self::Options) -> Request {
Request {
name: "area".to_string(),
include: "".to_string(),
}
}
fn from_response(response: Self::Response, _: Self::Options) -> Self {
Area { response }
}
}
#[cfg(test)]
......@@ -84,24 +122,24 @@ mod tests {
#[test]
fn area_read_xml1() {
let mbid = Mbid::from_str("a1411661-be21-4290-8dc1-50f3d8e3ea67").unwrap();
let area: Area = crate::util::test_utils::fetch_entity_old(&mbid).unwrap();
let area: Area = crate::util::test_utils::fetch_entity(&mbid, ()).unwrap();
assert_eq!(area.mbid, mbid);
assert_eq!(area.name, "Honolulu".to_string());
assert_eq!(area.sort_name, "Honolulu".to_string());
assert_eq!(area.area_type, AreaType::City);
assert_eq!(area.iso_3166, None);
assert_eq!(area.mbid(), &mbid);
assert_eq!(area.name(), &"Honolulu".to_string());
assert_eq!(area.sort_name(), &"Honolulu".to_string());
assert_eq!(area.area_type(), AreaType::City);
assert_eq!(area.iso_3166(), None);
}
#[test]
fn area_read_xml2() {
let mbid = Mbid::from_str("2db42837-c832-3c27-b4a3-08198f75693c").unwrap();
let area: Area = crate::util::test_utils::fetch_entity_old(&mbid).unwrap();
let area: Area = crate::util::test_utils::fetch_entity(&mbid, ()).unwrap();
assert_eq!(area.mbid, mbid);
assert_eq!(area.name, "Japan".to_string());
assert_eq!(area.sort_name, "Japan".to_string());
assert_eq!(area.area_type, AreaType::Country);
assert_eq!(area.iso_3166, Some("JP".to_string()));
assert_eq!(area.mbid(), &mbid);
assert_eq!(area.name(), &"Japan".to_string());
assert_eq!(area.sort_name(), &"Japan".to_string());
assert_eq!(area.area_type(), AreaType::Country);
assert_eq!(area.iso_3166(), Some(&"JP".to_string()));
}
}
......@@ -161,13 +161,11 @@ macro_rules! ref_fetch_full_old
}
ref_fetch_full!(
ReleaseRef,
crate::entities::Release,
crate::entities::ReleaseOptions
ReleaseRef, crate::entities::Release, crate::entities::ReleaseOptions;
AreaRef, crate::entities::Area, ()
);
ref_fetch_full_old!(
AreaRef, crate::entities::Area;
ArtistRef, crate::entities::Artist;
LabelRef, crate::entities::Label;
RecordingRef, crate::entities::Recording
......
......@@ -141,6 +141,7 @@ macro_rules! define_search_builder {
};
}
/*
define_search_builder!(
AreaSearchBuilder,
AreaSearchField,
......@@ -148,6 +149,7 @@ define_search_builder!(
full_entities::Area,
"area-list"
);
*/
define_search_builder!(
ArtistSearchBuilder,
......
......@@ -21,6 +21,7 @@ pub trait SearchEntity {
// It's the same entity.
pub use self::full_entities::Area;
/*
impl SearchEntity for Area {
type FullEntity = Area;
......@@ -28,6 +29,7 @@ impl SearchEntity for Area {
Ok(self.to_owned())
}
}
*/
pub use self::full_entities::Artist;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment