1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
#![deny(
//    missing_docs,
    missing_debug_implementations,
    missing_copy_implementations,
    trivial_casts,
    trivial_numeric_casts,
    unsafe_code,
    unstable_features,
    unused_import_braces,
    unused_qualifications
)]

use configuration::Configuration;
use from_json;
use query::SearchQuery;
use EntityType;
use LocaleString;
use WikibaseError;

/// Search-result entity
///
/// Struct that holds all the data about a search result. The difference
/// to a normal entity is that no claims and sitelinks are returned. The
/// label, description and aliases are only returned for one language.
#[derive(Debug, Clone)]
pub struct SearchResultEntity {
    id: String,
    entity_type: EntityType,
    label: LocaleString,
    description: Option<LocaleString>,
    aliases: Vec<LocaleString>,
}

impl SearchResultEntity {
    pub fn new<S: Into<String>>(
        id: S,
        entity_type: EntityType,
        label: LocaleString,
        description: Option<LocaleString>,
        aliases: Vec<LocaleString>,
    ) -> SearchResultEntity {
        Self {
            id: id.into(),
            entity_type,
            label,
            description,
            aliases,
        }
    }

    pub fn aliases(&self) -> &Vec<LocaleString> {
        &self.aliases
    }

    pub fn description(&self) -> &Option<LocaleString> {
        &self.description
    }

    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn label(&self) -> &LocaleString {
        &self.label
    }

    pub fn set_aliases(&mut self, aliases: Vec<LocaleString>) {
        self.aliases = aliases;
    }

    pub fn set_descriptions(&mut self, description: Option<LocaleString>) {
        self.description = description;
    }

    pub fn set_labels(&mut self, label: LocaleString) {
        self.label = label;
    }
}

/// Search results
///
/// A struct holding a vector of search result entities.
#[derive(Debug, Clone)]
pub struct SearchResults {
    results: Vec<SearchResultEntity>,
}

impl SearchResults {
    pub fn new(results: Vec<SearchResultEntity>) -> SearchResults {
        Self { results }
    }

    /// Takes a wikibase entity query and returns a result of an error.
    pub fn new_from_query(
        query: &SearchQuery,
        configuration: &Configuration,
    ) -> Result<SearchResults, WikibaseError> {
        let params = query.params();
        let request_result = configuration.api().get_query_api_json_all(&params);

        let json_response = match request_result {
            Ok(value) => value,
            Err(_error) => return Err(WikibaseError::Configuration("Search failed".to_string())),
        };

        from_json::search_result_entities_from_json(&json_response, &query)
    }

    pub fn results(&self) -> &Vec<SearchResultEntity> {
        &self.results
    }
}