Refactor UI trait, add fake ui

CandyCorvid
Jul 13, 2023, 10:57 AM
J7MKV7UTCGQVFRRN3UH6635NKXMCYHXAPZECCFPW2JA4MHMVFFCAC

Dependencies

  • [2] 3VVQWLOX Major refactor: modularisation
  • [3] FZIABKEV New tests, refactor to be more modular, improve prompt ui, add test run parameter
  • [4] WGRFJRTE Add tests, rename Status to Header, implement redirect and proper input handling
  • [*] DBKKKHC2 Initial commit

Change contents

  • edit in src/main.rs at line 14
    [3.478]
    [2.967]
    /// Represents a user interface
  • replacement in src/main.rs at line 16
    [2.982][2.982:1196]()
    fn show(&self, content: media::Media);
    fn warn<D: Display>(&self, warning: D);
    fn read(&self, prompt: impl Display) -> Option<String>;
    fn read_secret(&self, prompt: impl Display) -> Option<String>;
    [2.982]
    [2.1196]
    /// Render a page to the screen
    fn show(&mut self, content: media::Media);
    /// Display a warning
    fn warn<D: Display>(&mut self, warning: D);
    /// Display a prompt and read input from the user
    fn read(&mut self, prompt: impl Display) -> Option<String>;
    /// Display a prompt and read hidden input from the user
    fn read_secret(&mut self, prompt: impl Display) -> Option<String>;
  • edit in src/main.rs at line 25
    [2.1198][2.1198:1261]()
    impl Ui for () {
    fn show(&self, _content: media::Media) {}
  • replacement in src/main.rs at line 26
    [3.501][2.1262:1309](),[2.1309][3.761:762](),[3.761][3.761:762](),[3.762][2.1310:1372]()
    fn warn<D: Display>(&self, _warning: D) {}
    fn read(&self, _prompt: impl Display) -> Option<String> {
    [3.501]
    [2.1372]
    /// User interface that does nothing
    pub struct NopUi;
    impl Ui for NopUi {
    fn show(&mut self, _content: media::Media) {}
    fn warn<D: Display>(&mut self, _warning: D) {}
    fn read(&mut self, _prompt: impl Display) -> Option<String> {
  • edit in src/main.rs at line 34
    [3.1294]
    [3.2343]
    fn read_secret(&mut self, _prompt: impl Display) -> Option<String> {
    None
    }
    }
  • replacement in src/main.rs at line 39
    [3.2344][2.1386:1468]()
    fn read_secret(&self, _prompt: impl Display) -> Option<String> {
    None
    [3.2344]
    [3.2456]
    /// User interface that records method calls and returns canned inputs
    #[cfg(test)]
    #[derive(Default)]
    struct FakeUi {
    inputs: Vec<String>,
    events: Vec<FUIEvent>,
    }
    #[cfg(test)]
    enum FUIEvent {
    //TODO figure out how to solve lifetime issues
    // and store Media in this variant
    // Or just convert it to a string? idk
    Show,
    Warn(String),
    Read(String),
    ReadSecret(String),
    }
    #[cfg(test)]
    impl FakeUi {
    pub fn new() -> Self {
    Default::default()
    }
    }
    #[cfg(test)]
    impl Ui for FakeUi {
    fn show(&mut self, _content: media::Media) {
    self.events.push(FUIEvent::Show)
    }
    fn warn<D: Display>(&mut self, warning: D) {
    self.events.push(FUIEvent::Warn(warning.to_string()))
    }
    fn read(&mut self, prompt: impl Display) -> Option<String> {
    self.events.push(FUIEvent::Read(prompt.to_string()));
    self.inputs.pop()
    }
    fn read_secret(&mut self, prompt: impl Display) -> Option<String> {
    self.events.push(FUIEvent::ReadSecret(prompt.to_string()));
    self.inputs.pop()
  • edit in src/main.rs at line 103
    [3.6483]
    [3.6483]
    use crate::NopUi;
  • replacement in src/main.rs at line 109
    [2.1716][2.1716:1863]()
    crate::core::run_url::<crate::network::TcpNetwork<crate::danger::Naive>>(
    &mut crate::cli::Cli {},
    url,
    );
    [2.1716]
    [3.7125]
    crate::core::run_url::<crate::network::TcpNetwork<crate::danger::Naive>>(&mut NopUi, url);
  • replacement in src/core.rs at line 129
    [2.5642][2.5642:5739]()
    pub fn read_response_header(ui: &impl Ui, mut stream: impl Read) -> ioResult<response::Header> {
    [2.5642]
    [2.5739]
    pub fn read_response_header(ui: &mut impl Ui, mut stream: impl Read) -> ioResult<response::Header> {
  • replacement in src/core.rs at line 212
    [2.8166][2.8166:8264]()
    let head = read_response_header(&(), &mut &*resp_head).expect("failed to read response");
    [2.8166]
    [2.8264]
    let head = read_response_header(&mut crate::NopUi, &mut &*resp_head)
    .expect("failed to read response");
  • replacement in src/cli.rs at line 32
    [2.9200][2.9200:9228]()
    test_run(&cli);
    [2.9200]
    [2.9228]
    test_run(&mut cli);
  • replacement in src/cli.rs at line 45
    [2.9578][2.9578:9603]()
    fn test_run(cli: &Cli) {
    [2.9578]
    [2.9603]
    fn test_run(cli: &mut Cli) {
  • replacement in src/cli.rs at line 139
    [2.12486][2.12486:12523]()
    fn show(&self, content: Media) {
    [2.12486]
    [2.12523]
    fn show(&mut self, content: Media) {
  • replacement in src/cli.rs at line 146
    [2.12666][2.12666:12727]()
    fn read(&self, prompt: impl Display) -> Option<String> {
    [2.12666]
    [2.12727]
    fn read(&mut self, prompt: impl Display) -> Option<String> {
  • replacement in src/cli.rs at line 157
    [2.12951][2.12951:13019]()
    fn read_secret(&self, prompt: impl Display) -> Option<String> {
    [2.12951]
    [2.13019]
    fn read_secret(&mut self, prompt: impl Display) -> Option<String> {
  • replacement in src/cli.rs at line 174
    [2.13591][2.13591:13646]()
    fn warn<D: std::fmt::Display>(&self, warning: D) {
    [2.13591]
    [2.13646]
    fn warn<D: std::fmt::Display>(&mut self, warning: D) {