Skip to content

Commit

Permalink
add global constants to the documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
tertsdiepraam committed Dec 6, 2024
1 parent b56052f commit 5a809de
Showing 1 changed file with 31 additions and 10 deletions.
41 changes: 31 additions & 10 deletions src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ pub struct DocumentedFunc<F> {
pub struct RuntimeConstant {
pub name: Identifier,
pub ty: TypeId,
pub docstring: String,
pub bytes: Box<[u8]>,
}

Expand Down Expand Up @@ -389,7 +390,8 @@ impl Runtime {

pub fn register_constant<T: 'static>(
&mut self,
name: &str,
name: impl Into<String>,
docstring: &str,
x: T,
) -> Result<(), String> {
let type_id = TypeId::of::<T>();
Expand All @@ -403,12 +405,13 @@ impl Runtime {
)
};

let symbol = Identifier::from(name);
let symbol = Identifier::from(name.into());
self.constants.insert(
symbol,
RuntimeConstant {
name: symbol,
ty: type_id,
docstring: docstring.into(),
bytes: bytes.into_boxed_slice(),
},
);
Expand Down Expand Up @@ -458,12 +461,12 @@ impl Runtime {
Ok(())
}

fn print_function(&self, f: &RuntimeFunction) {
let print_ty = |ty: TypeId| {
let ty = self.get_runtime_type(ty).unwrap();
ty.name.as_ref()
};
fn print_ty(&self, ty: TypeId) -> &str {
let ty = self.get_runtime_type(ty).unwrap();
ty.name.as_ref()
}

fn print_function(&self, f: &RuntimeFunction) {
let RuntimeFunction {
name,
description,
Expand All @@ -475,7 +478,7 @@ impl Runtime {
let mut params = description
.parameter_types()
.iter()
.map(|ty| print_ty(*ty))
.map(|ty| self.print_ty(*ty))
.collect::<Vec<_>>();
let ret = params.remove(0);

Expand All @@ -488,7 +491,7 @@ impl Runtime {
format!("{}.", params.next().unwrap())
}
FunctionKind::StaticMethod(id) => {
format!("{}.", print_ty(id))
format!("{}.", self.print_ty(id))
}
FunctionKind::Free => "".into(),
};
Expand Down Expand Up @@ -532,6 +535,22 @@ impl Runtime {
self.print_function(f);
}

for RuntimeConstant {
name,
ty,
docstring,
..
} in self.constants.values()
{
println!(
"`````{{roto::constant}} {name}: {}",
self.print_ty(*ty)
);
for line in docstring.lines() {
println!("{line}");
}
println!("`````\n");
}
for RuntimeType {
name,
type_id,
Expand All @@ -541,7 +560,7 @@ impl Runtime {
{
println!("`````{{roto:type}} {name}");
for line in docstring.lines() {
println!("{line}")
println!("{line}");
}
println!();

Expand Down Expand Up @@ -719,12 +738,14 @@ impl Runtime {

rt.register_constant(
"LOCALHOSTV4",
"The IPv4 address pointing to localhost: `127.0.0.1`",
IpAddr::from(Ipv4Addr::LOCALHOST),
)
.unwrap();

rt.register_constant(
"LOCALHOSTV6",
"The IPv6 address pointing to localhost: `::1`",
IpAddr::from(Ipv6Addr::LOCALHOST),
)
.unwrap();
Expand Down

0 comments on commit 5a809de

Please sign in to comment.