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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
pub mod ink;
pub mod instory;

use anyhow::{anyhow, bail, Error, Result};
pub use ink::Story;
use ink::{Knot, KnotName};
pub use instory::{Diagram, Response};
use instory::{Node, NodeKind};
use itertools::Itertools;
use lazy_static::lazy_static;
use regex::Regex;
use std::{fs, path::PathBuf, result::Result as StdResult, str::FromStr};
use structopt::StructOpt;
use url::Url;

/// Creates a closure that will count characters in the passed strings
/// and return `false` once the total count reaches `n`.
fn char_count_less_than(n: usize) -> impl FnMut(&String) -> bool {
  let mut count = 0;
  move |s: &String| {
    count += s.chars().count();
    count < n
  }
}

/// Deterministically generates knot name based on an instory node
fn temp_knot_name(node: &Node) -> KnotName {
  match &node.kind {
    NodeKind::Start => "start".into(),
    NodeKind::TextChoice { context } => {
      let knot_name = context
        .clone()
        .expect("Text node with no context")
        .text
        .split(|ch: char| ch.is_whitespace() || ch.is_ascii_punctuation())
        .filter(|s| !s.is_empty())
        .map(|s| {
          s.chars()
            .filter(|ch| ch.is_alphanumeric())
            .collect::<String>()
        })
        .take_while(char_count_less_than(30))
        .join("_");
      let id = node.id.to_string().chars().take(6).collect::<String>();
      format!("{}_{}", knot_name, id).into()
    }
  }
}

pub fn instory_to_ink(diagram: &Diagram) -> Result<Story> {
  let start_node = diagram
    .nodes
    .iter()
    .find(|node| node.kind == NodeKind::Start)
    .ok_or_else(|| anyhow!("no start node"))?;
  let first_node = diagram
    .choices(start_node)
    .first()
    .ok_or_else(|| anyhow!("start node has no child nodes"))?
    .1;

  let start_knot_name: KnotName = temp_knot_name(&first_node);
  let knots = diagram
    .nodes
    .iter()
    .filter_map(|node| match &node.kind {
      NodeKind::TextChoice { context } => {
        let knot = Knot {
          text: context
            .clone()
            .expect("context is never empty for text nodes")
            .text,
          choices: diagram
            .choices(node)
            .iter()
            .map(|(choice_option_text, choice_node)| {
              (choice_option_text.to_string(), temp_knot_name(choice_node))
            })
            .collect(),
        };
        let name = temp_knot_name(node);
        Some((name, knot))
      }
      _ => None,
    })
    .collect();

  Ok(Story {
    start: start_knot_name,
    knots,
  })
}

enum StoryLocator {
  Id(u32),
  File(PathBuf),
  Url(Url),
}

impl StoryLocator {
  fn get(&self) -> Result<Diagram> {
    fn download(url: &Url) -> Result<String> {
      Ok(ureq::get(&url.to_string()).call()?.into_string()?)
    }

    let json = match self {
      StoryLocator::File(file) => fs::read_to_string(file)?,
      StoryLocator::Id(id) => download(&instory::diagram_url_from_id(*id))?,
      StoryLocator::Url(url) => {
        lazy_static! {
          static ref RE: Regex =
            Regex::new(r"^https://instory.su/story/(?P<id>\d+)(/play)?").unwrap();
        }
        let url_string = url.clone().to_string();
        let captures = RE.captures(&url_string);
        if let Some(captures) = captures {
          let id = captures.name("id").unwrap().as_str().parse().unwrap();
          download(&instory::diagram_url_from_id(id))?
        } else {
          download(url)?
        }
      }
    };
    let response: instory::Response<instory::Diagram> = serde_json::from_str(&json)?;
    Ok(response.data)
  }
}

impl FromStr for StoryLocator {
  type Err = Error;

  fn from_str(s: &str) -> StdResult<Self, Self::Err> {
    if let Ok(id) = s.parse() {
      return Ok(StoryLocator::Id(id));
    }

    if let Ok(url) = s.parse() {
      return Ok(StoryLocator::Url(url));
    }

    if let Ok(path) = s.parse() {
      return Ok(StoryLocator::File(path));
    }

    bail!("Must be a story URL, a file path, or a story ID.")
  }
}

#[derive(StructOpt)]
pub struct Inkstory {
  story_locator: StoryLocator,
  #[structopt(long, help = "Replaces \"rn\" with newline")]
  fix_rn: bool,
}

impl Inkstory {
  pub fn new(locator: &str) -> Result<Self> {
    let inkstory = Inkstory {
      story_locator: locator.parse()?,
      fix_rn: false,
    };
    Ok(inkstory)
  }

  pub fn exec(&self) -> Result<()> {
    let diagram = self.story_locator.get()?;
    let mut story = instory_to_ink(&diagram)?;
    if self.fix_rn {
      story.knots.iter_mut().for_each(|(_, knot)| {
        knot.text = knot.text.replace("rn", "\n");
      });
    }
    print!("{}", story);
    Ok(())
  }
}