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
/*
Copyright (C) 2021 Kunal Mehta <legoktm@debian.org>

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

use crate::parsoid::{ImmutableWikicode, Wikicode};
use mwapi_responses::timestamp::Timestamp;
use serde::Deserialize;

/// Extra options for saving pages
pub struct SaveOptions {
    pub(crate) summary: String,
    pub(crate) mark_as_bot: Option<bool>,
    pub(crate) tags: Vec<String>,
}

impl SaveOptions {
    /// Create options with the given summary
    pub fn summary(summary: &str) -> Self {
        Self {
            summary: summary.to_string(),
            mark_as_bot: None,
            tags: vec![],
        }
    }

    /// Change the edit summary
    pub fn set_summary(mut self, summary: &str) -> Self {
        self.summary = summary.to_string();
        self
    }

    /// Overide the default mark_as_bot option with the given value
    pub fn mark_as_bot(mut self, mark: bool) -> Self {
        self.mark_as_bot = Some(mark);
        self
    }

    /// Add the given change tag to the edit
    pub fn add_tag(mut self, tag: &str) -> Self {
        self.tags.push(tag.to_string());
        self
    }
}

pub enum Saveable {
    Wikitext(String),
    Html(ImmutableWikicode),
}

impl From<String> for Saveable {
    fn from(wikitext: String) -> Self {
        Self::Wikitext(wikitext)
    }
}

impl From<&str> for Saveable {
    fn from(wikitext: &str) -> Self {
        Self::Wikitext(wikitext.to_string())
    }
}

impl From<&String> for Saveable {
    fn from(wikitext: &String) -> Self {
        Self::Wikitext(wikitext.to_string())
    }
}

impl From<Wikicode> for Saveable {
    fn from(html: Wikicode) -> Self {
        html.into_immutable().into()
    }
}

impl From<ImmutableWikicode> for Saveable {
    fn from(html: ImmutableWikicode) -> Self {
        Self::Html(html)
    }
}

/// Response from action=edit
///
/// TODO: Move to mwapi_responses
#[derive(Deserialize, Clone, Debug)]
pub struct EditResponse {
    pub contentmodel: String,
    pub newrevid: Option<u64>,
    pub newtimestamp: Option<Timestamp>,
    pub oldrevid: Option<u64>,
    #[serde(default)]
    pub nochange: bool,
    #[serde(default)]
    pub new: bool,
    pub pageid: u32,
    pub result: String,
    pub title: String,
}