[core] API: compiling Seam 1 skeleton (types, signatures, stubs)
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
|
||||
use crate::error::ParseError;
|
||||
use crate::path::WinPath;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
/// How to start a game. The protocol variants become a URI through [`Launch::uri`]; [`Launch::Exe`] is started
|
||||
/// directly (never through a shell) with an explicit working folder.
|
||||
///
|
||||
/// The fields are public so callers can match on them, but every builder ([`Launch::steam`], [`Launch::epic`],
|
||||
/// [`Launch::exe`], [`Launch::domi_link`], [`Launch::web`]) validates, deserialising validates, and
|
||||
/// [`Launch::uri`] validates again, so a hand-built bad value yields no URI.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case", try_from = "LaunchRepr")]
|
||||
pub enum Launch {
|
||||
/// `steam://rungameid/<id>`.
|
||||
SteamRunGameId(u32),
|
||||
/// `com.epicgames.launcher://apps/<app_name>?action=launch&silent=true`; `app_name` is `[A-Za-z0-9_-]{1,64}`.
|
||||
Epic { app_name: String },
|
||||
/// GOG and manual games: the `.exe`, its working folder and the arguments, passed as separate arguments.
|
||||
Exe { exe: WinPath, working_dir: WinPath, args: Vec<String> },
|
||||
/// `domi-<app>://open`; `app` follows the Atrium id rules.
|
||||
DomiLink { app: String },
|
||||
/// A web game: `https://` only, the host on an allow-list the caller passes in.
|
||||
Url(String),
|
||||
}
|
||||
|
||||
impl Launch {
|
||||
/// A Steam launch; the app id may not be 0.
|
||||
pub fn steam(app_id: u32) -> Result<Launch, ParseError> {
|
||||
let _ = app_id;
|
||||
Err(ParseError::BadId)
|
||||
}
|
||||
|
||||
/// An Epic launch; `app_name` must match `[A-Za-z0-9_-]{1,64}`.
|
||||
pub fn epic(app_name: &str) -> Result<Launch, ParseError> {
|
||||
let _ = app_name;
|
||||
Err(ParseError::BadId)
|
||||
}
|
||||
|
||||
/// A direct launch. `exe` must end in `.exe` (so never a `.bat`, `.cmd` or `.lnk`, which Windows would hand to
|
||||
/// a shell); each argument is checked (no control characters, caps on count and length).
|
||||
pub fn exe(exe: WinPath, working_dir: WinPath, args: Vec<String>) -> Result<Launch, ParseError> {
|
||||
let _ = (exe, working_dir, args);
|
||||
Err(ParseError::BadPath("exe"))
|
||||
}
|
||||
|
||||
/// A Domi app link; `app` follows the Atrium id rules (`[a-z0-9-]{1,32}`, no `-` at either end).
|
||||
pub fn domi_link(app: &str) -> Result<Launch, ParseError> {
|
||||
let _ = app;
|
||||
Err(ParseError::BadId)
|
||||
}
|
||||
|
||||
/// A web game. The URL must be `https://<host>[/path][?query][#fragment]` with no user info and no port, and
|
||||
/// `host` must equal (ignoring case) one of `allowed_hosts`. The scheme and host are lowercased.
|
||||
pub fn web(url: &str, allowed_hosts: &[&str]) -> Result<Launch, ParseError> {
|
||||
let _ = (url, allowed_hosts);
|
||||
Err(ParseError::BadValue("url"))
|
||||
}
|
||||
|
||||
/// The exact URI for the protocol variants, rebuilt from the validated fields; `None` for [`Launch::Exe`] and
|
||||
/// for any field that fails validation. For [`Launch::Url`] this checks the URL's form only; use
|
||||
/// [`Launch::uri_checked`] to apply the host allow-list again.
|
||||
pub fn uri(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
/// As [`Launch::uri`], and a [`Launch::Url`] must also have its host in `allowed_hosts`.
|
||||
pub fn uri_checked(&self, allowed_hosts: &[&str]) -> Option<String> {
|
||||
let _ = allowed_hosts;
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// The wire shape of [`Launch`]; deserialising goes through it so every field is validated.
|
||||
#[derive(Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
enum LaunchRepr {
|
||||
SteamRunGameId(u32),
|
||||
Epic { app_name: String },
|
||||
Exe { exe: WinPath, working_dir: WinPath, args: Vec<String> },
|
||||
DomiLink { app: String },
|
||||
Url(String),
|
||||
}
|
||||
|
||||
impl TryFrom<LaunchRepr> for Launch {
|
||||
type Error = ParseError;
|
||||
|
||||
fn try_from(r: LaunchRepr) -> Result<Self, Self::Error> {
|
||||
match r {
|
||||
LaunchRepr::SteamRunGameId(id) => Launch::steam(id),
|
||||
LaunchRepr::Epic { app_name } => Launch::epic(&app_name),
|
||||
LaunchRepr::Exe { exe, working_dir, args } => Launch::exe(exe, working_dir, args),
|
||||
LaunchRepr::DomiLink { app } => Launch::domi_link(&app),
|
||||
LaunchRepr::Url(url) => {
|
||||
let _ = url;
|
||||
Err(ParseError::BadValue("url"))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user